Browse Source

Fix #463: accept multiple triggers as argument

Rafał Pitoń 10 years ago
parent
commit
ae6d0266a2

+ 6 - 6
docs/developers/notifications.rst

@@ -18,16 +18,16 @@ notify_user
 * ``trigger:`` short text used to identify this message for ``read_user_notification`` function.
 * ``trigger:`` short text used to identify this message for ``read_user_notification`` function.
 * ``formats:`` Optional. Dict of formats for ``message`` argument that should be boldened.
 * ``formats:`` Optional. Dict of formats for ``message`` argument that should be boldened.
 * ``sender:`` Optional. User that notification origins from.
 * ``sender:`` Optional. User that notification origins from.
-* ``update_user:``Optional. Boolean controlling if to call ``user.update`` after setting notification, or not.
+* ``update_user:`` Optional. Boolean controlling if to call ``user.update`` after setting notification, or not. Defaults to ``True``.
 
 
 
 
-read_user_notification
-----------------------
+read_user_notifications
+-----------------------
 
 
-.. function:: read_user_notification(user, trigger, atomic=True)
+.. function:: read_user_notifications(user, triggers, atomic=True)
 
 
-Sets user notification identified by ``trigger`` as read. This function checks internally if user has new notifications before it queries database.
+Sets user notifications identified by ``triggers`` as read. This function checks internally if user has new notifications before it queries database.
 
 
 * ``user:`` User to whom notification belongs to
 * ``user:`` User to whom notification belongs to
-* ``trigger:`` Short text used to identify messages to trigger as read.
+* ``triggers:`` Short text or list of short texts used to identify notifications that will be set as read.
 * ``atomic:`` Lets you control if you should wrap this in dedicated transaction.
 * ``atomic:`` Lets you control if you should wrap this in dedicated transaction.

+ 13 - 7
misago/notifications/api.py

@@ -8,7 +8,7 @@ from misago.notifications.utils import hash_trigger
 
 
 __all__ = [
 __all__ = [
     'notify_user',
     'notify_user',
-    'read_user_notification',
+    'read_user_notifications',
     'read_all_user_alerts',
     'read_all_user_alerts',
     'assert_real_new_notifications_count',
     'assert_real_new_notifications_count',
 ]
 ]
