subscribe.templatetags.subscriptions_tags: 18 total statements, 93.3% covered

Generated: Thu 2013-02-28 14:37 SGT

Source file: /Users/martin/Repos/django-subscribe/subscribe/templatetags/subscriptions_tags.py

Stats: 14 executed, 1 missed, 3 excluded, 36 ignored

  1. """Templatetags for the ``subscriptions`` app."""
  2. from django import template
  3. from django.contrib.contenttypes.models import ContentType
  4. from ..models import Subscription
  5. register = template.Library()
  6. @register.assignment_tag
  7. def get_ctype(obj):
  8. """
  9. Returns the ``ContentType`` for the given object.
  10. :param obj: Any object.
  11. """
  12. return ContentType.objects.get_for_model(obj)
  13. @register.assignment_tag
  14. def get_subscribers(obj):
  15. """
  16. Returns the subscribers for a given object.
  17. :param obj: Any object.
  18. """
  19. ctype = ContentType.objects.get_for_model(obj)
  20. return Subscription.objects.filter(content_type=ctype, object_id=obj.pk)
  21. @register.assignment_tag
  22. def is_subscribed(user, obj):
  23. """
  24. Returns ``True`` if the user is subscribed to the given object.
  25. :param user: A ``User`` instance.
  26. :param obj: Any object.
  27. """
  28. if not user.is_authenticated():
  29. return False
  30. ctype = ContentType.objects.get_for_model(obj)
  31. try:
  32. Subscription.objects.get(
  33. user=user, content_type=ctype, object_id=obj.pk)
  34. except Subscription.DoesNotExist:
  35. return False
  36. return True