Skip to content

Commit

Permalink
Merge pull request #150 from osminogin/api-mark-as-read
Browse files Browse the repository at this point in the history
 Mark notifications as read for API requests
  • Loading branch information
zhang-z committed Sep 26, 2016
2 parents 3032626 + 3b61a71 commit dece4ad
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
21 changes: 14 additions & 7 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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``

Query string arguments:

- **max** - maximum length of unread list.
- **mark_as_read** - mark notification in list as read.

Representations of notifications are based on the django method: ``model_to_dict``
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:
Expand Down
20 changes: 20 additions & 0 deletions notifications/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,26 @@ def test_unread_list_api(self):
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

Expand Down
2 changes: 2 additions & 0 deletions notifications/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit dece4ad

Please sign in to comment.