incubator-allura-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From brond...@apache.org
Subject [3/5] git commit: [#2834] ticket:178 add subscribe for tools ability to /auth/prefs/
Date Wed, 07 Nov 2012 20:05:09 GMT
[#2834] ticket:178 add subscribe for tools ability to /auth/prefs/


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

Branch: refs/heads/master
Commit: 80bf6d27e8af592930d4c0696f9ae4a93324a391
Parents: dc3b9d5
Author: Igor Bondarenko <jetmind2@gmail.com>
Authored: Thu Sep 27 13:30:10 2012 +0300
Committer: Dave Brondsema <dbrondsema@geek.net>
Committed: Wed Nov 7 19:59:42 2012 +0000

----------------------------------------------------------------------
 Allura/allura/controllers/auth.py             |   34 ++++++++-
 Allura/allura/lib/widgets/subscriptions.py    |    8 +-
 Allura/allura/templates/user_preferences.html |    3 +-
 Allura/allura/tests/functional/test_auth.py   |   78 ++++++++++++++++++-
 4 files changed, 110 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/80bf6d27/Allura/allura/controllers/auth.py
----------------------------------------------------------------------
diff --git a/Allura/allura/controllers/auth.py b/Allura/allura/controllers/auth.py
index c43b571..7fdda6a 100644
--- a/Allura/allura/controllers/auth.py
+++ b/Allura/allura/controllers/auth.py
@@ -310,14 +310,34 @@ class PreferencesController(BaseController):
             if mb.artifact_url:
                 title = '<a href="%s">%s</a>' % (mb.artifact_url,title)
             subscriptions.append(dict(
-                    _id=mb._id,
+                    subscription_id=mb._id,
                     project_name=project.name,
                     mount_point=app_config.options['mount_point'],
                     artifact_title=title,
                     topic=mb.topic,
                     type=mb.type,
                     frequency=mb.frequency.unit,
-                    artifact=mb.artifact_index_id))
+                    artifact=mb.artifact_index_id,
+                    subscribed=True))
+
+        my_projects = dict((p._id, p) for p in c.user.my_projects())
+        my_tools = app_collection.m.find(dict(
+            project_id={'$in': my_projects.keys()}))
+        for tool in my_tools:
+            p_id = tool.project_id
+            subscribed = M.Mailbox.subscribed(
+                    project_id=p_id, app_config_id=tool._id)
+            if not subscribed:
+                subscriptions.append(dict(
+                    tool_id=tool._id,
+                    project_id=p_id,
+                    project_name=my_projects[p_id].name,
+                    mount_point=tool.options['mount_point'],
+                    artifact_title='No subscription',
+                    topic=None,
+                    type=None,
+                    frequency=None,
+                    artifact=None))
         api_token = M.ApiToken.query.get(user_id=c.user._id)
         provider = plugin.AuthenticationProvider.get(request)
         menu = provider.account_navigation()
@@ -378,8 +398,14 @@ class PreferencesController(BaseController):
     @validate(F.subscription_form, error_handler=index)
     def update_subscriptions(self, subscriptions=None, **kw):
         for s in subscriptions:
-            if s['unsubscribe']:
-                s['_id'].delete()
+            if s['subscribed']:
+                if s['tool_id'] and s['project_id']:
+                    M.Mailbox.subscribe(
+                        project_id=bson.ObjectId(s['project_id']),
+                        app_config_id=bson.ObjectId(s['tool_id']))
+            else:
+                if s['subscription_id'] is not None:
+                    s['subscription_id'].delete()
         redirect(request.referer)
 
     @expose()

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/80bf6d27/Allura/allura/lib/widgets/subscriptions.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/widgets/subscriptions.py b/Allura/allura/lib/widgets/subscriptions.py
index 2ab57ae..b92b255 100644
--- a/Allura/allura/lib/widgets/subscriptions.py
+++ b/Allura/allura/lib/widgets/subscriptions.py
@@ -11,7 +11,9 @@ from .form_fields import SubmitButton
 # Discussion forms
 class _SubscriptionTable(ew.TableField):
     class hidden_fields(ew_core.NameList):
-        _id = ew.HiddenField(validator=V.Ming(M.Mailbox))
+        subscription_id = ew.HiddenField(validator=V.Ming(M.Mailbox))
+        tool_id = ew.HiddenField()
+        project_id = ew.HiddenField()
         topic = ew.HiddenField()
         artifact_index_id = ew.HiddenField()
     class fields(ew_core.NameList):
@@ -22,12 +24,12 @@ class _SubscriptionTable(ew.TableField):
         frequency = ew.HTMLField(label='Frequency', show_label=True)
         artifact_title = ew.HTMLField(label='Artifact', show_label=True)
         # unsubscribe = SubmitButton()
-        unsubscribe = ew.Checkbox(suppress_label=True)
+        subscribed = ew.Checkbox(suppress_label=True)
 
 class SubscriptionForm(ew.SimpleForm):
     defaults=dict(
         ew.SimpleForm.defaults,
-        submit_text='Unsubscribe from marked artifacts')
+        submit_text='Save')
     class fields(ew_core.NameList):
         subscriptions=_SubscriptionTable()
 

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/80bf6d27/Allura/allura/templates/user_preferences.html
----------------------------------------------------------------------
diff --git a/Allura/allura/templates/user_preferences.html b/Allura/allura/templates/user_preferences.html
index 874ab16..a0855a8 100644
--- a/Allura/allura/templates/user_preferences.html
+++ b/Allura/allura/templates/user_preferences.html
@@ -67,6 +67,7 @@
 
   <h2>Subscriptions</h2>
   {% if subscriptions %}
+    <p><em>Mark tools that you want to subscribe to. Unmark tools that you want
to unsubscribe from. Press 'Save' button.</em></p>
     {{c.form.display(action='update_subscriptions', value=dict(subscriptions=subscriptions))}}
   {% else%}
     <p>No subscriptions.</p>
@@ -99,7 +100,7 @@
           </select>
         </div>
         {% endif %}
-    
+
     {% if tg.config.get('auth.method', 'local') == 'local' %}
       {% for a in c.user.email_addresses %}
         <input name="addr-{{loop.index0}}.ord" value="{{loop.index0}}" type="hidden"/>

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/80bf6d27/Allura/allura/tests/functional/test_auth.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tests/functional/test_auth.py b/Allura/allura/tests/functional/test_auth.py
index 7f7e9b2..3493594 100644
--- a/Allura/allura/tests/functional/test_auth.py
+++ b/Allura/allura/tests/functional/test_auth.py
@@ -1,4 +1,5 @@
 import json
+from bson import ObjectId
 
 import mock
 from nose.tools import assert_equal
@@ -42,11 +43,6 @@ class TestAuth(TestController):
     def test_prefs(self):
         r = self.app.get('/auth/prefs/', extra_environ=dict(username='test-admin'))
         assert 'test@example.com' not in r
-        subscriptions = M.Mailbox.query.find(dict(user_id=c.user._id, is_flash=False)).all()
-        # make sure page actually lists all the user's subscriptions
-        assert len(subscriptions) > 0, 'Test user has no subscriptions, cannot verify
that they are shown'
-        for m in subscriptions:
-            assert m._id in r, "Page doesn't list subscription for Mailbox._id = %s" % m._id
         r = self.app.post('/auth/prefs/update', params={
                  'display_name':'Test Admin',
                  'new_addr.addr':'test@example.com',
@@ -78,6 +74,78 @@ class TestAuth(TestController):
                  'preferences.email_format':'plain'},
                 extra_environ=dict(username='test-admin'))
 
+    @td.with_user_project('test-admin')
+    def test_prefs_subscriptions(self):
+        r = self.app.get('/auth/prefs/',
+                extra_environ=dict(username='test-admin'))
+        subscriptions = M.Mailbox.query.find(dict(
+            user_id=c.user._id, is_flash=False)).all()
+        # make sure page actually lists all the user's subscriptions
+        assert len(subscriptions) > 0, 'Test user has no subscriptions, cannot verify
that they are shown'
+        for m in subscriptions:
+            assert m._id in r, "Page doesn't list subscription for Mailbox._id = %s" % m._id
+
+        # make sure page lists all tools which user can subscribe
+        user = M.User.query.get(username='test-admin')
+        tools = []
+        for p in user.my_projects():
+            for ac in p.app_configs:
+                if not M.Mailbox.subscribed(project_id=p._id, app_config_id=ac._id):
+                    tools.append(ac._id)
+        for tool_id in tools:
+            assert tool_id in r, "Page doesn't list tool with app_config_id = %s" % tool_id
+
+    def _find_subscriptions_form(self, r):
+        form = None
+        for f in r.forms.itervalues():
+            if f.action == 'update_subscriptions':
+                form = f
+                break;
+        assert form is not None, "Can't find subscriptions form"
+        return form
+
+    def _find_subscriptions_field(self, form, subscribed=False):
+        field_name = None
+        for k, v in form.fields.iteritems():
+            if subscribed:
+                check = c and v[0].value == 'on'
+            else:
+                check = c and v[0].value != 'on'
+            if k and k.endswith('.subscribed') and check:
+                field_name = k.replace('.subscribed', '')
+        assert field_name, "Can't find unsubscribed tool for user"
+        return field_name
+
+    @td.with_user_project('test-admin')
+    def test_prefs_subscriptions_subscribe(self):
+        resp = self.app.get('/auth/prefs/',
+                extra_environ=dict(username='test-admin'))
+        form = self._find_subscriptions_form(resp)
+        # find not subscribed tool, subscribe and verify
+        field_name = self._find_subscriptions_field(form, subscribed=False)
+        t_id = ObjectId(form.fields[field_name + '.tool_id'][0].value)
+        p_id = ObjectId(form.fields[field_name + '.project_id'][0].value)
+        subscribed = M.Mailbox.subscribed(project_id=p_id, app_config_id=t_id)
+        assert not subscribed, "User already subscribed for tool %s" % t_id
+        form.fields[field_name + '.subscribed'][0].value = 'on'
+        form.submit()
+        subscribed = M.Mailbox.subscribed(project_id=p_id, app_config_id=t_id)
+        assert subscribed, "User is not subscribed for tool %s" % t_id
+
+    @td.with_user_project('test-admin')
+    def test_prefs_subscriptions_unsubscribe(self):
+        resp = self.app.get('/auth/prefs/',
+                extra_environ=dict(username='test-admin'))
+        form = self._find_subscriptions_form(resp)
+        field_name = self._find_subscriptions_field(form, subscribed=True)
+        s_id = ObjectId(form.fields[field_name + '.subscription_id'][0].value)
+        s = M.Mailbox.query.get(_id=s_id)
+        assert s, "User has not subscription with Mailbox._id = %s" % s_id
+        form.fields[field_name + '.subscribed'][0].value = None
+        form.submit()
+        s = M.Mailbox.query.get(_id=s_id)
+        assert not s, "User still has subscription with Mailbox._id %s" % s_id
+
     def test_api_key(self):
          r = self.app.get('/auth/prefs/')
          assert 'No API token generated' in r


Mime
View raw message