You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by je...@apache.org on 2014/09/17 15:34:10 UTC

[23/35] git commit: [#7657] ticket:651 Refactor update emails controller/page to reuse in site admin UI

[#7657] ticket:651 Refactor update emails controller/page to reuse in site admin UI


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

Branch: refs/heads/je/42cc_7657
Commit: 6adf0aca3870e10b8e6aa04ffc60fbad774d5f21
Parents: b555320
Author: Igor Bondarenko <je...@gmail.com>
Authored: Tue Sep 16 13:18:58 2014 +0300
Committer: Igor Bondarenko <je...@gmail.com>
Committed: Wed Sep 17 16:11:37 2014 +0300

----------------------------------------------------------------------
 Allura/allura/controllers/auth.py               | 120 ++++++++++---------
 Allura/allura/templates/update_emails_form.html |  68 +++++++++++
 Allura/allura/templates/user_prefs.html         | 102 +++++-----------
 3 files changed, 159 insertions(+), 131 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/allura/blob/6adf0aca/Allura/allura/controllers/auth.py
----------------------------------------------------------------------
diff --git a/Allura/allura/controllers/auth.py b/Allura/allura/controllers/auth.py
index 1f8c8aa..8d254bb 100644
--- a/Allura/allura/controllers/auth.py
+++ b/Allura/allura/controllers/auth.py
@@ -432,73 +432,81 @@ class PreferencesController(BaseController):
         menu = provider.account_navigation()
         return dict(menu=menu)
 
+    def _update_emails(self, **kw):
+        addr = kw.pop('addr', None)
+        new_addr= kw.pop('new_addr', None)
+        primary_addr = kw.pop('primary_addr', None)
+        oid = kw.pop('oid', None)
+        new_oid = kw.pop('new_oid', None)
+        provider = plugin.AuthenticationProvider.get(request)
+        for i, (old_a, data) in enumerate(zip(c.user.email_addresses, addr or [])):
+            obj = c.user.address_object(old_a)
+            if data.get('delete') or not obj:
+                if not kw.get('password') or not provider.validate_password(c.user, kw.get('password')):
+                    flash('You must provide your current password to delete an email', 'error')
+                    return
+                if primary_addr == c.user.email_addresses[i]:
+                    if select_new_primary_addr(c.user, ignore_emails=primary_addr) is None \
+                            and asbool(config.get('auth.require_email_addr', False)):
+                        flash('You must have at least one verified email address.', 'error')
+                        return
+                    else:
+                        # clear it now, a new one will get set below
+                        c.user.set_pref('email_address', None)
+                        primary_addr = None
+                h.auditlog_user('Email address deleted: %s', c.user.email_addresses[i])
+                del c.user.email_addresses[i]
+                if obj:
+                    obj.delete()
+        if new_addr.get('claim') or new_addr.get('addr'):
+            if not kw.get('password') or not provider.validate_password(c.user, kw.get('password')):
+                flash('You must provide your current password to claim new email', 'error')
+                return
+            if M.EmailAddress.query.get(email=new_addr['addr'], confirmed=True) \
+                    or M.EmailAddress.query.get(email=new_addr['addr'], claimed_by_user_id=c.user._id):
+                flash('Email address already claimed', 'error')
+            elif mail_util.isvalid(new_addr['addr']):
+                c.user.email_addresses.append(new_addr['addr'])
+                em = M.EmailAddress.create(new_addr['addr'])
+                em.claimed_by_user_id = c.user._id
+                em.send_verification_link()
+                h.auditlog_user('New email address: %s', new_addr['addr'])
+                flash('A verification email has been sent.  Please check your email and click to confirm.')
+            else:
+                flash('Email address %s is invalid' % new_addr['addr'], 'error')
+        if not primary_addr and not c.user.get_pref('email_address') and c.user.email_addresses:
+            primary_addr = select_new_primary_addr(c.user)
+        if primary_addr:
+            if c.user.get_pref('email_address') != primary_addr:
+                if not kw.get('password') or not provider.validate_password(c.user, kw.get('password')):
+                    flash('You must provide your current password to change primary address', 'error')
+                    return
+                h.auditlog_user(
+                    'Primary email changed: %s => %s',
+                    c.user.get_pref('email_address'),
+                    primary_addr)
+            c.user.set_pref('email_address', primary_addr)
+
+    @h.vardec
+    @expose()
+    @require_post()
+    def update_emails(self, **kw):
+        if asbool(config.get('auth.allow_edit_prefs', True)):
+            self._update_emails(**kw)
+        redirect('.')
+
     @h.vardec
     @expose()
     @require_post()
-    def update(self,
-               addr=None,
-               new_addr=None,
-               primary_addr=None,
-               oid=None,
-               new_oid=None,
-               preferences=None,
-               **kw):
+    def update(self, preferences=None, **kw):
         if asbool(config.get('auth.allow_edit_prefs', True)):
             if not preferences.get('display_name'):
                 flash("Display Name cannot be empty.", 'error')
                 redirect('.')
-            provider = plugin.AuthenticationProvider.get(request)
             old = c.user.get_pref('display_name')
             c.user.set_pref('display_name', preferences['display_name'])
             if old != preferences['display_name']:
                 h.auditlog_user('Display Name changed %s => %s', old, preferences['display_name'])
-            for i, (old_a, data) in enumerate(zip(c.user.email_addresses, addr or [])):
-                obj = c.user.address_object(old_a)
-                if data.get('delete') or not obj:
-                    if not kw.get('password') or not provider.validate_password(c.user, kw.get('password')):
-                        flash('You must provide your current password to delete an email', 'error')
-                        redirect('.')
-                    if primary_addr == c.user.email_addresses[i]:
-                        if select_new_primary_addr(c.user, ignore_emails=primary_addr) is None \
-                                and asbool(config.get('auth.require_email_addr', False)):
-                            flash('You must have at least one verified email address.', 'error')
-                            redirect('.')
-                        else:
-                            # clear it now, a new one will get set below
-                            c.user.set_pref('email_address', None)
-                            primary_addr = None
-                    h.auditlog_user('Email address deleted: %s', c.user.email_addresses[i])
-                    del c.user.email_addresses[i]
-                    if obj:
-                        obj.delete()
-            if new_addr.get('claim') or new_addr.get('addr'):
-                if not kw.get('password') or not provider.validate_password(c.user, kw.get('password')):
-                    flash('You must provide your current password to claim new email', 'error')
-                    redirect('.')
-                if M.EmailAddress.query.get(email=new_addr['addr'], confirmed=True) \
-                        or M.EmailAddress.query.get(email=new_addr['addr'], claimed_by_user_id=c.user._id):
-                    flash('Email address already claimed', 'error')
-                elif mail_util.isvalid(new_addr['addr']):
-                    c.user.email_addresses.append(new_addr['addr'])
-                    em = M.EmailAddress.create(new_addr['addr'])
-                    em.claimed_by_user_id = c.user._id
-                    em.send_verification_link()
-                    h.auditlog_user('New email address: %s', new_addr['addr'])
-                    flash('A verification email has been sent.  Please check your email and click to confirm.')
-                else:
-                    flash('Email address %s is invalid' % new_addr['addr'], 'error')
-            if not primary_addr and not c.user.get_pref('email_address') and c.user.email_addresses:
-                primary_addr = select_new_primary_addr(c.user)
-            if primary_addr:
-                if c.user.get_pref('email_address') != primary_addr:
-                    if not kw.get('password') or not provider.validate_password(c.user, kw.get('password')):
-                        flash('You must provide your current password to change primary address', 'error')
-                        redirect('.')
-                    h.auditlog_user(
-                        'Primary email changed: %s => %s',
-                        c.user.get_pref('email_address'),
-                        primary_addr)
-                c.user.set_pref('email_address', primary_addr)
             for k, v in preferences.iteritems():
                 if k == 'results_per_page':
                     v = int(v)

http://git-wip-us.apache.org/repos/asf/allura/blob/6adf0aca/Allura/allura/templates/update_emails_form.html
----------------------------------------------------------------------
diff --git a/Allura/allura/templates/update_emails_form.html b/Allura/allura/templates/update_emails_form.html
new file mode 100644
index 0000000..20aecce
--- /dev/null
+++ b/Allura/allura/templates/update_emails_form.html
@@ -0,0 +1,68 @@
+{#-
+       Licensed to the Apache Software Foundation (ASF) under one
+       or more contributor license agreements.  See the NOTICE file
+       distributed with this work for additional information
+       regarding copyright ownership.  The ASF licenses this file
+       to you under the Apache License, Version 2.0 (the
+       "License"); you may not use this file except in compliance
+       with the License.  You may obtain a copy of the License at
+
+         http://www.apache.org/licenses/LICENSE-2.0
+
+       Unless required by applicable law or agreed to in writing,
+       software distributed under the License is distributed on an
+       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+       KIND, either express or implied.  See the License for the
+       specific language governing permissions and limitations
+       under the License.
+-#}
+<form action="update_emails" method="post" name="update-email">
+  {% for a in c.user.email_addresses %}
+    <input name="addr-{{loop.index0}}.ord" value="{{loop.index0}}" type="hidden"/>
+  {% endfor %}
+  <table class="grid-22">
+    <tr>
+      <th>Primary?</th>
+      <th>Address</th>
+      <th>Confirmed</th>
+      <th></th>
+    </tr>
+    {% for a in c.user.email_addresses %}
+    <tr>
+      {% set obj = c.user.address_object(a) %}
+      {% if obj.confirmed %}
+        <td>{{lib.radio_button('primary_addr', None, a, c.user.preferences.email_address)}}</td>
+      {% else %}
+        <td> <input type="radio" disabled="disabled"></td>
+      {% endif %}
+      <td>{{a}}</td>
+      {% if obj %}
+      <td>
+        {% if obj.confirmed %}
+          yes
+        {% else %}
+          no (<a href="/auth/send_verification_link?a={{a}}">verify</a>)
+        {% endif %}
+      </td>
+      {% else %}
+        <td>Unknown addr obj {{a}}</td>
+      {% endif %}
+      <td>
+        <div class="addr-delete">
+          {{lib.submit_button('Delete', 'addr-%s.delete' % loop.index0)}}
+          {{lib.hidden_field('addr-%s.delete' % loop.index0, '')}}
+        </div>
+      </td>
+    </tr>
+    {% endfor %}
+    <tr>
+      <td colspan="2">{{lib.text_field('new_addr.addr', '')}}</td>
+      <td colspan="2">{{lib.submit_button('Claim New Address', name='new_addr.claim')}}</td>
+    </tr>
+  </table>
+  <div class="grid-22">
+    {{lib.submit_button('Save', name='addr-save')}}
+  </div>
+  {{lib.hidden_field('password', '')}}
+  {{lib.csrf_token()}}
+</form>

http://git-wip-us.apache.org/repos/asf/allura/blob/6adf0aca/Allura/allura/templates/user_prefs.html
----------------------------------------------------------------------
diff --git a/Allura/allura/templates/user_prefs.html b/Allura/allura/templates/user_prefs.html
index 253174f..a19ae4e 100644
--- a/Allura/allura/templates/user_prefs.html
+++ b/Allura/allura/templates/user_prefs.html
@@ -41,84 +41,33 @@
 
     {% block edit_prefs_form %}
       {% if h.asbool(tg.config.get('auth.allow_edit_prefs', True)) %}
-      <form action="update" method="post" name="update-email">
         <fieldset>
-          <legend>General and Email Settings</legend>
-          <label class="grid-4">Display Name</label>
-          <div class="grid-18">
-            <input name="preferences.display_name" value="{{c.user.display_name}}" type="text">
-          </div>
-          <label class="grid-4">Page Size</label>
-          <div class="grid-18">
-            <select name="preferences.results_per_page">
-              {% for per_page in [25, 50, 100, 250] %}
-                  <option {% if per_page == c.user.preferences.results_per_page %}selected="selected"{% endif %}
-                     value="{{per_page}}">{{per_page}}</option>
-              {% endfor %}
-            </select>
-          </div>
+          <legend>General Settings</legend>
+          <form action="update" method="POST">
+            <label class="grid-4">Display Name</label>
+            <div class="grid-18">
+              <input name="preferences.display_name" value="{{c.user.display_name}}" type="text">
+            </div>
+            <label class="grid-4">Page Size</label>
+            <div class="grid-18">
+              <select name="preferences.results_per_page">
+                {% for per_page in [25, 50, 100, 250] %}
+                    <option {% if per_page == c.user.preferences.results_per_page %}selected="selected"{% endif %}
+                       value="{{per_page}}">{{per_page}}</option>
+                {% endfor %}
+              </select>
+            </div>
+            <div class="grid-22">
+              {{lib.submit_button('Save')}}
+            </div>
+            {{lib.csrf_token()}}
+          </form>
+        </fieldset>
 
-          {% for a in c.user.email_addresses %}
-            <input name="addr-{{loop.index0}}.ord" value="{{loop.index0}}" type="hidden"/>
-          {% endfor %}
-          {#
-           # This is a hidden copy of a 'Save' submit button.
-           # We need this because form uses several submit buttons, and
-           # if user presses 'Enter' in one of the fields, browser chooses *first* submit button.
-           # In the case when user has at least one email address, first button is delete button
-           # for first email address. So user ends up deleting their first email address,
-           # instead of changing display name, for example.
-           #}
-          {{lib.submit_button('Save', style='display:none')}}
-          <hr>
-          <h3>Email Addresses</h3>
-          <table class="grid-22">
-            <tr>
-              <th>Primary?</th>
-              <th>Address</th>
-              <th>Confirmed</th>
-              <th></th>
-            </tr>
-            {% for a in c.user.email_addresses %}
-            <tr>
-              {% set obj = c.user.address_object(a) %}
-              {% if obj.confirmed %}
-                <td>{{lib.radio_button('primary_addr', None, a, c.user.preferences.email_address)}}</td>
-              {% else %}
-                <td> <input type="radio" disabled="disabled"></td>
-              {% endif %}
-              <td>{{a}}</td>
-              {% if obj %}
-              <td>
-                {% if obj.confirmed %}
-                  yes
-                {% else %}
-                  no (<a href="/auth/send_verification_link?a={{a}}">verify</a>)
-                {% endif %}
-              </td>
-              {% else %}
-                <td>Unknown addr obj {{a}}</td>
-              {% endif %}
-              <td>
-                <div class="addr-delete">
-                  {{lib.submit_button('Delete', 'addr-%s.delete' % loop.index0)}}
-                  {{lib.hidden_field('addr-%s.delete' % loop.index0, '')}}
-                </div>
-              </td>
-            </tr>
-            {% endfor %}
-            <tr>
-              <td colspan="2">{{lib.text_field('new_addr.addr', '')}}</td>
-              <td colspan="2">{{lib.submit_button('Claim New Address', name='new_addr.claim')}}</td>
-            </tr>
-          </table>
-          <div class="grid-22">
-            {{lib.submit_button('Save', name='addr-save')}}
-          </div>
-          {{lib.hidden_field('password', '')}}
-          {{lib.csrf_token()}}
+        <fieldset>
+          <legend>Email addresses</legend>
+          {% include 'allura:templates/update_emails_form.html' %}
         </fieldset>
-      </form>
 
       <!-- popup -->
       <form class="cur_password" style="display:none">
@@ -190,6 +139,8 @@
 {% endblock %}
 
 {% block extra_js %}
+  {% if h.asbool(tg.config.get('auth.allow_edit_prefs', True)) %}
+  {# js to ask for a current password on the email form #}
   <script type="text/javascript">
       $(function() {
 
@@ -238,4 +189,5 @@
         });
       });
   </script>
+  {% endif %}
  {% endblock %}