From 5a700d5f006cfcda932d0a17d14286ebdbecda00 Mon Sep 17 00:00:00 2001 From: lian Date: Fri, 19 Aug 2016 15:00:10 +0800 Subject: [PATCH] update set_notices_seen --- seahub/api2/endpoints/notifications.py | 28 +++++++++++++++++++ .../notifications/user_notification_list.html | 4 +-- seahub/urls.py | 9 +++--- seahub/views/ajax.py | 25 ----------------- static/scripts/app/views/notifications.js | 4 +-- static/scripts/common.js | 3 +- tests/api/endpoints/test_notifications.py | 24 ++++++++++++++-- 7 files changed, 60 insertions(+), 37 deletions(-) diff --git a/seahub/api2/endpoints/notifications.py b/seahub/api2/endpoints/notifications.py index 0b54037a65..0cccd1cb5d 100644 --- a/seahub/api2/endpoints/notifications.py +++ b/seahub/api2/endpoints/notifications.py @@ -17,6 +17,11 @@ class NotificationsView(APIView): throttle_classes = (UserRateThrottle,) def get(self, request): + """ currently only used for get unseen notifications count + + Permission checking: + 1. login user. + """ username = request.user.username unseen_count = UserNotification.objects.count_unseen_user_notifications(username) @@ -24,3 +29,26 @@ class NotificationsView(APIView): result['unseen_count'] = unseen_count return Response(result) + + def put(self, request): + """ currently only used for mark all notifications seen + + Permission checking: + 1. login user. + """ + + username = request.user.username + unseen_notices = UserNotification.objects.get_user_notifications(username, + seen=False) + for notice in unseen_notices: + notice.seen = True + notice.save() + + # TODO mark related user msg as read + if notice.is_user_message(): + d = notice.user_message_detail_to_dict() + msg_from = d.get('msg_from') + from seahub.message.models import UserMessage + UserMessage.objects.update_unread_messages(msg_from, username) + + return Response({'success': True}) diff --git a/seahub/notifications/templates/notifications/user_notification_list.html b/seahub/notifications/templates/notifications/user_notification_list.html index fad2cc0314..fe5bebc50e 100644 --- a/seahub/notifications/templates/notifications/user_notification_list.html +++ b/seahub/notifications/templates/notifications/user_notification_list.html @@ -43,8 +43,8 @@ $('#mark-all-read').click(function() { var unread_items = $('#notices-table .unread'); if (unread_items.length > 0) { $.ajax({ - url: '{% url 'set_notices_seen' %}', - type: 'POST', + url: "{% url 'api-v2.1-notifications' %}", + type: 'PUT', dataType: 'json', beforeSend: prepareCSRFToken, success: function() { diff --git a/seahub/urls.py b/seahub/urls.py index 3b0315e78f..1eda4f9ce9 100644 --- a/seahub/urls.py +++ b/seahub/urls.py @@ -162,7 +162,6 @@ urlpatterns = patterns( url(r'^ajax/unenc-rw-repos/$', unenc_rw_repos, name='unenc_rw_repos'), url(r'^ajax/upload-file-done/$', upload_file_done, name='upload_file_done'), url(r'^ajax/get_popup_notices/$', get_popup_notices, name='get_popup_notices'), - url(r'^ajax/set_notices_seen/$', set_notices_seen, name='set_notices_seen'), url(r'^ajax/set_notice_seen_by_id/$', set_notice_seen_by_id, name='set_notice_seen_by_id'), url(r'^ajax/space_and_traffic/$', space_and_traffic, name='space_and_traffic'), url(r'^ajax/repo/(?P[-0-9a-f]{36})/setting/change-passwd/$', ajax_repo_change_passwd, name='ajax_repo_change_passwd'), @@ -200,13 +199,13 @@ urlpatterns = patterns( url(r'^api/v2.1/query-zip-progress/$', QueryZipProgressView.as_view(), name='api-v2.1-query-zip-progress'), url(r'^api/v2.1/repos/(?P[-0-9a-f]{36})/dir/$', DirView.as_view(), name='api-v2.1-dir-view'), url(r'^api/v2.1/repos/(?P[-0-9a-f]{36})/set-password/$', RepoSetPassword.as_view(), name="api-v2.1-repo-set-password"), + url(r'^api/v2.1/invitations/$', InvitationsView.as_view()), + url(r'^api/v2.1/invitations/(?P[a-f0-9]{32})/$', InvitationView.as_view()), + url(r'^api/v2.1/notifications/$', NotificationsView.as_view(), name='api-v2.1-notifications'), + url(r'^api/v2.1/admin/sysinfo/$', SysInfo.as_view(), name='api-v2.1-sysinfo'), url(r'^api/v2.1/admin/devices/$', AdminDevices.as_view(), name='api-v2.1-admin-devices'), url(r'^api/v2.1/admin/device-errors/$', AdminDeviceErrors.as_view(), name='api-v2.1-admin-device-errors'), - url(r'^api/v2.1/invitations/$', InvitationsView.as_view()), - url(r'^api/v2.1/invitations/(?P[a-f0-9]{32})/$', InvitationView.as_view()), - url(r'^api/v2.1/notifications/$', NotificationsView.as_view()), - url(r'^api/v2.1/admin/libraries/$', AdminLibraries.as_view(), name='api-v2.1-admin-libraries'), url(r'^api/v2.1/admin/libraries/(?P[-0-9a-f]{36})/$', AdminLibrary.as_view(), name='api-v2.1-admin-library'), url(r'^api/v2.1/admin/libraries/(?P[-0-9a-f]{36})/dirents/$', AdminLibraryDirents.as_view(), name='api-v2.1-admin-library-dirents'), diff --git a/seahub/views/ajax.py b/seahub/views/ajax.py index 37492c5541..145973af09 100644 --- a/seahub/views/ajax.py +++ b/seahub/views/ajax.py @@ -1255,31 +1255,6 @@ def get_popup_notices(request): "notice_html": notice_html, }), content_type=content_type) -@login_required_ajax -@require_POST -def set_notices_seen(request): - """Set user's notices seen: - - Arguments: - - `request`: - """ - content_type = 'application/json; charset=utf-8' - username = request.user.username - - unseen_notices = UserNotification.objects.get_user_notifications(username, - seen=False) - for notice in unseen_notices: - notice.seen = True - notice.save() - - # mark related user msg as read - if notice.is_user_message(): - d = notice.user_message_detail_to_dict() - msg_from = d.get('msg_from') - UserMessage.objects.update_unread_messages(msg_from, username) - - return HttpResponse(json.dumps({'success': True}), content_type=content_type) - @login_required_ajax @require_POST def set_notice_seen_by_id(request): diff --git a/static/scripts/app/views/notifications.js b/static/scripts/app/views/notifications.js index 4f47a19d7b..486f3e3002 100644 --- a/static/scripts/app/views/notifications.js +++ b/static/scripts/app/views/notifications.js @@ -123,8 +123,8 @@ define([ if (this.$(".unread").length > 0) { // set all unread notice to be read $.ajax({ - url: Common.getUrl({name: 'set_notices_seen'}), - type: 'POST', + url: Common.getUrl({name: 'notifications'}), + type: 'PUT', dataType: 'json', beforeSend: Common.prepareCSRFToken, success: function() { diff --git a/static/scripts/common.js b/static/scripts/common.js index ee822f4b91..1d90679254 100644 --- a/static/scripts/common.js +++ b/static/scripts/common.js @@ -157,9 +157,10 @@ define([ // Misc case 'thumbnail_create': return siteRoot + 'thumbnail/' + options.repo_id + '/create/'; case 'get_popup_notices': return siteRoot + 'ajax/get_popup_notices/'; - case 'set_notices_seen': return siteRoot + 'ajax/set_notices_seen/'; + case 'notifications': return siteRoot + 'api/v2.1/notifications/'; case 'set_notice_seen_by_id': return siteRoot + 'ajax/set_notice_seen_by_id/'; + case 'toggle_personal_modules': return siteRoot + 'ajax/toggle-personal-modules/'; case 'starred_files': return siteRoot + 'api2/starredfiles/'; case 'events': return siteRoot + 'api2/events/'; diff --git a/tests/api/endpoints/test_notifications.py b/tests/api/endpoints/test_notifications.py index 46f0d081c8..6f0249ec9d 100644 --- a/tests/api/endpoints/test_notifications.py +++ b/tests/api/endpoints/test_notifications.py @@ -9,9 +9,9 @@ class InvitationsTest(BaseTestCase): def test_can_get_unseen_count(self): - self.login_as(self.user) - UserNotification.objects.add_file_uploaded_msg(self.username, 'test') + + self.login_as(self.user) resp = self.client.get(self.endpoint) self.assertEqual(200, resp.status_code) @@ -22,3 +22,23 @@ class InvitationsTest(BaseTestCase): resp = self.client.get(self.endpoint) self.assertEqual(403, resp.status_code) + + def test_can_unseen_all_notifications(self): + + UserNotification.objects.add_file_uploaded_msg(self.username, 'test') + assert UserNotification.objects.count_unseen_user_notifications(self.username) == 1 + + self.login_as(self.user) + resp = self.client.put(self.endpoint, {}, 'application/x-www-form-urlencoded') + self.assertEqual(200, resp.status_code) + + assert UserNotification.objects.count_unseen_user_notifications(self.username) == 0 + + def test_unseen_notifications_with_invalid_user_permission(self): + + UserNotification.objects.add_file_uploaded_msg(self.username, 'test') + assert UserNotification.objects.count_unseen_user_notifications(self.username) == 1 + + resp = self.client.put(self.endpoint, {}, 'application/x-www-form-urlencoded') + self.assertEqual(403, resp.status_code) +