Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Below is an example of some signals I use in Django to update denormalized counts and to Tweet out
- automatically when certain records are created.
- """
- def channel_post_save(sender, instance, created, **kwargs):
- """
- Increment the number of channels in the category. A corollary `channel_post_delete` would be
- created as well to reverse this change out. I attach my signals in my models.py and save
- them in signals.py.
- """
- if created is True:
- instance.category.channels = instance.category.channels + 1
- instance.category.save()
- # Obviously we shouldn't tweet if we're in DEBUG mode. Also, we don't
- # tweet out our own channel creations. Just other 3rd party channels.
- if settings.DEBUG is True and instance.name[:5] != "TMTTI":
- msg = 'Just added %s (%s) to our index!'
- api = twitter.Api(username="your_username", password="secret")
- api.PostUpdate(msg % (instance.name, instance.site))
- def url_post_save(sender, instance, created, **kwargs):
- """
- Increment the number of URLs in both the category and in the channel
- """
- if created is True:
- instance.channel.category.urls = instance.channel.category.urls + 1
- instance.channel.category.save()
- instance.channel.urls = instance.channel.urls + 1
- instance.channel.save()
Add Comment
Please, Sign In to add comment