Guest User

Untitled

a guest
Mar 12th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. """
  2. Below is an example of some signals I use in Django to update denormalized counts and to Tweet out
  3. automatically when certain records are created.
  4. """
  5.  
  6. def channel_post_save(sender, instance, created, **kwargs):
  7. """
  8. Increment the number of channels in the category. A corollary `channel_post_delete` would be
  9. created as well to reverse this change out. I attach my signals in my models.py and save
  10. them in signals.py.
  11. """
  12. if created is True:
  13. instance.category.channels = instance.category.channels + 1
  14. instance.category.save()
  15.  
  16. # Obviously we shouldn't tweet if we're in DEBUG mode. Also, we don't
  17. # tweet out our own channel creations. Just other 3rd party channels.
  18. if settings.DEBUG is True and instance.name[:5] != "TMTTI":
  19. msg = 'Just added %s (%s) to our index!'
  20. api = twitter.Api(username="your_username", password="secret")
  21. api.PostUpdate(msg % (instance.name, instance.site))
  22.  
  23. def url_post_save(sender, instance, created, **kwargs):
  24. """
  25. Increment the number of URLs in both the category and in the channel
  26. """
  27. if created is True:
  28. instance.channel.category.urls = instance.channel.category.urls + 1
  29. instance.channel.category.save()
  30.  
  31. instance.channel.urls = instance.channel.urls + 1
  32. instance.channel.save()
Add Comment
Please, Sign In to add comment