Simplify the notification tag.

No longer can store the value in a variable.
This commit is contained in:
Matthew Schinckel 2012-10-25 09:37:30 +10:30
parent b9425a4abb
commit 93cebc0f0a

View file

@ -1,56 +1,16 @@
# -*- coding: utf-8 -*-
from django.template import Library
from django.template.base import TemplateSyntaxError
from notifications.models import Notification
from django.template import Node
register = Library()
# TODO: Simplify this: it's a really simple tag!
class InboxCountNode(Node):
"For use in the notifications_unread tag"
def __init__(self, asvar=None):
self.asvar = asvar
def render(self, context):
"""
Return the count of unread messages for the user found in context,
(may be 0) or an empty string.
"""
try:
user = context['user']
if user.is_anonymous():
count = ''
else:
count = user.notifications.unread().count()
except (KeyError, AttributeError):
count = ''
if self.asvar:
context[self.asvar] = count
return ''
return count
@register.tag
def notifications_unread(parser, token):
"""
Give the number of unread notifications for a user,
or nothing (an empty string) for an anonymous user.
Storing the count in a variable for further processing is advised, such as::
{% notifications_unread as unread_count %}
...
{% if unread_count %}
You have <strong>{{ unread_count }}</strong> unread notifications.
{% endif %}
"""
bits = token.split_contents()
if len(bits) > 1:
if len(bits) != 3:
raise TemplateSyntaxError("'{0}' tag takes no argument or exactly two arguments".format(bits[0]))
if bits[1] != 'as':
raise TemplateSyntaxError("First argument to '{0}' tag must be 'as'".format(bits[0]))
return InboxCountNode(bits[2])
else:
return InboxCountNode()
@register.simple_tag(takes_context=True)
def notifications_unread(context):
if 'user' not in context:
return ''
user = context['user']
if user.is_anonymous():
return ''
return user.notifications.unread().count()