From 99dfa0a81873e9916e4e95c0d04d7a5376f8104b Mon Sep 17 00:00:00 2001 From: Vladimir Osintsev Date: Sun, 25 Sep 2016 00:22:33 +0300 Subject: [PATCH 1/4] Add mark_as_read param for API --- README.rst | 21 ++++++++++++++------- notifications/views.py | 2 ++ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/README.rst b/README.rst index 20cc675..7d66c7d 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** + - **mark_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 time). How to use: 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 From f2175fe77eab209d82bd6ccf912b611b3b195e41 Mon Sep 17 00:00:00 2001 From: Vladimir Osintsev Date: Sun, 25 Sep 2016 15:06:45 +0300 Subject: [PATCH 2/4] Testing mark_as_read query param --- notifications/tests/tests.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/notifications/tests/tests.py b/notifications/tests/tests.py index 336bc5a..32f4b98 100644 --- a/notifications/tests/tests.py +++ b/notifications/tests/tests.py @@ -295,6 +295,24 @@ 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'], 4) + self.assertEqual(len(data['unread_list']), num_requested) + def test_live_update_tags(self): from django.shortcuts import render From 1ab6fe2a0464073ae2cbd8f60121bd6eee29703c Mon Sep 17 00:00:00 2001 From: Vladimir Osintsev Date: Sun, 25 Sep 2016 15:11:08 +0300 Subject: [PATCH 3/4] Minor fixes to documentation --- README.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.rst b/README.rst index 7d66c7d..05425d1 100644 --- a/README.rst +++ b/README.rst @@ -254,10 +254,10 @@ There are two possible API calls that can be made: Query string arguments: - - **max** - - **mark_as_read** + - **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 time). + 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: From 3b61a71a712a4b077c3f083aed747a8e2e910f51 Mon Sep 17 00:00:00 2001 From: Vladimir Osintsev Date: Sun, 25 Sep 2016 15:43:56 +0300 Subject: [PATCH 4/4] Minor fixes --- notifications/tests/tests.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/notifications/tests/tests.py b/notifications/tests/tests.py index 32f4b98..6b57db6 100644 --- a/notifications/tests/tests.py +++ b/notifications/tests/tests.py @@ -298,19 +298,21 @@ class NotificationTestPages(TestCase): 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, - }) + 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(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, - }) + 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'], 4) + 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):