Copy Source | Copy HTML import twython.core as twython
Copy Source | Copy HTML
- class Post (models.Model):
- title = models.CharField (max_length = 100 )
- media = models.TextField ()
- published = models.BooleanField (default = False)
- tweeted = models.BooleanField (default = False, editable = False)
- def __unicode__ (self):
- return u '% s' % self .title
- @ models.permalink
- def get_absolute_url (self):
- return ( 'news.views.news_view' , [ str ( self .id)])
Copy Source | Copy HTML
- def post_to_twitter (sender, instance, ** kwargs):
- "" " <br/> We send an announcement in TWI if the post is published but not yet closed <br/> " ""
- if instance.published and not instance.tweeted:
- try :
- twitter = twython.setup (username = "TWITTER_USER" , password = "TWITTER_PASSWORD" )
- long_url = "http: //% s% s" % (Site.objects.get_current (), instance.get_absolute_url ())
- short_url = twitter.shortenURL (long_url)
- twi_message = instance.title + "" + short_url
- try :
- twitter.updateStatus (twi_message)
- Post.objects. filter (pk = instance.pk) .update (tweeted = True)
- except TwythonError:
- pass
- except AuthError:
- pass
post_save.connect(post_to_twitter, sender=Post)
Source: https://habr.com/ru/post/88811/
All Articles