Generated: Sat 2013-04-06 17:51 SGT
Source file: /Users/martin/Repos/django-subscribe/subscribe/templatetags/subscribe_tags.py
Stats: 14 executed, 1 missed, 3 excluded, 36 ignored
"""Templatetags for the ``subscribe`` app."""from django import templatefrom django.contrib.contenttypes.models import ContentTypefrom ..models import Subscriptionregister = template.Library()@register.assignment_tagdef get_ctype(obj): """ Returns the ``ContentType`` for the given object. :param obj: Any object. """ return ContentType.objects.get_for_model(obj)@register.assignment_tagdef get_subscribers(obj): """ Returns the subscribers for a given object. :param obj: Any object. """ ctype = ContentType.objects.get_for_model(obj) return Subscription.objects.filter(content_type=ctype, object_id=obj.pk)@register.assignment_tagdef is_subscribed(user, obj): """ Returns ``True`` if the user is subscribed to the given object. :param user: A ``User`` instance. :param obj: Any object. """ if not user.is_authenticated(): return False ctype = ContentType.objects.get_for_model(obj) try: Subscription.objects.get( user=user, content_type=ctype, object_id=obj.pk) except Subscription.DoesNotExist: return False return True