allura-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hei...@apache.org
Subject [12/50] allura git commit: [#8044] ticket:889 Added rest api for get_site_notification
Date Wed, 27 Jan 2016 20:22:26 GMT
[#8044] ticket:889 Added rest api for get_site_notification

* Added RestController.notification, SiteNotification.__json__
* Changed ThemeProvider.get_site_notification
* Added new tests


Project: http://git-wip-us.apache.org/repos/asf/allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/allura/commit/e8e7b93a
Tree: http://git-wip-us.apache.org/repos/asf/allura/tree/e8e7b93a
Diff: http://git-wip-us.apache.org/repos/asf/allura/diff/e8e7b93a

Branch: refs/heads/hs/8035
Commit: e8e7b93ae4d9a11f4cfc652c9532a6609bf25019
Parents: 3ccbb5d
Author: Denis Kotov <deniskkotov@gmail.com>
Authored: Tue Jan 19 23:11:25 2016 +0200
Committer: Dave Brondsema <dave@brondsema.net>
Committed: Wed Jan 27 12:37:37 2016 -0500

----------------------------------------------------------------------
 Allura/allura/controllers/rest.py              | 10 +++++++
 Allura/allura/lib/plugin.py                    | 14 ++++++++--
 Allura/allura/model/__init__.py                |  4 +--
 Allura/allura/model/notification.py            | 11 ++++++++
 Allura/allura/tests/functional/test_rest.py    | 13 +++++++++
 Allura/allura/tests/model/test_notification.py | 31 +++++++++++++++++++++
 Allura/allura/tests/test_plugin.py             | 31 +++++++++++++++++++++
 7 files changed, 109 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/allura/blob/e8e7b93a/Allura/allura/controllers/rest.py
----------------------------------------------------------------------
diff --git a/Allura/allura/controllers/rest.py b/Allura/allura/controllers/rest.py
index a8a98fd..c091874 100644
--- a/Allura/allura/controllers/rest.py
+++ b/Allura/allura/controllers/rest.py
@@ -89,6 +89,16 @@ class RestController(object):
             summary['site_stats'] = stats
         return summary
 
+    @expose('json:')
+    def notification(self, cookie=None):
+        c.api_token = self._authenticate_request()
+        if c.api_token:
+            c.user = c.api_token.user
+        r = g.theme.get_site_notification(is_api=True, api_cookie=cookie)
+        if r:
+            return dict(notification=r[0], cookie=r[1])
+        return {}
+
     @expose()
     def _lookup(self, name, *remainder):
         c.api_token = self._authenticate_request()

http://git-wip-us.apache.org/repos/asf/allura/blob/e8e7b93a/Allura/allura/lib/plugin.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/plugin.py b/Allura/allura/lib/plugin.py
index cf833c1..e1653d9 100644
--- a/Allura/allura/lib/plugin.py
+++ b/Allura/allura/lib/plugin.py
@@ -1310,7 +1310,7 @@ class ThemeProvider(object):
         else:
             return app.icon_url(size)
 
-    def get_site_notification(self):
+    def get_site_notification(self, is_api=None, api_cookie=None):
         from pylons import request, response
         from allura.model.notification import SiteNotification
         note = SiteNotification.current()
@@ -1328,7 +1328,11 @@ class ThemeProvider(object):
         if note.page_tool_type and (c.app is None or c.app.config.tool_name.lower() != note.page_tool_type.lower()):
             return None
 
-        cookie = request.cookies.get('site-notification', '').split('-')
+        cookie = api_cookie
+        if not cookie:
+            cookie = request.cookies.get('site-notification', '')
+        cookie = cookie.split('-')
+
         if len(cookie) == 3 and cookie[0] == str(note._id):
             views = asint(cookie[1]) + 1
             closed = asbool(cookie[2])
@@ -1337,9 +1341,13 @@ class ThemeProvider(object):
             closed = False
         if closed or note.impressions > 0 and views > note.impressions:
             return None
+
+        set_cookie = '-'.join(map(str, [note._id, views, closed]))
+        if is_api:
+            return note, set_cookie
         response.set_cookie(
             'site-notification',
-            '-'.join(map(str, [note._id, views, closed])),
+            set_cookie,
             max_age=timedelta(days=365))
         return note
 

http://git-wip-us.apache.org/repos/asf/allura/blob/e8e7b93a/Allura/allura/model/__init__.py
----------------------------------------------------------------------
diff --git a/Allura/allura/model/__init__.py b/Allura/allura/model/__init__.py
index cb817b4..a69e7c9 100644
--- a/Allura/allura/model/__init__.py
+++ b/Allura/allura/model/__init__.py
@@ -29,7 +29,7 @@ from .attachments import BaseAttachment
 from .auth import AuthGlobals, User, ProjectRole, EmailAddress, OldProjectRole
 from .auth import AuditLog, audit_log, AlluraUserProperty
 from .filesystem import File
-from .notification import Notification, Mailbox
+from .notification import Notification, Mailbox, SiteNotification
 from .repository import Repository, RepositoryImplementation
 from .repository import MergeRequest, GitLikeTree
 from .stats import Stats
@@ -60,4 +60,4 @@ __all__ = [
     'OAuthRequestToken', 'OAuthAccessToken', 'MonQTask', 'Webhook', 'ACE', 'ACL', 'EVERYONE',
'ALL_PERMISSIONS',
     'DENY_ALL', 'MarkdownCache', 'main_doc_session', 'main_orm_session', 'project_doc_session',
'project_orm_session',
     'artifact_orm_session', 'repository_orm_session', 'task_orm_session', 'ArtifactSessionExtension',
'repository',
-    'repo_refresh', ]
+    'repo_refresh', 'SiteNotification']

http://git-wip-us.apache.org/repos/asf/allura/blob/e8e7b93a/Allura/allura/model/notification.py
----------------------------------------------------------------------
diff --git a/Allura/allura/model/notification.py b/Allura/allura/model/notification.py
index dfde847..a513795 100644
--- a/Allura/allura/model/notification.py
+++ b/Allura/allura/model/notification.py
@@ -720,6 +720,17 @@ class SiteNotification(MappedClass):
     page_regex = FieldProperty(str, if_missing=None)
     page_tool_type = FieldProperty(str, if_missing=None)
 
+    def __json__(self):
+        return dict(
+            _id=str(self._id),
+            content=self.content,
+            active=self.active,
+            impressions=self.impressions,
+            user_role=self.user_role if self.user_role else '',
+            page_regex=self.page_regex if self.page_regex else '',
+            page_tool_type=self.page_tool_type if self.page_tool_type else ''
+        )
+
     @classmethod
     def current(cls):
         note = cls.query.find({'active': True}).sort('_id', -1).limit(1).first()

http://git-wip-us.apache.org/repos/asf/allura/blob/e8e7b93a/Allura/allura/tests/functional/test_rest.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tests/functional/test_rest.py b/Allura/allura/tests/functional/test_rest.py
index 90b71ed..6f95c05 100644
--- a/Allura/allura/tests/functional/test_rest.py
+++ b/Allura/allura/tests/functional/test_rest.py
@@ -385,6 +385,19 @@ class TestRestHome(TestRestApiBase):
         self.app.post('/rest/p/test/wiki/NewPage', headers={'Origin': 'http://bad.com/'},
                       status=401)
 
+    @mock.patch('allura.lib.plugin.ThemeProvider.get_site_notification')
+    def test_notification(self, get_site_notification):
+        note = M.SiteNotification()
+        cookie = '{}-1-False'.format(note._id)
+        g.theme.get_site_notification = mock.Mock(return_value=(note, cookie))
+
+        r = self.app.get('/rest/notification')
+
+        assert r.status_int == 200
+        assert r.json['cookie'] == cookie
+        assert r.json['notification'] == note.__json__()
+
+
 
 class TestDoap(TestRestApiBase):
     validate_skip = True

http://git-wip-us.apache.org/repos/asf/allura/blob/e8e7b93a/Allura/allura/tests/model/test_notification.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tests/model/test_notification.py b/Allura/allura/tests/model/test_notification.py
index ac7ca3e..869a704 100644
--- a/Allura/allura/tests/model/test_notification.py
+++ b/Allura/allura/tests/model/test_notification.py
@@ -475,6 +475,37 @@ class TestSubscriptionTypes(unittest.TestCase):
         assert_equal(count, 1)
 
 
+class TestSiteNotification(unittest.TestCase):
+    def setUp(self):
+        self.note = M.SiteNotification(
+            active=True,
+            impressions=0,
+            content='test',
+            page_regex='',
+            page_tool_type='',
+            user_role=''
+        )
+
+    def test_json_type(self):
+        note_json = self.note.__json__()
+        assert isinstance(note_json, dict)
+
+    def test_json_keys(self):
+        keys = self.note.__json__().keys()
+        assert 'active' in keys
+        assert 'impressions' in keys
+        assert 'content' in keys
+        assert 'page_regex' in keys
+        assert 'page_tool_type' in keys
+        assert 'user_role' in keys
+
+    def test_json_values_if_missing(self):
+        note_json = self.note.__json__()
+        assert note_json['page_regex'] is ''
+        assert note_json['page_tool_type'] is ''
+        assert note_json['user_role'] is ''
+
+
 def _clear_subscriptions():
         M.Mailbox.query.remove({})
 

http://git-wip-us.apache.org/repos/asf/allura/blob/e8e7b93a/Allura/allura/tests/test_plugin.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tests/test_plugin.py b/Allura/allura/tests/test_plugin.py
index 0441f18..4f65c23 100644
--- a/Allura/allura/tests/test_plugin.py
+++ b/Allura/allura/tests/test_plugin.py
@@ -489,6 +489,37 @@ class TestThemeProvider(object):
         request.path_qs = 'ttt'
         assert_is(ThemeProvider().get_site_notification(), None)
 
+    @patch('allura.model.notification.SiteNotification')
+    def test_get_site_notification_with_is_api(self, SiteNotification):
+        note = SiteNotification.current.return_value
+        note._id = 'test_id'
+        note.user_role = None
+        note.page_regex = None
+        note.page_tool_type = None
+        get_note = ThemeProvider().get_site_notification(is_api=True)
+
+        assert isinstance(get_note, tuple)
+        assert len(get_note) is 2
+        assert get_note[0] is note
+        assert get_note[1] == 'test_id-1-False'
+
+    @patch('allura.model.notification.SiteNotification')
+    @patch('pylons.request')
+    def test_get_site_notifications_with_api_cookie(self, request, SiteNotification):
+        note = SiteNotification.current.return_value
+        note._id = 'test_id'
+        note.user_role = None
+        note.page_regex = None
+        note.page_tool_type = None
+        request.cookies = {}
+        get_note = ThemeProvider().get_site_notification(
+            is_api=True,
+            api_cookie='test_id-1-False'
+        )
+
+        assert get_note[0] is note
+        assert get_note[1] == 'test_id-2-False'
+
 
 class TestLocalAuthenticationProvider(object):
 


Mime
View raw message