Hide keyboard shortcuts

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 

4 

5register = template.Library() 

6 

7# Tags 

8 

9 

10@register.filter 

11def has_notifications(user): 

12 """Indicates whether the given user has unread notifications. 

13 

14 :param user: The user instance to be checked. 

15 :type user: AUTH_USER_MODEL 

16 

17 :rtype: bool 

18 

19 """ 

20 if user: 

21 return has_unread_notifications(user) 

22 

23 return False 

24 

25 

26@register.inclusion_tag("notifications/templates/tags/notification_bar.html") 

27def notification_bar(user): 

28 """Display a promoted notification. 

29 

30 :param user: The user instance for which the notification is displayed. 

31 :type user: AUTH_USER_MODEL 

32 

33 """ 

34 return { 

35 'notification': get_promoted_notification(user), 

36 } 

37 

38 

39@register.simple_tag 

40def promoted_notification(user): 

41 """Get latest promoted notification for a given user. 

42 

43 :param user: The user instance for which the notification is displayed. 

44 :type user: AUTH_USER_MODEL 

45 

46 :rtype: NotificationUser | None 

47 

48 .. code-block:: html 

49 

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 %} 

57 

58 """ 

59 return get_promoted_notification(user) 

60 

61 

62@register.filter() 

63def unread_notification_count(user): 

64 """Get a count of a user's unread notifications. 

65 

66 :param user: The user instance to be checked. 

67 :type user: AUTH_USER_MODEL 

68 

69 :rtype: int 

70 

71 """ 

72 return get_unread_notification_count(user) 

73 

74 

75@register.inclusion_tag("notifications/templates/tags/notification_items.html") 

76def unread_notifications(user): 

77 """Get the unread notifications for the given user. 

78 

79 :param user: The user instance to be checked. 

80 :type user: AUTH_USER_MODEL 

81 

82 """ 

83 qs = get_unread_notifications(user) 

84 

85 return { 

86 'notifications': qs, 

87 }