1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-09-01 15:09:14 +00:00

update set_notice_seen_by_id

This commit is contained in:
lian
2016-08-19 15:17:46 +08:00
committed by lian
parent 5a700d5f00
commit 86f948aaa8
6 changed files with 53 additions and 32 deletions

View File

@@ -1,4 +1,6 @@
# Copyright (c) 2012-2016 Seafile Ltd. # Copyright (c) 2012-2016 Seafile Ltd.
import logging
from rest_framework.authentication import SessionAuthentication from rest_framework.authentication import SessionAuthentication
from rest_framework.permissions import IsAuthenticated from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response from rest_framework.response import Response
@@ -8,6 +10,7 @@ from seahub.api2.authentication import TokenAuthentication
from seahub.api2.throttling import UserRateThrottle from seahub.api2.throttling import UserRateThrottle
from seahub.notifications.models import UserNotification from seahub.notifications.models import UserNotification
logger = logging.getLogger(__name__)
json_content_type = 'application/json; charset=utf-8' json_content_type = 'application/json; charset=utf-8'
class NotificationsView(APIView): class NotificationsView(APIView):
@@ -52,3 +55,30 @@ class NotificationsView(APIView):
UserMessage.objects.update_unread_messages(msg_from, username) UserMessage.objects.update_unread_messages(msg_from, username)
return Response({'success': True}) 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})

View File

@@ -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.query_zip_progress import QueryZipProgressView
from seahub.api2.endpoints.invitations import InvitationsView from seahub.api2.endpoints.invitations import InvitationsView
from seahub.api2.endpoints.invitation import InvitationView 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.login import Login
from seahub.api2.endpoints.admin.file_audit import FileAudit 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/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/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/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/space_and_traffic/$', space_and_traffic, name='space_and_traffic'),
url(r'^ajax/repo/(?P<repo_id>[-0-9a-f]{36})/setting/change-passwd/$', ajax_repo_change_passwd, name='ajax_repo_change_passwd'), url(r'^ajax/repo/(?P<repo_id>[-0-9a-f]{36})/setting/change-passwd/$', ajax_repo_change_passwd, name='ajax_repo_change_passwd'),
url(r'^ajax/repo/(?P<repo_id>[-0-9a-f]{36})/get-folder-perm-by-path/$', get_folder_perm_by_path, name='get_folder_perm_by_path'), url(r'^ajax/repo/(?P<repo_id>[-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/$', InvitationsView.as_view()),
url(r'^api/v2.1/invitations/(?P<token>[a-f0-9]{32})/$', InvitationView.as_view()), url(r'^api/v2.1/invitations/(?P<token>[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/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/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/devices/$', AdminDevices.as_view(), name='api-v2.1-admin-devices'),

View File

@@ -1255,32 +1255,6 @@ def get_popup_notices(request):
"notice_html": notice_html, "notice_html": notice_html,
}), content_type=content_type) }), 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 @login_required_ajax
def space_and_traffic(request): def space_and_traffic(request):
content_type = 'application/json; charset=utf-8' content_type = 'application/json; charset=utf-8'

View File

@@ -100,9 +100,10 @@ define([
var notice_id = $el.closest('.unread').data('id'); var notice_id = $el.closest('.unread').data('id');
var link_href = $el.attr('href'); var link_href = $el.attr('href');
$.ajax({ $.ajax({
url: Common.getUrl({name: 'set_notice_seen_by_id'}) + '?notice_id=' + encodeURIComponent(notice_id), url: Common.getUrl({name: 'notification'}),
type: 'POST', type: 'PUT',
dataType: 'json', dataType: 'json',
data:{'notice_id': encodeURIComponent(notice_id)},
beforeSend: Common.prepareCSRFToken, beforeSend: Common.prepareCSRFToken,
success: function(data) { success: function(data) {
location.href = link_href; location.href = link_href;

View File

@@ -159,7 +159,7 @@ define([
case 'get_popup_notices': return siteRoot + 'ajax/get_popup_notices/'; case 'get_popup_notices': return siteRoot + 'ajax/get_popup_notices/';
case 'notifications': return siteRoot + 'api/v2.1/notifications/'; 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 'toggle_personal_modules': return siteRoot + 'ajax/toggle-personal-modules/';
case 'starred_files': return siteRoot + 'api2/starredfiles/'; case 'starred_files': return siteRoot + 'api2/starredfiles/';

View File

@@ -2,7 +2,7 @@ import json
from seahub.test_utils import BaseTestCase from seahub.test_utils import BaseTestCase
from seahub.notifications.models import UserNotification from seahub.notifications.models import UserNotification
class InvitationsTest(BaseTestCase): class NotificationsTest(BaseTestCase):
def setUp(self): def setUp(self):
self.endpoint = '/api/v2.1/notifications/' self.endpoint = '/api/v2.1/notifications/'
self.username = self.user.username self.username = self.user.username
@@ -42,3 +42,19 @@ class InvitationsTest(BaseTestCase):
resp = self.client.put(self.endpoint, {}, 'application/x-www-form-urlencoded') resp = self.client.put(self.endpoint, {}, 'application/x-www-form-urlencoded')
self.assertEqual(403, resp.status_code) 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