You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by he...@apache.org on 2016/01/27 21:22:30 UTC

[16/50] allura git commit: [#8044] ticket:889 Move main logic in _get_site_notification

[#8044] ticket:889 Move main logic in _get_site_notification


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

Branch: refs/heads/hs/8035
Commit: e73efd1df38e555697c9da6df327f71b7b646d7e
Parents: 1920105
Author: Denis Kotov <de...@gmail.com>
Authored: Fri Jan 22 01:08:18 2016 +0200
Committer: Dave Brondsema <da...@brondsema.net>
Committed: Wed Jan 27 12:37:38 2016 -0500

----------------------------------------------------------------------
 Allura/allura/controllers/rest.py           | 12 +++++++---
 Allura/allura/lib/plugin.py                 | 29 +++++++++++++++---------
 Allura/allura/tests/functional/test_rest.py |  7 +++---
 Allura/allura/tests/test_plugin.py          |  9 ++++----
 4 files changed, 35 insertions(+), 22 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/allura/blob/e73efd1d/Allura/allura/controllers/rest.py
----------------------------------------------------------------------
diff --git a/Allura/allura/controllers/rest.py b/Allura/allura/controllers/rest.py
index 83c67f8..ef09398 100644
--- a/Allura/allura/controllers/rest.py
+++ b/Allura/allura/controllers/rest.py
@@ -90,11 +90,17 @@ class RestController(object):
         return summary
 
     @expose('json:')
-    def notification(self, cookie=None, **kw):
+    def notification(self, cookie='', **kw):
         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)
+            r = g.theme._get_site_notification(
+                user=c.api_token.user,
+                site_notification_cookie_value=cookie
+            )
+        else:
+            r = g.theme._get_site_notification(
+                site_notification_cookie_value=cookie
+            )
         if r:
             return dict(notification=r[0], cookie=r[1])
         return {}

http://git-wip-us.apache.org/repos/asf/allura/blob/e73efd1d/Allura/allura/lib/plugin.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/plugin.py b/Allura/allura/lib/plugin.py
index e1653d9..6ae6eb0 100644
--- a/Allura/allura/lib/plugin.py
+++ b/Allura/allura/lib/plugin.py
@@ -1310,16 +1310,16 @@ class ThemeProvider(object):
         else:
             return app.icon_url(size)
 
-    def get_site_notification(self, is_api=None, api_cookie=None):
-        from pylons import request, response
+    def _get_site_notification(self, user=None, site_notification_cookie_value=''):
+        from pylons import request
         from allura.model.notification import SiteNotification
         note = SiteNotification.current()
         if note is None:
             return None
-        if note.user_role and c.user.is_anonymous():
+        if note.user_role and (not user or user.is_anonymous()):
             return None
         if note.user_role:
-            projects = c.user.my_projects_by_role_name(note.user_role)
+            projects = user.my_projects_by_role_name(note.user_role)
             if len(projects) == 0 or len(projects) == 1 and projects[0].is_user_project:
                 return None
 
@@ -1328,11 +1328,7 @@ 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 = api_cookie
-        if not cookie:
-            cookie = request.cookies.get('site-notification', '')
-        cookie = cookie.split('-')
-
+        cookie = site_notification_cookie_value.split('-')
         if len(cookie) == 3 and cookie[0] == str(note._id):
             views = asint(cookie[1]) + 1
             closed = asbool(cookie[2])
@@ -1343,8 +1339,19 @@ class ThemeProvider(object):
             return None
 
         set_cookie = '-'.join(map(str, [note._id, views, closed]))
-        if is_api:
-            return note, set_cookie
+        return note, set_cookie
+
+    def get_site_notification(self):
+        from pylons import request, response
+
+        r = self._get_site_notification(
+            c.user,
+            request.cookies.get('site-notification', '')
+        )
+        if not r:
+            return None
+        note, set_cookie = r
+
         response.set_cookie(
             'site-notification',
             set_cookie,

http://git-wip-us.apache.org/repos/asf/allura/blob/e73efd1d/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 6f95c05..0ff0526 100644
--- a/Allura/allura/tests/functional/test_rest.py
+++ b/Allura/allura/tests/functional/test_rest.py
@@ -385,15 +385,16 @@ 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):
+    @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))
+        g.theme._get_site_notification = mock.Mock(return_value=(note, cookie))
 
         r = self.app.get('/rest/notification')
 
         assert r.status_int == 200
+        print r.json
         assert r.json['cookie'] == cookie
         assert r.json['notification'] == note.__json__()
 

http://git-wip-us.apache.org/repos/asf/allura/blob/e73efd1d/Allura/allura/tests/test_plugin.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tests/test_plugin.py b/Allura/allura/tests/test_plugin.py
index 4f65c23..1cbd686 100644
--- a/Allura/allura/tests/test_plugin.py
+++ b/Allura/allura/tests/test_plugin.py
@@ -490,13 +490,13 @@ class TestThemeProvider(object):
         assert_is(ThemeProvider().get_site_notification(), None)
 
     @patch('allura.model.notification.SiteNotification')
-    def test_get_site_notification_with_is_api(self, SiteNotification):
+    def test_get__site_notification(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)
+        get_note = ThemeProvider()._get_site_notification()
 
         assert isinstance(get_note, tuple)
         assert len(get_note) is 2
@@ -512,9 +512,8 @@ class TestThemeProvider(object):
         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'
+        get_note = ThemeProvider()._get_site_notification(
+            site_notification_cookie_value='test_id-1-False'
         )
 
         assert get_note[0] is note