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

[1/8] allura git commit: [#8044] ticket:889 Add kwargs to notification api

Repository: allura
Updated Branches:
  refs/heads/master 3ccbb5d4c -> 14ccf22b4


[#8044] ticket:889 Add kwargs to notification api


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

Branch: refs/heads/master
Commit: 192010517ff09417ff38adabf51e8b1bf60399a9
Parents: e8e7b93
Author: Denis Kotov <de...@gmail.com>
Authored: Wed Jan 20 12:27:56 2016 +0200
Committer: Dave Brondsema <da...@brondsema.net>
Committed: Wed Jan 27 12:37:37 2016 -0500

----------------------------------------------------------------------
 Allura/allura/controllers/rest.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/allura/blob/19201051/Allura/allura/controllers/rest.py
----------------------------------------------------------------------
diff --git a/Allura/allura/controllers/rest.py b/Allura/allura/controllers/rest.py
index c091874..83c67f8 100644
--- a/Allura/allura/controllers/rest.py
+++ b/Allura/allura/controllers/rest.py
@@ -90,7 +90,7 @@ class RestController(object):
         return summary
 
     @expose('json:')
-    def notification(self, cookie=None):
+    def notification(self, cookie=None, **kw):
         c.api_token = self._authenticate_request()
         if c.api_token:
             c.user = c.api_token.user


[6/8] allura git commit: [#8044] ticket:889 Add notification to raml doc

Posted by br...@apache.org.
[#8044] ticket:889 Add notification to raml doc


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

Branch: refs/heads/master
Commit: af8bd6e52a09ad0e370fa1dea86458f51541981a
Parents: e73efd1
Author: Denis Kotov <de...@gmail.com>
Authored: Fri Jan 22 01:54:10 2016 +0200
Committer: Dave Brondsema <da...@brondsema.net>
Committed: Wed Jan 27 12:37:38 2016 -0500

----------------------------------------------------------------------
 Allura/docs/api-rest/api.raml | 8 ++++++++
 1 file changed, 8 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/allura/blob/af8bd6e5/Allura/docs/api-rest/api.raml
----------------------------------------------------------------------
diff --git a/Allura/docs/api-rest/api.raml b/Allura/docs/api-rest/api.raml
index 4514a2e..6326978 100755
--- a/Allura/docs/api-rest/api.raml
+++ b/Allura/docs/api-rest/api.raml
@@ -35,6 +35,14 @@ documentation:
   - title: API Overview
     content: !include docs.md
 
+/notification:
+  description: |
+    Get current Site Notification
+  uriParameters:
+    cookie:
+      description: site-notification cookie value
+      example: "56a111736c4ff90011b9f932-1-False"
+
 /{neighborhood}:
     description: |
       Neighborhoods are groups of logically related projects, which have the same default options.


[8/8] allura git commit: [#8044] fix RAML syntax

Posted by br...@apache.org.
[#8044] fix RAML syntax


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

Branch: refs/heads/master
Commit: 14ccf22b4690fa0dcc1f72a651c9178fc48d3b4c
Parents: 5adc337
Author: Dave Brondsema <da...@brondsema.net>
Authored: Wed Jan 27 12:42:34 2016 -0500
Committer: Dave Brondsema <da...@brondsema.net>
Committed: Wed Jan 27 12:42:34 2016 -0500

----------------------------------------------------------------------
 Allura/docs/api-rest/api.raml | 21 +++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/allura/blob/14ccf22b/Allura/docs/api-rest/api.raml
----------------------------------------------------------------------
diff --git a/Allura/docs/api-rest/api.raml b/Allura/docs/api-rest/api.raml
index 9c50d9d..68d8a0f 100755
--- a/Allura/docs/api-rest/api.raml
+++ b/Allura/docs/api-rest/api.raml
@@ -740,13 +740,14 @@ documentation:
 /notification:
   description: |
     Get current Site Notification
-  uriParameters:
-    cookie:
-      description: site-notification cookie value
-      example: "56a111736c4ff90011b9f932-1-False"
-    url:
-      description: page where must be shown notification
-      example: "/p/test-project"
-    tool_name:
-      description: tool's name which used on page
-      exaple: "Git"
\ No newline at end of file
+  get:
+    queryParameters:
+      cookie:
+        description: site-notification cookie value
+        example: "56a111736c4ff90011b9f932-1-False"
+      url:
+        description: page where notification will be shown
+        example: "/p/test-project"
+      tool_name:
+        description: tool's name for current page
+        example: "Git"
\ No newline at end of file


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

Posted by br...@apache.org.
[#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/master
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


[3/8] allura git commit: [#8044] remove unnecessary c.app check, since the c.app.config usage is moved out of this function now

Posted by br...@apache.org.
[#8044] remove unnecessary c.app check, since the c.app.config usage is moved out of this function now


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

Branch: refs/heads/master
Commit: 5adc337eb4fffd14f8c6a7f33c4bf4840d67f8b2
Parents: 1ec256c
Author: Dave Brondsema <da...@brondsema.net>
Authored: Wed Jan 27 12:37:09 2016 -0500
Committer: Dave Brondsema <da...@brondsema.net>
Committed: Wed Jan 27 12:37:38 2016 -0500

----------------------------------------------------------------------
 Allura/allura/lib/plugin.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/allura/blob/5adc337e/Allura/allura/lib/plugin.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/plugin.py b/Allura/allura/lib/plugin.py
index e76c53c..1c65a10 100644
--- a/Allura/allura/lib/plugin.py
+++ b/Allura/allura/lib/plugin.py
@@ -1324,7 +1324,7 @@ class ThemeProvider(object):
 
         if note.page_regex and re.search(note.page_regex, url) is None:
             return None
-        if note.page_tool_type and (c.app is None or tool_name.lower() != note.page_tool_type.lower()):
+        if note.page_tool_type and tool_name.lower() != note.page_tool_type.lower():
             return None
 
         cookie = site_notification_cookie_value.split('-')


[5/8] allura git commit: [#8044] ticket:891 Add tool_name parameter to notification API, change API documentation

Posted by br...@apache.org.
[#8044] ticket:891 Add tool_name parameter to notification API, change API documentation


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

Branch: refs/heads/master
Commit: 1ec256c5033618836b96719b43d59817cd27e084
Parents: 62cbf93
Author: Denis Kotov <de...@gmail.com>
Authored: Sat Jan 23 19:26:48 2016 +0200
Committer: Dave Brondsema <da...@brondsema.net>
Committed: Wed Jan 27 12:37:38 2016 -0500

----------------------------------------------------------------------
 Allura/allura/controllers/rest.py           |  3 ++-
 Allura/allura/lib/plugin.py                 |  9 +++++----
 Allura/allura/tests/functional/test_rest.py |  7 ++++---
 Allura/docs/api-rest/api.raml               | 24 +++++++++++++++---------
 4 files changed, 26 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/allura/blob/1ec256c5/Allura/allura/controllers/rest.py
----------------------------------------------------------------------
diff --git a/Allura/allura/controllers/rest.py b/Allura/allura/controllers/rest.py
index 70a9ffd..915517c 100644
--- a/Allura/allura/controllers/rest.py
+++ b/Allura/allura/controllers/rest.py
@@ -90,12 +90,13 @@ class RestController(object):
         return summary
 
     @expose('json:')
-    def notification(self, cookie='', url='', **kw):
+    def notification(self, cookie='', url='', tool_name='', **kw):
         c.api_token = self._authenticate_request()
         user = c.api_token.user if c.api_token else c.user
         r = g.theme._get_site_notification(
             url=url,
             user=user,
+            tool_name=tool_name,
             site_notification_cookie_value=cookie
         )
         if r:

http://git-wip-us.apache.org/repos/asf/allura/blob/1ec256c5/Allura/allura/lib/plugin.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/plugin.py b/Allura/allura/lib/plugin.py
index dffd1fa..e76c53c 100644
--- a/Allura/allura/lib/plugin.py
+++ b/Allura/allura/lib/plugin.py
@@ -1310,12 +1310,12 @@ class ThemeProvider(object):
         else:
             return app.icon_url(size)
 
-    def _get_site_notification(self, url='', user=None, site_notification_cookie_value=''):
+    def _get_site_notification(self, url='', user=None, tool_name='', site_notification_cookie_value=''):
         from allura.model.notification import SiteNotification
         note = SiteNotification.current()
         if note is None:
             return None
-        if note.user_role and user.is_anonymous():
+        if note.user_role and (not user or user.is_anonymous()):
             return None
         if note.user_role:
             projects = user.my_projects_by_role_name(note.user_role)
@@ -1324,7 +1324,7 @@ class ThemeProvider(object):
 
         if note.page_regex and re.search(note.page_regex, url) is None:
             return None
-        if note.page_tool_type and (c.app is None or c.app.config.tool_name.lower() != note.page_tool_type.lower()):
+        if note.page_tool_type and (c.app is None or tool_name.lower() != note.page_tool_type.lower()):
             return None
 
         cookie = site_notification_cookie_value.split('-')
@@ -1342,10 +1342,11 @@ class ThemeProvider(object):
 
     def get_site_notification(self):
         from pylons import request, response
-
+        tool_name = c.app.config.tool_name if c.app else ''
         r = self._get_site_notification(
             request.path_qs,
             c.user,
+            tool_name,
             request.cookies.get('site-notification', '')
         )
         if not r:

http://git-wip-us.apache.org/repos/asf/allura/blob/1ec256c5/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 3ec58f6..91cf40f 100644
--- a/Allura/allura/tests/functional/test_rest.py
+++ b/Allura/allura/tests/functional/test_rest.py
@@ -392,11 +392,12 @@ class TestRestHome(TestRestApiBase):
         cookie = '{}-1-False'.format(note._id)
         g.theme._get_site_notification = mock.Mock(return_value=(note, cookie))
 
-        r = self.app.get('/rest/notification?url=test_url&cookie=test_cookie')
+        r = self.app.get('/rest/notification?url=test_url&cookie=test_cookie&tool_name=test_tool')
         g.theme._get_site_notification.assert_called_once_with(
             url='test_url',
-            site_notification_cookie_value='test_cookie',
-            user=user
+            user=user,
+            tool_name='test_tool',
+            site_notification_cookie_value='test_cookie'
         )
 
         assert r.status_int == 200

http://git-wip-us.apache.org/repos/asf/allura/blob/1ec256c5/Allura/docs/api-rest/api.raml
----------------------------------------------------------------------
diff --git a/Allura/docs/api-rest/api.raml b/Allura/docs/api-rest/api.raml
index 6326978..9c50d9d 100755
--- a/Allura/docs/api-rest/api.raml
+++ b/Allura/docs/api-rest/api.raml
@@ -35,14 +35,6 @@ documentation:
   - title: API Overview
     content: !include docs.md
 
-/notification:
-  description: |
-    Get current Site Notification
-  uriParameters:
-    cookie:
-      description: site-notification cookie value
-      example: "56a111736c4ff90011b9f932-1-False"
-
 /{neighborhood}:
     description: |
       Neighborhoods are groups of logically related projects, which have the same default options.
@@ -743,4 +735,18 @@ documentation:
           example: !include examples/userProfile.json,
           schema: !include schemas/userProfile.json
         }
-      }
\ No newline at end of file
+      }
+
+/notification:
+  description: |
+    Get current Site Notification
+  uriParameters:
+    cookie:
+      description: site-notification cookie value
+      example: "56a111736c4ff90011b9f932-1-False"
+    url:
+      description: page where must be shown notification
+      example: "/p/test-project"
+    tool_name:
+      description: tool's name which used on page
+      exaple: "Git"
\ No newline at end of file


[2/8] allura git commit: [#8044] ticket:889 Added rest api for get_site_notification

Posted by br...@apache.org.
[#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/master
Commit: e8e7b93ae4d9a11f4cfc652c9532a6609bf25019
Parents: 3ccbb5d
Author: Denis Kotov <de...@gmail.com>
Authored: Tue Jan 19 23:11:25 2016 +0200
Committer: Dave Brondsema <da...@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):
 


[7/8] allura git commit: [#8044] ticket:891 Add url parameter to notification API, change notification API test

Posted by br...@apache.org.
[#8044] ticket:891 Add url parameter to notification API, change notification API test


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

Branch: refs/heads/master
Commit: 62cbf93216e98f421ed0573ef13186ffaeafd8ed
Parents: af8bd6e
Author: Denis Kotov <de...@gmail.com>
Authored: Sat Jan 23 14:48:42 2016 +0200
Committer: Dave Brondsema <da...@brondsema.net>
Committed: Wed Jan 27 12:37:38 2016 -0500

----------------------------------------------------------------------
 Allura/allura/controllers/rest.py           | 17 +++++++----------
 Allura/allura/lib/plugin.py                 |  8 ++++----
 Allura/allura/tests/functional/test_rest.py | 13 +++++++++++--
 3 files changed, 22 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/allura/blob/62cbf932/Allura/allura/controllers/rest.py
----------------------------------------------------------------------
diff --git a/Allura/allura/controllers/rest.py b/Allura/allura/controllers/rest.py
index ef09398..70a9ffd 100644
--- a/Allura/allura/controllers/rest.py
+++ b/Allura/allura/controllers/rest.py
@@ -90,17 +90,14 @@ class RestController(object):
         return summary
 
     @expose('json:')
-    def notification(self, cookie='', **kw):
+    def notification(self, cookie='', url='', **kw):
         c.api_token = self._authenticate_request()
-        if c.api_token:
-            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
-            )
+        user = c.api_token.user if c.api_token else c.user
+        r = g.theme._get_site_notification(
+            url=url,
+            user=user,
+            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/62cbf932/Allura/allura/lib/plugin.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/plugin.py b/Allura/allura/lib/plugin.py
index 6ae6eb0..dffd1fa 100644
--- a/Allura/allura/lib/plugin.py
+++ b/Allura/allura/lib/plugin.py
@@ -1310,20 +1310,19 @@ class ThemeProvider(object):
         else:
             return app.icon_url(size)
 
-    def _get_site_notification(self, user=None, site_notification_cookie_value=''):
-        from pylons import request
+    def _get_site_notification(self, url='', user=None, site_notification_cookie_value=''):
         from allura.model.notification import SiteNotification
         note = SiteNotification.current()
         if note is None:
             return None
-        if note.user_role and (not user or user.is_anonymous()):
+        if note.user_role and user.is_anonymous():
             return None
         if 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
 
-        if note.page_regex and re.search(note.page_regex, request.path_qs) is None:
+        if note.page_regex and re.search(note.page_regex, url) is None:
             return None
         if note.page_tool_type and (c.app is None or c.app.config.tool_name.lower() != note.page_tool_type.lower()):
             return None
@@ -1345,6 +1344,7 @@ class ThemeProvider(object):
         from pylons import request, response
 
         r = self._get_site_notification(
+            request.path_qs,
             c.user,
             request.cookies.get('site-notification', '')
         )

http://git-wip-us.apache.org/repos/asf/allura/blob/62cbf932/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 0ff0526..3ec58f6 100644
--- a/Allura/allura/tests/functional/test_rest.py
+++ b/Allura/allura/tests/functional/test_rest.py
@@ -387,17 +387,26 @@ class TestRestHome(TestRestApiBase):
 
     @mock.patch('allura.lib.plugin.ThemeProvider._get_site_notification')
     def test_notification(self, _get_site_notification):
+        user = M.User.by_username('test-admin')
         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')
+        r = self.app.get('/rest/notification?url=test_url&cookie=test_cookie')
+        g.theme._get_site_notification.assert_called_once_with(
+            url='test_url',
+            site_notification_cookie_value='test_cookie',
+            user=user
+        )
 
         assert r.status_int == 200
-        print r.json
         assert r.json['cookie'] == cookie
         assert r.json['notification'] == note.__json__()
 
+        g.theme._get_site_notification = mock.Mock(return_value=None)
+        r = self.app.get('/rest/notification')
+        assert r.json == {}
+
 
 
 class TestDoap(TestRestApiBase):