diff --git a/README.rst b/README.rst index 7847f3f..fd09cc7 100644 --- a/README.rst +++ b/README.rst @@ -239,18 +239,25 @@ for updating specific fields within a django template. There are two possible API calls that can be made: - 1. ``api/unread_count/`` that returns a javascript object with 1 key: ``unread_count`` eg:: +1. ``api/unread_count/`` that returns a javascript object with 1 key: ``unread_count`` eg:: {"unread_count":1} - #. ``api/unread_list/`` that returns a javascript object with 2 keys: `unread_count` and `unread_list` eg:: +#. ``api/unread_list/`` that returns a javascript object with 2 keys: `unread_count` and `unread_list` eg:: - { - "unread_count":1, - "unread_list":[--list of json representations of notifications--] - } + { + "unread_count":1, + "unread_list":[--list of json representations of notifications--] + } - Representations of notifications are based on the django method: ``model_to_dict`` + Representations of notifications are based on the django method: ``model_to_dict`` + + Query string arguments: + + - **max** - maximum length of unread list. + - **mark_as_read** - mark notification in list as read. + + For example, get ``api/unread_list/?max=3&mark_as_read=true`` returns 3 notifications and mark them read (remove from list on next request). How to use: diff --git a/notifications/tests/tests.py b/notifications/tests/tests.py index 336bc5a..6b57db6 100644 --- a/notifications/tests/tests.py +++ b/notifications/tests/tests.py @@ -295,6 +295,26 @@ class NotificationTestPages(TestCase): self.assertEqual(len(data['unread_list']), 1) self.assertEqual(data['unread_list'][0]['verb'], 'commented') + def test_unread_list_api_mark_as_read(self): + self.login('to', 'pwd') + num_requested = 3 + response = self.client.get( + reverse('notifications:live_unread_notification_list'), + data={"max": num_requested, "mark_as_read": 1} + ) + data = json.loads(response.content.decode('utf-8')) + self.assertEqual(data['unread_count'], + self.message_count - num_requested) + self.assertEqual(len(data['unread_list']), num_requested) + response = self.client.get( + reverse('notifications:live_unread_notification_list'), + data={"max": num_requested, "mark_as_read": 1} + ) + data = json.loads(response.content.decode('utf-8')) + self.assertEqual(data['unread_count'], + self.message_count - 2*num_requested) + self.assertEqual(len(data['unread_list']), num_requested) + def test_live_update_tags(self): from django.shortcuts import render diff --git a/notifications/views.py b/notifications/views.py index a7dcef9..bbc545a 100644 --- a/notifications/views.py +++ b/notifications/views.py @@ -156,6 +156,8 @@ def live_unread_notification_list(request): if n.action_object: struct['action_object'] = str(n.action_object) unread_list.append(struct) + if request.GET.get('mark_as_read'): + n.mark_as_read() data = { 'unread_count': request.user.notifications.unread().count(), 'unread_list': unread_list