Commits

belegnar  committed 912c63e

twitter crossposting

  • Participants
  • Parent commits 0109284

Comments (0)

Files changed (4)

File apps/twitterpost/__init__.py

Empty file added.

File apps/twitterpost/admin.py

+from django.contrib import admin
+
+from blog.models import Post
+from blog.admin import PostAdmin
+from lib.helpers import signals
+
+from twitterpost.models import TwitterPost
+
+class TwitterPostAdmin(admin.ModelAdmin):
+    list_display = ('post', 'need_crosspost')
+
+admin.site.register(TwitterPost, TwitterPostAdmin)
+
+class TwitterPostInlineAdmin(admin.TabularInline):
+    model = TwitterPost
+    max_num = 1
+    fk_name = 'post'
+
+PostAdmin.list_display += ('tw_object', )
+
+pa = admin.site._registry[Post]
+pa.inline_instances.append(TwitterPostInlineAdmin(pa.model, pa.admin_site))
+
+#signals.post_save(sender = TwitterPost)(tw_crosspost)

File apps/twitterpost/models.py

+from django.contrib.sites.models import Site
+from django.db import models
+from django.db.models.signals import post_save
+from django.template import Context
+from django.template.loader import get_template
+from django.utils.translation import ugettext_lazy as _
+
+import settings
+import twitter
+
+from lib.helpers import get_object_or_none
+from blog.models import Post
+
+def tw_create(local_post, remote_post):
+    api = twitter.Api(consumer_key = settings.TWITTER_CONSUMER_KEY,
+                      consumer_secret = settings.TWITTER_CONSUMER_SECRET,
+                      access_token_key = settings.TWITTER_ACCESS_TOKEN_KEY,
+                      access_token_secret = settings.TWITTER_ACCESS_TOKEN_SECRET)
+
+    msg = get_template('twitterpost/body.txt').render(Context({'header' : local_post.name,
+                                                               'link' : u'http://%s%s' % (Site.objects.get_current(), local_post.get_absolute_url())}))
+    status = api.PostUpdate(msg)
+    remote_post.tw_id = status.id
+
+class TwitterPost(models.Model):
+    post = models.ForeignKey(Post, editable = False, db_column = 'post_id', related_name = 'tw_post')
+    tw_id = models.IntegerField(editable = False, blank = True, null = False)
+    need_crosspost = models.BooleanField(verbose_name = _('Needs crossposting'), default = False)
+
+    def __unicode__(self):
+        return u'%s' % self.post
+
+def tw_crosspost(instance, **kwargs):
+    post = instance.post
+    if not post.is_draft and instance.need_crosspost:
+        if instance.tw_id is None:
+            tw_create(post, instance)
+
+post_save.connect(tw_crosspost, sender = TwitterPost, weak = False)
+
+def get_tw_object_link(post):
+    tw = get_object_or_none(TwitterPost, post=post)
+    if tw:
+        return '<a href="../../twitter/twitterpost/%s/">edit</a>' % tw.pk
+    else:
+        return ''
+
+get_tw_object_link.allow_tags = True
+
+Post.add_to_class('tw_object', get_tw_object_link)

File templates/twitterpost/body.txt

+{{ header|striptags|slice:":120" }} {{ link }}