summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Kislitsky <akislitsky@mirantis.com>2017-01-31 13:08:55 +0300
committerAlexander Kislitsky <akislitsky@mirantis.com>2017-02-07 16:39:05 +0000
commit71fb921dbe2a2cc845a4b48f96fb45dcaae6b4fb (patch)
treeb4a38b4ab7418802cb484af78b7fb3c4fe6ac844
parent5eb41d7bc18b4ac06fdff7099d338736a610abf2 (diff)
Handler for changing notifications statuses added
As workaround for decreasing loading in the UI on unread notifications fetching we can mark all notifications as read. For such purposes we add NotificationsMarkAllHandler Change-Id: I2e6a0daaf8712ab3064df728a8fb463ef805aa06 Partial-Bug: #1657348
Notes
Notes (review): Code-Review+1: Georgy Kibardin <gkibardin@mirantis.com> Verified+1: Fuel CI <fuel-ci-bot@mirantis.com> Code-Review+1: Julia Aranovich <jkirnosova@mirantis.com> Code-Review+2: Aleksey Kasatkin <akasatkin@mirantis.com> Workflow+1: Ihor Kalnytskyi <ikalnitsky@mirantis.com> Verified+2: Jenkins Submitted-by: Jenkins Submitted-at: Wed, 08 Feb 2017 14:32:33 +0000 Reviewed-on: https://review.openstack.org/427085 Project: openstack/fuel-web Branch: refs/heads/master
-rw-r--r--nailgun/nailgun/api/v1/handlers/notifications.py26
-rw-r--r--nailgun/nailgun/api/v1/urls.py5
-rw-r--r--nailgun/nailgun/api/v1/validators/json_schema/notification.py29
-rw-r--r--nailgun/nailgun/api/v1/validators/notification.py12
-rw-r--r--nailgun/nailgun/objects/notification.py5
-rw-r--r--nailgun/nailgun/test/unit/test_notification_handler.py37
6 files changed, 107 insertions, 7 deletions
diff --git a/nailgun/nailgun/api/v1/handlers/notifications.py b/nailgun/nailgun/api/v1/handlers/notifications.py
index c516b4e..e12b621 100644
--- a/nailgun/nailgun/api/v1/handlers/notifications.py
+++ b/nailgun/nailgun/api/v1/handlers/notifications.py
@@ -19,15 +19,14 @@ Handlers dealing with notifications
19""" 19"""
20import web 20import web
21 21
22from nailgun.api.v1.handlers.base import BaseHandler
22from nailgun.api.v1.handlers.base import CollectionHandler 23from nailgun.api.v1.handlers.base import CollectionHandler
23from nailgun.api.v1.handlers.base import SingleHandler
24
25from nailgun import objects
26
27from nailgun.api.v1.handlers.base import handle_errors 24from nailgun.api.v1.handlers.base import handle_errors
28from nailgun.api.v1.handlers.base import serialize 25from nailgun.api.v1.handlers.base import serialize
26from nailgun.api.v1.handlers.base import SingleHandler
29from nailgun.api.v1.handlers.base import validate 27from nailgun.api.v1.handlers.base import validate
30from nailgun.api.v1.validators.notification import NotificationValidator 28from nailgun.api.v1.validators.notification import NotificationValidator
29from nailgun import objects
31 30
32 31
33class NotificationHandler(SingleHandler): 32class NotificationHandler(SingleHandler):
@@ -89,3 +88,22 @@ class NotificationCollectionStatsHandler(CollectionHandler):
89 :http: * 405 (Method not allowed) 88 :http: * 405 (Method not allowed)
90 """ 89 """
91 raise self.http(405) 90 raise self.http(405)
91
92
93class NotificationStatusHandler(BaseHandler):
94
95 validator = NotificationValidator
96
97 @handle_errors
98 @validate
99 @serialize
100 def PUT(self):
101 """Updates status of all notifications
102
103 :http: * 200 (OK)
104 * 400 (Invalid data)
105 """
106 web_data = web.data()
107 data = self.validator.validate_change_status(web_data)
108 status = data['status']
109 objects.NotificationCollection.update_statuses(status)
diff --git a/nailgun/nailgun/api/v1/urls.py b/nailgun/nailgun/api/v1/urls.py
index 78dbeba..ee472ac 100644
--- a/nailgun/nailgun/api/v1/urls.py
+++ b/nailgun/nailgun/api/v1/urls.py
@@ -88,6 +88,9 @@ from nailgun.api.v1.handlers.plugin_link import PluginLinkHandler
88from nailgun.api.v1.handlers.notifications import NotificationCollectionHandler 88from nailgun.api.v1.handlers.notifications import NotificationCollectionHandler
89from nailgun.api.v1.handlers.notifications import \ 89from nailgun.api.v1.handlers.notifications import \
90 NotificationCollectionStatsHandler 90 NotificationCollectionStatsHandler
91from nailgun.api.v1.handlers.notifications import \
92 NotificationStatusHandler
93
91from nailgun.api.v1.handlers.notifications import NotificationHandler 94from nailgun.api.v1.handlers.notifications import NotificationHandler
92 95
93from nailgun.api.v1.handlers.orchestrator import DefaultDeploymentInfo 96from nailgun.api.v1.handlers.orchestrator import DefaultDeploymentInfo
@@ -336,6 +339,8 @@ urls = (
336 339
337 r'/notifications/?$', 340 r'/notifications/?$',
338 NotificationCollectionHandler, 341 NotificationCollectionHandler,
342 r'/notifications/change_status/?$',
343 NotificationStatusHandler,
339 r'/notifications/(?P<obj_id>\d+)/?$', 344 r'/notifications/(?P<obj_id>\d+)/?$',
340 NotificationHandler, 345 NotificationHandler,
341 r'/notifications/stats/?$', 346 r'/notifications/stats/?$',
diff --git a/nailgun/nailgun/api/v1/validators/json_schema/notification.py b/nailgun/nailgun/api/v1/validators/json_schema/notification.py
new file mode 100644
index 0000000..84f382b
--- /dev/null
+++ b/nailgun/nailgun/api/v1/validators/json_schema/notification.py
@@ -0,0 +1,29 @@
1# Copyright 2017 Mirantis, Inc.
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may
4# not use this file except in compliance with the License. You may obtain
5# a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations
13# under the License.
14
15from nailgun import consts
16
17
18NOTIFICATIONS_CHANGE_STATUS = {
19 "$schema": "http://json-schema.org/draft-04/schema#",
20 "type": "object",
21 "required": ["status"],
22 "properties": {
23 "status": {
24 "type": "string",
25 "enum": list(consts.NOTIFICATION_STATUSES)
26 },
27
28 }
29}
diff --git a/nailgun/nailgun/api/v1/validators/notification.py b/nailgun/nailgun/api/v1/validators/notification.py
index 36ca7db..d40594e 100644
--- a/nailgun/nailgun/api/v1/validators/notification.py
+++ b/nailgun/nailgun/api/v1/validators/notification.py
@@ -13,11 +13,11 @@
13# License for the specific language governing permissions and limitations 13# License for the specific language governing permissions and limitations
14# under the License. 14# under the License.
15 15
16from nailgun import consts
17from nailgun import objects
18
19from nailgun.api.v1.validators.base import BasicValidator 16from nailgun.api.v1.validators.base import BasicValidator
17from nailgun.api.v1.validators.json_schema import notification
18from nailgun import consts
20from nailgun import errors 19from nailgun import errors
20from nailgun import objects
21 21
22 22
23class NotificationValidator(BasicValidator): 23class NotificationValidator(BasicValidator):
@@ -90,3 +90,9 @@ class NotificationValidator(BasicValidator):
90 without any validations. 90 without any validations.
91 """ 91 """
92 pass 92 pass
93
94 @classmethod
95 def validate_change_status(cls, data):
96 parsed = super(NotificationValidator, cls).validate(data)
97 cls.validate_schema(parsed, notification.NOTIFICATIONS_CHANGE_STATUS)
98 return parsed
diff --git a/nailgun/nailgun/objects/notification.py b/nailgun/nailgun/objects/notification.py
index e7d2bb8..ec58405 100644
--- a/nailgun/nailgun/objects/notification.py
+++ b/nailgun/nailgun/objects/notification.py
@@ -109,3 +109,8 @@ class Notification(NailgunObject):
109class NotificationCollection(NailgunCollection): 109class NotificationCollection(NailgunCollection):
110 110
111 single = Notification 111 single = Notification
112
113 @classmethod
114 def update_statuses(cls, status):
115 db().query(cls.single.model).update(
116 {cls.single.model.status: status}, synchronize_session=False)
diff --git a/nailgun/nailgun/test/unit/test_notification_handler.py b/nailgun/nailgun/test/unit/test_notification_handler.py
index 69fd57e..fa4b277 100644
--- a/nailgun/nailgun/test/unit/test_notification_handler.py
+++ b/nailgun/nailgun/test/unit/test_notification_handler.py
@@ -16,6 +16,7 @@
16 16
17from oslo_serialization import jsonutils 17from oslo_serialization import jsonutils
18 18
19from nailgun import consts
19from nailgun.test.base import BaseIntegrationTest 20from nailgun.test.base import BaseIntegrationTest
20from nailgun.utils import reverse 21from nailgun.utils import reverse
21 22
@@ -140,3 +141,39 @@ class TestHandlers(BaseIntegrationTest):
140 expect_errors=True 141 expect_errors=True
141 ) 142 )
142 self.assertEqual(405, resp.status_code) 143 self.assertEqual(405, resp.status_code)
144
145 def test_notification_status(self):
146 self.env.create_notification(status=consts.NOTIFICATION_STATUSES.read)
147 self.env.create_notification(
148 status=consts.NOTIFICATION_STATUSES.unread)
149 self.env.create_notification(
150 status=consts.NOTIFICATION_STATUSES.unread)
151
152 expected_status = consts.NOTIFICATION_STATUSES.unread
153 resp = self.app.put(
154 reverse('NotificationStatusHandler'),
155 params=jsonutils.dumps({'status': expected_status}),
156 headers=self.default_headers
157 )
158 self.assertEqual(200, resp.status_code)
159
160 # Checking statuses are changed
161 resp = self.app.get(
162 reverse('NotificationCollectionHandler'),
163 headers=self.default_headers
164 )
165 self.assertEqual(200, resp.status_code)
166 for notif in resp.json_body:
167 self.assertEqual(expected_status, notif['status'])
168
169 def test_notification_status_not_allowed_methods(self):
170 methods = ('get', 'post', 'delete', 'patch', 'head')
171 url = reverse('NotificationStatusHandler')
172 for m in methods:
173 method = getattr(self.app, m)
174 resp = method(
175 url,
176 headers=self.default_headers,
177 expect_errors=True
178 )
179 self.assertEqual(405, resp.status_code)