@@ -46,19 +46,25 @@ def notify_user(user, message, url, trigger=None, formats=None, sender=None,
     return new_notification
     return new_notification
 
 
 
 
-def read_user_notification(user, trigger, atomic=True):
+def read_user_notifications(user, triggers, atomic=True):
     if user.is_authenticated() and user.new_notifications:
     if user.is_authenticated() and user.new_notifications:
         if atomic:
         if atomic:
             with transaction.atomic():
             with transaction.atomic():
-                _real_read_user_notification(user, trigger)
+                _real_read_user_notifications(user, triggers)
         else:
         else:
-            _real_read_user_notification(user, trigger)
+            _real_read_user_notifications(user, triggers)
 
 
 
 
-def _real_read_user_notification(user, trigger):
-    trigger_hash = hash_trigger(trigger)
+def _real_read_user_notifications(user, triggers):
+    if isinstance(triggers, basestring):
+        triggers = [triggers]
+
+    hashes = [hash_trigger(trigger) for trigger in triggers]
     update_qs = user.misago_notifications.filter(is_new=True)
     update_qs = user.misago_notifications.filter(is_new=True)
-    update_qs = update_qs.filter(trigger=trigger_hash)
+    if len(hashes) == 1:
+        update_qs = update_qs.filter(trigger=hashes[0])
+    else:
+        update_qs = update_qs.filter(trigger__in=hashes)
     updated = update_qs.update(is_new=False)
     updated = update_qs.update(is_new=False)
 
 
     if updated:
     if updated:

+ 3 - 3
misago/notifications/tests/test_api.py

@@ -26,8 +26,8 @@ class NotificationsAPITests(TestCase):
         self.reload_test_user()
         self.reload_test_user()
         self.assertEqual(self.test_user.new_notifications, 1)
         self.assertEqual(self.test_user.new_notifications, 1)
 
 
-    def test_read_user_notification(self):
-        """read_user_notification reads user notification"""
+    def test_read_user_notifications(self):
+        """read_user_notifications reads user notification"""
         api.notify_user(self.test_user,
         api.notify_user(self.test_user,
                         "Test notify %(token)s",
                         "Test notify %(token)s",
                         "/users/",
                         "/users/",
@@ -36,7 +36,7 @@ class NotificationsAPITests(TestCase):
                         self.test_user)
                         self.test_user)
         self.reload_test_user()
         self.reload_test_user()
 
 
-        api.read_user_notification(self.test_user, "test")
+        api.read_user_notifications(self.test_user, "test")
 
 
         self.assertEqual(self.test_user.new_notifications, 0)
         self.assertEqual(self.test_user.new_notifications, 0)
         queryset = self.test_user.misago_notifications.filter(is_new=True)
         queryset = self.test_user.misago_notifications.filter(is_new=True)

+ 3 - 3
misago/users/views/profile.py

@@ -11,7 +11,7 @@ from misago.acl import add_acl
 from misago.core.decorators import require_POST
 from misago.core.decorators import require_POST
 from misago.core.shortcuts import get_object_or_404, paginate, validate_slug
 from misago.core.shortcuts import get_object_or_404, paginate, validate_slug
 from misago.core.utils import clean_return_path
 from misago.core.utils import clean_return_path
-from misago.notifications import notify_user, read_user_notification
+from misago.notifications import notify_user, read_user_notifications
 from misago.threads.permissions import allow_message_user
 from misago.threads.permissions import allow_message_user
 
 
 from misago.users.bans import get_user_ban
 from misago.users.bans import get_user_ban
@@ -48,7 +48,7 @@ def profile_view(f):
             profile.is_blocked = False
             profile.is_blocked = False
 
 
         if request.user.is_authenticated and request.method == "GET":
         if request.user.is_authenticated and request.method == "GET":
-            read_user_notification(request.user, "profile_%s" % profile.pk)
+            read_user_notifications(request.user, "profile_%s" % profile.pk)
 
 
         return f(request, *args, **kwargs)
         return f(request, *args, **kwargs)
     return decorator
     return decorator
@@ -75,7 +75,7 @@ def notification_view(trigger):
             profile = kwargs.get('profile')
             profile = kwargs.get('profile')
 
 
             if user.is_authenticated and request.method == "GET":
             if user.is_authenticated and request.method == "GET":
-                read_user_notification(user, trigger % profile.pk)
+                read_user_notifications(user, trigger % profile.pk)
 
 
             return f(*args, **kwargs)
             return f(*args, **kwargs)
         return decorator
         return decorator

+ 5 - 5
misago/users/views/usercp.py

@@ -13,7 +13,7 @@ from misago.core.decorators import ajax_only, require_POST
 from misago.core.exceptions import AjaxError
 from misago.core.exceptions import AjaxError
 from misago.core.mail import mail_user
 from misago.core.mail import mail_user
 from misago.markup import Editor
 from misago.markup import Editor
-from misago.notifications import read_user_notification
+from misago.notifications import read_user_notifications
 
 
 from misago.users import avatars
 from misago.users import avatars
 from misago.users.decorators import deny_guests
 from misago.users.decorators import deny_guests
@@ -90,8 +90,8 @@ def change_avatar(request):
 def avatar_not_locked(f):
 def avatar_not_locked(f):
     def decorator(request, *args, **kwargs):
     def decorator(request, *args, **kwargs):
         if request.method == "GET":
         if request.method == "GET":
-            read_user_notification(request.user,
-                                   'usercp_avatar_%s' % request.user.pk)
+            read_user_notifications(request.user,
+                                    'usercp_avatar_%s' % request.user.pk)
         if request.user.is_avatar_locked:
         if request.user.is_avatar_locked:
             message = _("Your avatar is locked and can't be changed.")
             message = _("Your avatar is locked and can't be changed.")
             messages.info(request, message)
             messages.info(request, message)
@@ -223,8 +223,8 @@ def edit_signature(request):
         raise Http404()
         raise Http404()
 
 
     if request.method == "GET":
     if request.method == "GET":
-        read_user_notification(request.user,
-                               'usercp_signature_%s' % request.user.pk)
+        read_user_notifications(request.user,
+                                'usercp_signature_%s' % request.user.pk)
 
 
     form = EditSignatureForm(instance=request.user)
     form = EditSignatureForm(instance=request.user)
     if not request.user.is_signature_locked and request.method == 'POST':
     if not request.user.is_signature_locked and request.method == 'POST':