Tweeting from a django application

Download Template

A recent feature request for hatch was to have a twitter feed. A twitter feed would allow people to be notified when new config templates are created via twitter.


akonkol
February 17, 2012






%{--- %{project_name}%/settings.py ---}%
TWITTER_ACCOUNTS = {
    'public': {
        'consumer_key':        '%{consumer_key}%',
        'consumer_secret':     '%{consumer_secret}%',
        'access_token_key':    '%{access_token_key}%,
        'access_token_secret': '%{access_token_secret}%'
     },
}


%{--- %{project_name}%/%{application_name}%/models.py ---}%
from django.contrib.sites.models import Site

def send_tweet(title, url)
   import twitter
   from django.conf import settings
   try:
      api = twitter.Api(**account)
      tweet_message = "%s just added %s" %(title, url)
      status = api.PostUpdate(tweet_message)
   exception:
      # I don't care if the tweet didn't go through.
      pass



def class Post(models.Model)
	...
        ...
        ...
	
        def get_absolute_url(self):
	   #below generates the absolute url for this object
           return "http://%s/posts/%s" % (Site.objects.get_current().domain, self.slug)

	def save(self, *args, **kwargs):
	   super(Post, self).save()
	   send_tweet(self.name, self.get_absolute_url())

You must be logged in to comment.