mirror of
https://github.com/Hopiu/django-notifications.git
synced 2026-05-19 18:01:54 +00:00
Simplify the notification tag.
No longer can store the value in a variable.
This commit is contained in:
parent
b9425a4abb
commit
93cebc0f0a
1 changed files with 9 additions and 49 deletions
|
|
@ -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()
|
||||
Loading…
Reference in a new issue