mirror of
https://github.com/haiwen/seahub.git
synced 2025-09-01 07:01:12 +00:00
update get unseen notifications count
This commit is contained in:
26
seahub/api2/endpoints/notifications.py
Normal file
26
seahub/api2/endpoints/notifications.py
Normal file
@@ -0,0 +1,26 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
from rest_framework.authentication import SessionAuthentication
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.views import APIView
|
||||
|
||||
from seahub.api2.authentication import TokenAuthentication
|
||||
from seahub.api2.throttling import UserRateThrottle
|
||||
from seahub.notifications.models import UserNotification
|
||||
|
||||
json_content_type = 'application/json; charset=utf-8'
|
||||
|
||||
class NotificationsView(APIView):
|
||||
|
||||
authentication_classes = (TokenAuthentication, SessionAuthentication)
|
||||
permission_classes = (IsAuthenticated,)
|
||||
throttle_classes = (UserRateThrottle,)
|
||||
|
||||
def get(self, request):
|
||||
|
||||
username = request.user.username
|
||||
unseen_count = UserNotification.objects.count_unseen_user_notifications(username)
|
||||
result = {}
|
||||
result['unseen_count'] = unseen_count
|
||||
|
||||
return Response(result)
|
@@ -34,6 +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.admin.login import Login
|
||||
from seahub.api2.endpoints.admin.file_audit import FileAudit
|
||||
@@ -161,7 +162,6 @@ urlpatterns = patterns(
|
||||
url(r'^ajax/unenc-rw-repos/$', unenc_rw_repos, name='unenc_rw_repos'),
|
||||
url(r'^ajax/contacts/$', get_contacts, name='get_contacts'),
|
||||
url(r'^ajax/upload-file-done/$', upload_file_done, name='upload_file_done'),
|
||||
url(r'^ajax/unseen-notices-count/$', unseen_notices_count, name='unseen_notices_count'),
|
||||
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'),
|
||||
@@ -206,6 +206,8 @@ urlpatterns = patterns(
|
||||
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<token>[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<repo_id>[-0-9a-f]{36})/$', AdminLibrary.as_view(), name='api-v2.1-admin-library'),
|
||||
url(r'^api/v2.1/admin/libraries/(?P<repo_id>[-0-9a-f]{36})/dirents/$', AdminLibraryDirents.as_view(), name='api-v2.1-admin-library-dirents'),
|
||||
|
@@ -1229,21 +1229,6 @@ def upload_file_done(request):
|
||||
|
||||
return HttpResponse(json.dumps({'success': True}), content_type=ct)
|
||||
|
||||
@login_required_ajax
|
||||
def unseen_notices_count(request):
|
||||
"""Count user's unseen notices.
|
||||
|
||||
Arguments:
|
||||
- `request`:
|
||||
"""
|
||||
content_type = 'application/json; charset=utf-8'
|
||||
username = request.user.username
|
||||
|
||||
count = UserNotification.objects.count_unseen_user_notifications(username)
|
||||
result = {}
|
||||
result['count'] = count
|
||||
return HttpResponse(json.dumps(result), content_type=content_type)
|
||||
|
||||
@login_required_ajax
|
||||
def get_popup_notices(request):
|
||||
"""Get user's notifications.
|
||||
|
@@ -30,11 +30,11 @@ define([
|
||||
|
||||
var reqUnreadNum = function() {
|
||||
$.ajax({
|
||||
url: Common.getUrl({name: 'get_unseen_notices_num'}),
|
||||
url: Common.getUrl({name: 'notifications'}),
|
||||
dataType: 'json',
|
||||
cache: false,
|
||||
success: function(data) {
|
||||
var count = data['count'],
|
||||
var count = data['unseen_count'],
|
||||
$num = _this.$num;
|
||||
$num.html(count);
|
||||
if (count > 0) {
|
||||
|
@@ -159,7 +159,7 @@ define([
|
||||
case 'get_user_contacts': return siteRoot + 'ajax/contacts/';
|
||||
case 'get_popup_notices': return siteRoot + 'ajax/get_popup_notices/';
|
||||
case 'set_notices_seen': return siteRoot + 'ajax/set_notices_seen/';
|
||||
case 'get_unseen_notices_num': return siteRoot + 'ajax/unseen-notices-count/';
|
||||
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/';
|
||||
|
24
tests/api/endpoints/test_notifications.py
Normal file
24
tests/api/endpoints/test_notifications.py
Normal file
@@ -0,0 +1,24 @@
|
||||
import json
|
||||
from seahub.test_utils import BaseTestCase
|
||||
from seahub.notifications.models import UserNotification
|
||||
|
||||
class InvitationsTest(BaseTestCase):
|
||||
def setUp(self):
|
||||
self.endpoint = '/api/v2.1/notifications/'
|
||||
self.username = self.user.username
|
||||
|
||||
def test_can_get_unseen_count(self):
|
||||
|
||||
self.login_as(self.user)
|
||||
|
||||
UserNotification.objects.add_file_uploaded_msg(self.username, 'test')
|
||||
resp = self.client.get(self.endpoint)
|
||||
self.assertEqual(200, resp.status_code)
|
||||
|
||||
json_resp = json.loads(resp.content)
|
||||
assert json_resp['unseen_count'] == 1
|
||||
|
||||
def test_get_with_invalid_user_permission(self):
|
||||
|
||||
resp = self.client.get(self.endpoint)
|
||||
self.assertEqual(403, resp.status_code)
|
Reference in New Issue
Block a user