From 368e41d46917b8da76e32fa2fdfe37439733b2ce Mon Sep 17 00:00:00 2001 From: Alvaro Leonel Date: Thu, 7 Jun 2018 23:01:50 -0300 Subject: [PATCH] Fix #164 Add a filter to check if an user has notifications --- .../templatetags/notifications_tags.py | 7 +++ notifications/tests/tests.py | 51 +++++++++++++++---- 2 files changed, 49 insertions(+), 9 deletions(-) diff --git a/notifications/templatetags/notifications_tags.py b/notifications/templatetags/notifications_tags.py index b0fc507..8e9d425 100644 --- a/notifications/templatetags/notifications_tags.py +++ b/notifications/templatetags/notifications_tags.py @@ -27,6 +27,13 @@ else: notifications_unread = register.assignment_tag(takes_context=True)(notifications_unread) # noqa +@register.filter +def has_notification(user): + if user: + return user.notifications.unread().exists() + return False + + # Requires vanilla-js framework - http://vanilla-js.com/ @register.simple_tag def register_notify_callbacks(badge_class='live_notify_badge', # pylint: disable=too-many-arguments,missing-docstring diff --git a/notifications/tests/tests.py b/notifications/tests/tests.py index fd4a7cf..37826d5 100644 --- a/notifications/tests/tests.py +++ b/notifications/tests/tests.py @@ -6,16 +6,17 @@ Replace this with more appropriate tests for your application. # -*- coding: utf-8 -*- # pylint: disable=too-many-lines,missing-docstring import json -import pytz +import pytz from django.conf import settings from django.contrib.auth.models import Group, User from django.core.exceptions import ImproperlyConfigured +from django.template import Context, Template from django.test import RequestFactory, TestCase from django.utils import timezone from django.utils.timezone import localtime, utc -from notifications.signals import notify from notifications.models import Notification, notify_handler +from notifications.signals import notify from notifications.utils import id2slug try: @@ -224,22 +225,24 @@ class NotificationTestPages(TestCase): def test_next_pages(self): self.login('to', 'pwd') - response = self.client.get(reverse('notifications:mark_all_as_read'), data={ - "next": reverse('notifications:unread'), + query_parameters = '?var1=hello&var2=world' + + response = self.client.get(reverse('notifications:mark_all_as_read'),data={ + "next": reverse('notifications:unread') + query_parameters, }) - self.assertRedirects(response, reverse('notifications:unread')) + self.assertRedirects(response, reverse('notifications:unread') + query_parameters) slug = id2slug(self.to_user.notifications.first().id) response = self.client.get(reverse('notifications:mark_as_read', args=[slug]), data={ - "next": reverse('notifications:unread'), + "next": reverse('notifications:unread') + query_parameters, }) - self.assertRedirects(response, reverse('notifications:unread')) + self.assertRedirects(response, reverse('notifications:unread') + query_parameters) slug = id2slug(self.to_user.notifications.first().id) response = self.client.get(reverse('notifications:mark_as_unread', args=[slug]), { - "next": reverse('notifications:unread'), + "next": reverse('notifications:unread') + query_parameters, }) - self.assertRedirects(response, reverse('notifications:unread')) + self.assertRedirects(response, reverse('notifications:unread') + query_parameters) def test_delete_messages_pages(self): self.login('to', 'pwd') @@ -417,3 +420,33 @@ class NotificationTestExtraData(TestCase): data = json.loads(response.content.decode('utf-8')) self.assertEqual(data['unread_list'][0]['data']['url'], "/learn/ask-a-pro/q/test-question-9/299/") self.assertEqual(data['unread_list'][0]['data']['other_content'], "Hello my 'world'") + + +class TagTest(TestCase): + ''' Django notifications automated tags tests ''' + def setUp(self): + self.message_count = 1 + self.from_user = User.objects.create_user(username="from", password="pwd", email="example@example.com") + self.to_user = User.objects.create_user(username="to", password="pwd", email="example@example.com") + self.to_user.is_staff = True + self.to_user.save() + for _ in range(self.message_count): + notify.send( + self.from_user, + recipient=self.to_user, + verb='commented', + action_object=self.from_user, + url="/learn/ask-a-pro/q/test-question-9/299/", + other_content="Hello my 'world'" + ) + + def tag_test(self, template, context, output): + t = Template('{% load notifications_tags %}'+template) + c = Context(context) + self.assertEqual(t.render(c), output) + + def test_has_notification(self): + template = "{{ user|has_notification }}" + context = {"user":self.to_user} + output = u"True" + self.tag_test(template, context, output)