diff --git a/seahub/api2/endpoints/notifications.py b/seahub/api2/endpoints/notifications.py index 0cccd1cb5d..c44fa7c2b8 100644 --- a/seahub/api2/endpoints/notifications.py +++ b/seahub/api2/endpoints/notifications.py @@ -1,4 +1,6 @@ # Copyright (c) 2012-2016 Seafile Ltd. +import logging + from rest_framework.authentication import SessionAuthentication from rest_framework.permissions import IsAuthenticated from rest_framework.response import Response @@ -8,6 +10,7 @@ from seahub.api2.authentication import TokenAuthentication from seahub.api2.throttling import UserRateThrottle from seahub.notifications.models import UserNotification +logger = logging.getLogger(__name__) json_content_type = 'application/json; charset=utf-8' class NotificationsView(APIView): @@ -52,3 +55,30 @@ class NotificationsView(APIView): UserMessage.objects.update_unread_messages(msg_from, username) return Response({'success': True}) + +class NotificationView(APIView): + + authentication_classes = (TokenAuthentication, SessionAuthentication) + permission_classes = (IsAuthenticated,) + throttle_classes = (UserRateThrottle,) + + def put(self, request): + """ currently only used for mark a notification seen + + Permission checking: + 1. login user. + """ + + notice_id = request.data.get('notice_id') + + try: + notice = UserNotification.objects.get(id=notice_id) + except UserNotification.DoesNotExist as e: + logger.error(e) + pass + + if not notice.seen: + notice.seen = True + notice.save() + + return Response({'success': True}) diff --git a/seahub/urls.py b/seahub/urls.py index 1eda4f9ce9..b5df959441 100644 --- a/seahub/urls.py +++ b/seahub/urls.py @@ -34,7 +34,7 @@ from seahub.api2.endpoints.share_link_zip_task import ShareLinkZipTaskView from seahub.api2.endpoints.query_zip_progress import QueryZipProgressView from seahub.api2.endpoints.invitations import InvitationsView from seahub.api2.endpoints.invitation import InvitationView -from seahub.api2.endpoints.notifications import NotificationsView +from seahub.api2.endpoints.notifications import NotificationsView, NotificationView from seahub.api2.endpoints.admin.login import Login from seahub.api2.endpoints.admin.file_audit import FileAudit @@ -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_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'), url(r'^ajax/repo/(?P[-0-9a-f]{36})/get-folder-perm-by-path/$', get_folder_perm_by_path, name='get_folder_perm_by_path'), @@ -202,6 +201,7 @@ urlpatterns = patterns( 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/notification/$', NotificationView.as_view(), name='api-v2.1-notification'), 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'), diff --git a/seahub/views/ajax.py b/seahub/views/ajax.py index 145973af09..5f4d9ecaa3 100644 --- a/seahub/views/ajax.py +++ b/seahub/views/ajax.py @@ -1255,32 +1255,6 @@ def get_popup_notices(request): "notice_html": notice_html, }), content_type=content_type) -@login_required_ajax -@require_POST -def set_notice_seen_by_id(request): - """ - - Arguments: - - `request`: - """ - content_type = 'application/json; charset=utf-8' - notice_id = request.GET.get('notice_id') - - try: - notice = UserNotification.objects.get(id=notice_id) - except UserNotification.DoesNotExist as e: - logger.error(e) - return HttpResponse(json.dumps({ - 'error': _(u'Failed') - }), status=400, content_type=content_type) - - if not notice.seen: - notice.seen = True - notice.save() - - return HttpResponse(json.dumps({'success': True}), content_type=content_type) - - @login_required_ajax def space_and_traffic(request): content_type = 'application/json; charset=utf-8' diff --git a/static/scripts/app/views/notifications.js b/static/scripts/app/views/notifications.js index 486f3e3002..7b2322893d 100644 --- a/static/scripts/app/views/notifications.js +++ b/static/scripts/app/views/notifications.js @@ -100,9 +100,10 @@ define([ var notice_id = $el.closest('.unread').data('id'); var link_href = $el.attr('href'); $.ajax({ - url: Common.getUrl({name: 'set_notice_seen_by_id'}) + '?notice_id=' + encodeURIComponent(notice_id), - type: 'POST', + url: Common.getUrl({name: 'notification'}), + type: 'PUT', dataType: 'json', + data:{'notice_id': encodeURIComponent(notice_id)}, beforeSend: Common.prepareCSRFToken, success: function(data) { location.href = link_href; diff --git a/static/scripts/common.js b/static/scripts/common.js index 1d90679254..e8bbd66a27 100644 --- a/static/scripts/common.js +++ b/static/scripts/common.js @@ -159,7 +159,7 @@ define([ case 'get_popup_notices': return siteRoot + 'ajax/get_popup_notices/'; case 'notifications': return siteRoot + 'api/v2.1/notifications/'; - case 'set_notice_seen_by_id': return siteRoot + 'ajax/set_notice_seen_by_id/'; + case 'notification': return siteRoot + 'api/v2.1/notification/'; case 'toggle_personal_modules': return siteRoot + 'ajax/toggle-personal-modules/'; case 'starred_files': return siteRoot + 'api2/starredfiles/'; diff --git a/tests/api/endpoints/test_notifications.py b/tests/api/endpoints/test_notifications.py index 6f0249ec9d..0433cf1d83 100644 --- a/tests/api/endpoints/test_notifications.py +++ b/tests/api/endpoints/test_notifications.py @@ -2,7 +2,7 @@ import json from seahub.test_utils import BaseTestCase from seahub.notifications.models import UserNotification -class InvitationsTest(BaseTestCase): +class NotificationsTest(BaseTestCase): def setUp(self): self.endpoint = '/api/v2.1/notifications/' self.username = self.user.username @@ -42,3 +42,19 @@ class InvitationsTest(BaseTestCase): resp = self.client.put(self.endpoint, {}, 'application/x-www-form-urlencoded') self.assertEqual(403, resp.status_code) +class NotificationTest(BaseTestCase): + def setUp(self): + self.endpoint = '/api/v2.1/notification/' + self.username = self.user.username + + def test_can_unseen_notification_by_id(self): + + notice = UserNotification.objects.add_file_uploaded_msg(self.username, 'test') + assert UserNotification.objects.count_unseen_user_notifications(self.username) == 1 + + self.login_as(self.user) + data = 'notice_id=%d' % notice.id + resp = self.client.put(self.endpoint, data, 'application/x-www-form-urlencoded') + self.assertEqual(200, resp.status_code) + + assert UserNotification.objects.count_unseen_user_notifications(self.username) == 0