-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnotifications.py
138 lines (102 loc) · 4.33 KB
/
notifications.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
from opengever.activity import notification_center
from plone import api
from plone.restapi.batching import HypermediaBatch
from plone.restapi.deserializer import json_body
from plone.restapi.services import Service
from zExceptions import BadRequest
from zExceptions import NotFound
from zExceptions import Unauthorized
from zope.interface import implements
from zope.publisher.interfaces import IPublishTraverse
class NotificationsGet(Service):
"""API endpoint to get all notifications for a particular user.
GET /@notifications/peter.mueller HTTP/1.1
"""
implements(IPublishTraverse)
def __init__(self, context, request):
super(NotificationsGet, self).__init__(context, request)
self.params = []
self.center = notification_center()
def publishTraverse(self, request, name):
# Consume any path segments after /@notifications as parameters
self.params.append(name)
return self
def reply(self):
userid, notification_id = self.read_params()
if userid != api.user.get_current().getId():
raise Unauthorized(
"It's not allowed to access notifications of other users.")
if notification_id:
return self.get_notification(userid, notification_id)
return self.get_user_notifications()
def get_notification(self, userid, notification_id):
notification = self.center.get_notification(notification_id)
if not notification:
raise NotFound
if not notification.userid == userid:
raise Unauthorized(
"It's not allowed to access notifications of other users.")
return self.serialize(notification)
def get_user_notifications(self):
notifications = self.center.get_current_users_notifications()
batch = HypermediaBatch(self.request, notifications)
result = {
'@id': batch.canonical_url,
'items_total': batch.items_total,
'items': self.serialize(list(batch)),
}
if batch.links:
result['batching'] = batch.links
return result
def serialize(self, notifications):
url = api.portal.get().absolute_url()
if isinstance(notifications, list):
return [n.serialize(url) for n in notifications]
else:
return notifications.serialize(url)
def read_params(self):
if len(self.params) not in [1, 2]:
raise BadRequest("Must supply user ID as URL and optional "
"the notification id as path parameter.")
if len(self.params) == 2:
return self.params
return self.params[0], None
class NotificationPatch(Service):
"""API endpoint to set a specific notification for a particular user as read.
PATCH /@notifications/peter.mueller/3617 HTTP/1.1
Payload: {
"read": true
}
"""
implements(IPublishTraverse)
def __init__(self, context, request):
super(NotificationPatch, self).__init__(context, request)
self.params = []
self.center = notification_center()
def publishTraverse(self, request, name):
# Consume any path segments after /@notifications as parameters
self.params.append(name)
return self
def reply(self):
userid, notification_id = self.read_params()
mark_as_read = json_body(self.request).get('read', False)
if userid != api.user.get_current().getId():
raise Unauthorized(
"It's not allowed to access notifications of other users.")
notification = self.get_notification(userid, notification_id)
if mark_as_read:
self.center.mark_notification_as_read(notification.notification_id)
self.request.response.setStatus(204)
def get_notification(self, userid, notification_id):
notification = self.center.get_notification(notification_id)
if not notification:
raise NotFound
if not notification.userid == userid:
raise Unauthorized(
"It's not allowed to access notifications of other users.")
return notification
def read_params(self):
if len(self.params) != 2:
raise BadRequest("Must supply user ID and the notification id "
"as path parameter.")
return self.params