Skip to content

Instantly share code, notes, and snippets.

@joestump
Created September 9, 2009 22:28
Show Gist options
  • Save joestump/184140 to your computer and use it in GitHub Desktop.
Save joestump/184140 to your computer and use it in GitHub Desktop.
"""
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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment