allura-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From john...@apache.org
Subject [21/40] git commit: [#6777] Use single site-notification cookie and improve performance / logic of in/active notifications
Date Thu, 31 Oct 2013 21:57:32 GMT
[#6777] Use single site-notification cookie and improve performance / logic of in/active notifications

Signed-off-by: Cory Johns <cjohns@slashdotmedia.com>


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

Branch: refs/heads/cj/6714
Commit: 152838e778e8d39a6cec972e465323b77ed69ba5
Parents: 7c151f2
Author: Cory Johns <cjohns@slashdotmedia.com>
Authored: Mon Oct 21 22:33:59 2013 +0000
Committer: Dave Brondsema <dbrondsema@slashdotmedia.com>
Committed: Fri Oct 25 18:29:22 2013 +0000

----------------------------------------------------------------------
 Allura/allura/lib/plugin.py               | 18 +++++++----
 Allura/allura/model/notification.py       |  8 +++--
 Allura/allura/public/nf/js/allura-base.js |  5 ++-
 Allura/allura/tests/test_plugin.py        | 43 +++++++++++++++++++++++---
 Allura/docs/administration.rst            | 20 ++++++++++++
 5 files changed, 79 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/152838e7/Allura/allura/lib/plugin.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/plugin.py b/Allura/allura/lib/plugin.py
index 72dffaf..c09548e 100644
--- a/Allura/allura/lib/plugin.py
+++ b/Allura/allura/lib/plugin.py
@@ -873,13 +873,19 @@ class ThemeProvider(object):
         note = SiteNotification.current()
         if note is None:
             return None
-        closed_cookie = ('notification-closed-%s' % note._id).encode('utf8')
-        seen_cookie = ('notification-seen-%s' % note._id).encode('utf8')
-        closed = asbool(request.cookies.get(closed_cookie))
-        seen = asint(request.cookies.get(seen_cookie, '0'))+1
-        if closed or note.impressions > 0 and seen > note.impressions:
+        cookie = request.cookies.get('site-notification', '').split('-')
+        if len(cookie) == 3 and cookie[0] == str(note._id):
+            views = asint(cookie[1])+1
+            closed = asbool(cookie[2])
+        else:
+            views = 1
+            closed = False
+        if closed or note.impressions > 0 and views > note.impressions:
             return None
-        response.set_cookie(seen_cookie, str(seen), max_age=timedelta(days=365))
+        response.set_cookie(
+                'site-notification',
+                '-'.join(map(str, [note._id, views, closed])),
+                max_age=timedelta(days=365))
         return note
 
 class LocalProjectRegistrationProvider(ProjectRegistrationProvider):

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/152838e7/Allura/allura/model/notification.py
----------------------------------------------------------------------
diff --git a/Allura/allura/model/notification.py b/Allura/allura/model/notification.py
index 260e07d..8851362 100644
--- a/Allura/allura/model/notification.py
+++ b/Allura/allura/model/notification.py
@@ -630,13 +630,15 @@ class SiteNotification(MappedClass):
     class __mongometa__:
         session = main_orm_session
         name = 'site_notification'
-        indexes = ['deleted']
 
     _id = FieldProperty(S.ObjectId)
     content = FieldProperty(str, if_missing='')
-    deleted = FieldProperty(bool, if_missing=False)
+    active = FieldProperty(bool, if_missing=True)
     impressions = FieldProperty(int, if_missing=lambda:config.get('site_notification.impressions',
0))
 
     @classmethod
     def current(cls):
-        return cls.query.find({'deleted': False}).sort('_id', -1).limit(1).first()
+        note = cls.query.find().sort('_id', -1).limit(1).first()
+        if note is None or not note.active:
+            return None
+        return note

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/152838e7/Allura/allura/public/nf/js/allura-base.js
----------------------------------------------------------------------
diff --git a/Allura/allura/public/nf/js/allura-base.js b/Allura/allura/public/nf/js/allura-base.js
index 9c47a48..f9e9c0d 100644
--- a/Allura/allura/public/nf/js/allura-base.js
+++ b/Allura/allura/public/nf/js/allura-base.js
@@ -217,10 +217,13 @@ $(function(){
     var cval = $.cookie('_session_id');
     $('form[method=post]').append('<input name="_session_id" type="hidden" value="'+cval+'">');
 
+    var SN_ID=0, SN_VIEWS=1, SN_CLOSED=2;
     $('#site-notification .ico-close').click(function() {
         var $note = $(this).parent();
         $note.hide();
-        $.cookie('notification-closed-'+$note.data('notification-id'), 'true', {
+        var status = $.cookie('site-notification').split('-');
+        status[SN_CLOSED] = 'true';
+        $.cookie('site-notification', status.join('-'), {
             expires: 365,
             path: '/'
         });

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/152838e7/Allura/allura/tests/test_plugin.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tests/test_plugin.py b/Allura/allura/tests/test_plugin.py
index 9a9a7dd..66d4715 100644
--- a/Allura/allura/tests/test_plugin.py
+++ b/Allura/allura/tests/test_plugin.py
@@ -79,7 +79,7 @@ class TestThemeProvider(object):
     @patch('pylons.request')
     def test_get_site_notification_closed(self, request, response, SiteNotification):
         SiteNotification.current.return_value._id = 'deadbeef'
-        request.cookies = {'notification-closed-deadbeef': 'true'}
+        request.cookies = {'site-notification': 'deadbeef-1-true'}
         assert_is_none(ThemeProvider().get_site_notification())
         assert not response.set_cookie.called
 
@@ -90,7 +90,7 @@ class TestThemeProvider(object):
         note = SiteNotification.current.return_value
         note._id = 'deadbeef'
         note.impressions = 2
-        request.cookies = {'notification-seen-deadbeef': '3'}
+        request.cookies = {'site-notification': 'deadbeef-3-false'}
         assert_is_none(ThemeProvider().get_site_notification())
         assert not response.set_cookie.called
 
@@ -101,9 +101,9 @@ class TestThemeProvider(object):
         note = SiteNotification.current.return_value
         note._id = 'deadbeef'
         note.impressions = 2
-        request.cookies = {'notification-seen-deadbeef': '1'}
+        request.cookies = {'site-notification': 'deadbeef-1-false'}
         assert_is(ThemeProvider().get_site_notification(), note)
-        response.set_cookie.assert_called_once_with('notification-seen-deadbeef', '2', max_age=timedelta(days=365))
+        response.set_cookie.assert_called_once_with('site-notification', 'deadbeef-2-False',
max_age=timedelta(days=365))
 
     @patch('allura.model.notification.SiteNotification')
     @patch('pylons.response')
@@ -112,5 +112,38 @@ class TestThemeProvider(object):
         note = SiteNotification.current.return_value
         note._id = 'deadbeef'
         note.impressions = 0
-        request.cookies = {'notification-seen-deadbeef': '1000'}
+        request.cookies = {'site-notification': 'deadbeef-1000-false'}
         assert_is(ThemeProvider().get_site_notification(), note)
+
+    @patch('allura.model.notification.SiteNotification')
+    @patch('pylons.response')
+    @patch('pylons.request')
+    def test_get_site_notification_new_notification(self, request, response, SiteNotification):
+        note = SiteNotification.current.return_value
+        note._id = 'deadbeef'
+        note.impressions = 1
+        request.cookies = {'site-notification': '0ddba11-1000-true'}
+        assert_is(ThemeProvider().get_site_notification(), note)
+        response.set_cookie.assert_called_once_with('site-notification', 'deadbeef-1-False',
max_age=timedelta(days=365))
+
+    @patch('allura.model.notification.SiteNotification')
+    @patch('pylons.response')
+    @patch('pylons.request')
+    def test_get_site_notification_no_cookie(self, request, response, SiteNotification):
+        note = SiteNotification.current.return_value
+        note._id = 'deadbeef'
+        note.impressions = 0
+        request.cookies = {}
+        assert_is(ThemeProvider().get_site_notification(), note)
+        response.set_cookie.assert_called_once_with('site-notification', 'deadbeef-1-False',
max_age=timedelta(days=365))
+
+    @patch('allura.model.notification.SiteNotification')
+    @patch('pylons.response')
+    @patch('pylons.request')
+    def test_get_site_notification_bad_cookie(self, request, response, SiteNotification):
+        note = SiteNotification.current.return_value
+        note._id = 'deadbeef'
+        note.impressions = 0
+        request.cookies = {'site-notification': 'deadbeef-1000-true-bad'}
+        assert_is(ThemeProvider().get_site_notification(), note)
+        response.set_cookie.assert_called_once_with('site-notification', 'deadbeef-1-False',
max_age=timedelta(days=365))

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/152838e7/Allura/docs/administration.rst
----------------------------------------------------------------------
diff --git a/Allura/docs/administration.rst b/Allura/docs/administration.rst
index 3874179..eaf9387 100644
--- a/Allura/docs/administration.rst
+++ b/Allura/docs/administration.rst
@@ -73,3 +73,23 @@ One such script is `wiki-copy.py` which reads the wiki pages from one Allura
wik
 instance and uploads them to another Allura wiki instance.  It can be run as::
 
 $ python scripts/wiki-copy.py --help
+
+
+Site Notifications
+------------------
+
+Allura has support for site-wide notifications that appear below the site header,
+but there is currently no UI for managing them.  They can easily be inserted via
+manual mongo queries, however:
+
+    > db.site_notification.insert({
+    ... active: true,
+    ... impressions: 10,
+    ... content: 'You can now reimport exported project data.'
+    ... })
+
+This will create a notification that will be shown for 10 page views or until the
+user closes it manually.  An `impressions` value of 0 will show the notification
+indefinitely (until closed).  The notification content can contain HTML.  Only the
+most recent notification will be shown, unless it has `active:false`, in which case
+no notification will be shown.


Mime
View raw message