Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1from django import template
2from ..shortcuts import get_promoted_notification, get_unread_notification_count, get_unread_notifications, \
3 has_unread_notifications
5register = template.Library()
7# Tags
10@register.filter
11def has_notifications(user):
12 """Indicates whether the given user has unread notifications.
14 :param user: The user instance to be checked.
15 :type user: AUTH_USER_MODEL
17 :rtype: bool
19 """
20 if user:
21 return has_unread_notifications(user)
23 return False
26@register.inclusion_tag("notifications/templates/tags/notification_bar.html")
27def notification_bar(user):
28 """Display a promoted notification.
30 :param user: The user instance for which the notification is displayed.
31 :type user: AUTH_USER_MODEL
33 """
34 return {
35 'notification': get_promoted_notification(user),
36 }
39@register.simple_tag
40def promoted_notification(user):
41 """Get latest promoted notification for a given user.
43 :param user: The user instance for which the notification is displayed.
44 :type user: AUTH_USER_MODEL
46 :rtype: NotificationUser | None
48 .. code-block:: html
50 {% promoted_notification request.user as latest_notification %}
51 {% if latest_notification %}
52 <div class="notification">
53 <b>{{ latest_notification.subject }}</b>
54 {{ latest_notification.body }}
55 </div>
56 {% endif %}
58 """
59 return get_promoted_notification(user)
62@register.filter()
63def unread_notification_count(user):
64 """Get a count of a user's unread notifications.
66 :param user: The user instance to be checked.
67 :type user: AUTH_USER_MODEL
69 :rtype: int
71 """
72 return get_unread_notification_count(user)
75@register.inclusion_tag("notifications/templates/tags/notification_items.html")
76def unread_notifications(user):
77 """Get the unread notifications for the given user.
79 :param user: The user instance to be checked.
80 :type user: AUTH_USER_MODEL
82 """
83 qs = get_unread_notifications(user)
85 return {
86 'notifications': qs,
87 }