changeset 2368:5fc0717a060f

refactor userprefs for more consistent UI
author Johannes Berg <johannes AT sipsolutions DOT net>
date Thu, 12 Jul 2007 11:41:09 +0200
parents 405032c82324
children ccf996479233
files MoinMoin/userprefs/__init__.py MoinMoin/userprefs/changepass.py MoinMoin/userprefs/prefs.py MoinMoin/userprefs/suid.py
diffstat 4 files changed, 69 insertions(+), 117 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/userprefs/__init__.py	Thu Jul 12 11:40:52 2007 +0200
+++ b/MoinMoin/userprefs/__init__.py	Thu Jul 12 11:41:09 2007 +0200
@@ -8,6 +8,7 @@
     @license: GNU GPL, see COPYING for details.
 """
 from MoinMoin.util import pysupport
+from MoinMoin.widget import html
 
 
 # create a list of extension actions from the package directory
@@ -30,7 +31,8 @@
         '''
         self.request = request
         self._ = request.getText
-        title = 'No name set'
+        self.name = None
+        self.title = 'No name set'
 
     def create_form(self):
         '''
@@ -63,3 +65,38 @@
             submissions are accepted.
         '''
         return self.request.user and self.request.user.valid
+
+    def make_form(self, explanation=None):
+        '''
+            To have a consistent UI, use this method for most
+            preferences forms and then call make_row(). See
+            existing plugins, e.g. changepass.py.
+        '''
+        action = self.request.page.url(self.request)
+        _form = html.FORM(action=action)
+        _form.append(html.INPUT(type="hidden", name="action", value="userprefs"))
+        _form.append(html.INPUT(type="hidden", name="handler", value=self.name))
+
+        self._table = html.TABLE(border="0")
+
+        # Use the user interface language and direction
+        lang_attr = self.request.theme.ui_lang_attr()
+        _form.append(html.Raw('<div class="userpref"%s>' % lang_attr))
+        para = html.P()
+        _form.append(para)
+        if explanation:
+            para.append(explanation)
+
+        para.append(self._table)
+        _form.append(html.Raw("</div>"))
+
+        return _form
+
+    def make_row(self, label, cell, **kw):
+        '''
+           Create a row in the form table.
+        '''
+        self._table.append(html.TR().extend([
+            html.TD(**kw).extend([html.B().append(label), '   ']),
+            html.TD().extend(cell),
+        ]))
--- a/MoinMoin/userprefs/changepass.py	Thu Jul 12 11:40:52 2007 +0200
+++ b/MoinMoin/userprefs/changepass.py	Thu Jul 12 11:41:09 2007 +0200
@@ -23,6 +23,7 @@
         _ = request.getText
         self.cfg = request.cfg
         self.title = _("Change password")
+        self.name = 'changepass'
 
 
     def allowed(self):
@@ -60,60 +61,22 @@
                 return "Can't encode password: %s" % str(err)
 
 
-    def _make_form(self):
-        """ Create the FORM, and the TABLE with the input fields
-        """
-        _ = self._
-        sn = self.request.getScriptname()
-        pi = self.request.getPathinfo()
-        action = u"%s%s" % (sn, pi)
-        self._form = html.FORM(action=action)
-        self._table = html.TABLE(border="0")
-
-        # Use the user interface language and direction
-        lang_attr = self.request.theme.ui_lang_attr()
-        self._form.append(html.Raw('<div class="userpref"%s>' % lang_attr))
-
-        self._form.append(html.STRONG().append(html.P().append(html.Text(
-            _("To change your password, enter a new password twice.")))))
-
-        self._form.append(self._table)
-        self._form.append(html.Raw("</div>"))
-
-
-    def _make_row(self, label, cell, **kw):
-        """ Create a row in the form table.
-        """
-        self._table.append(html.TR().extend([
-            html.TD(**kw).extend([html.B().append(label), '   ']),
-            html.TD().extend(cell),
-        ]))
-
-
     def create_form(self, create_only=False, recover_only=False):
         """ Create the complete HTML form code. """
         _ = self._
-        self._make_form()
+        form = self.make_form(html.Text(_("To change your password, "
+                                          "enter a new password twice.")))
 
-        self._make_row(_('Password'),
-                       [html.INPUT(type="password", size=36, name="password")])
-        self._make_row(_('Password repeat'),
-                       [html.INPUT(type="password", size=36, name="password2")])
-
-        self._form.append(html.INPUT(type="hidden", name="action",
-                                     value="userprefs"))
-        self._form.append(html.INPUT(type="hidden", name="handler",
-                                     value="changepass"))
+        self.make_row(_('Password'),
+                      [html.INPUT(type="password", size=36, name="password")])
+        self.make_row(_('Password repeat'),
+                      [html.INPUT(type="password", size=36, name="password2")])
 
         # Add buttons
-        self._form.append(html.INPUT(type="hidden", name="action",
-                                     value="userprefs"))
-        self._form.append(html.INPUT(type="hidden", name="handler",
-                                     value="changepass"))
-
-        self._make_row('', [
-                html.INPUT(type="submit", name='save', value=_("Save")),
+        self.make_row('', [
+                html.INPUT(type="submit", name='save', value=_("Change password")),
+                ' ',
                 html.INPUT(type="submit", name='cancel', value=_("Cancel")),
               ])
 
-        return unicode(self._form)
+        return unicode(form)
--- a/MoinMoin/userprefs/prefs.py	Thu Jul 12 11:40:52 2007 +0200
+++ b/MoinMoin/userprefs/prefs.py	Thu Jul 12 11:41:09 2007 +0200
@@ -35,6 +35,7 @@
         self._ = request.getText
         self.cfg = request.cfg
         self.title = self._("Preferences")
+        self.name = 'prefs'
 
     def _decode_pagelist(self, key):
         """ Decode list of pages from form input
@@ -327,36 +328,11 @@
                   ]
         return util.web.makeSelection('editor_ui', options, editor_ui)
 
-    def _make_form(self):
-        """ Create the FORM, and the TABLE with the input fields
-        """
-        sn = self.request.getScriptname()
-        pi = self.request.getPathinfo()
-        action = u"%s%s" % (sn, pi)
-        self._form = html.FORM(action=action)
-        self._table = html.TABLE(border="0")
-
-        # Use the user interface language and direction
-        lang_attr = self.request.theme.ui_lang_attr()
-        self._form.append(html.Raw('<div class="userpref"%s>' % lang_attr))
-
-        self._form.append(self._table)
-        self._form.append(html.Raw("</div>"))
-
-
-    def make_row(self, label, cell, **kw):
-        """ Create a row in the form table.
-        """
-        self._table.append(html.TR().extend([
-            html.TD(**kw).extend([html.B().append(label), '   ']),
-            html.TD().extend(cell),
-        ]))
-
 
     def create_form(self):
         """ Create the complete HTML form code. """
         _ = self._
-        self._make_form()
+        self._form = self.make_form()
 
         if self.request.user.valid:
             buttons = [('save', _('Save')), ('cancel', _('Cancel')), ]
--- a/MoinMoin/userprefs/suid.py	Thu Jul 12 11:40:52 2007 +0200
+++ b/MoinMoin/userprefs/suid.py	Thu Jul 12 11:41:09 2007 +0200
@@ -20,13 +20,13 @@
         self._ = request.getText
         self.cfg = request.cfg
         self.title = self._("Switch user")
+        self.name = 'suid'
 
-    def _is_super_user(self):
-        return (self.request.user.isSuperUser() or
-                (not self.request._setuid_real_user is None
-                 and (self.request._setuid_real_user.isSuperUser())))
-
-    allowed = _is_super_user
+    def allowed(self):
+        return (UserPrefBase.allowed(self) and
+                self.request.user.isSuperUser() or
+                (not self.request._setuid_real_user is None and
+                 (self.request._setuid_real_user.isSuperUser())))
 
     def handle_form(self):
         _ = self._
@@ -36,7 +36,7 @@
             return
 
         if (wikiutil.checkTicket(self.request, self.request.form['ticket'][0]) and
-            self.request.request_method == 'POST' and self._is_super_user()):
+            self.request.request_method == 'POST'):
             su_user = form.get('selected_user', [''])[0]
             uid = user.getUserId(self.request, su_user)
             if not uid:
@@ -76,42 +76,18 @@
         current_user = self.request.user.name
         return util.web.makeSelection('selected_user', options, current_user, size=size)
 
-    def _make_form(self):
-        """ Create the FORM, and the TABLE with the input fields
-        """
-        sn = self.request.getScriptname()
-        pi = self.request.getPathinfo()
-        action = u"%s%s" % (sn, pi)
-        self._form = html.FORM(action=action)
-
-        # Use the user interface language and direction
-        lang_attr = self.request.theme.ui_lang_attr()
-        self._form.append(html.Raw('<div class="userpref"%s>' % lang_attr))
-
-        self._form.append(html.INPUT(type="hidden", name="action", value="userprefs"))
-        self._form.append(html.INPUT(type="hidden", name="handler", value="suid"))
-        self._form.append(html.Raw("</div>"))
-
-
     def create_form(self):
         """ Create the complete HTML form code. """
         _ = self._
-        self._make_form()
+        form = self.make_form(html.Text(_('As a superuser, you can temporarily '
+                                          'assume the identity of another user.')))
 
-        if (self.request.user.isSuperUser() or
-            (not self.request._setuid_real_user is None and
-             self.request._setuid_real_user.isSuperUser())):
-            ticket = wikiutil.createTicket(self.request)
-            self._form.append(html.P().append(
-                              html.Text(_('As the superuser, you can temporarily '
-                                          'assume the identity of another user.'))))
-            self._form.append(html.P().append(self._user_select()))
-            self._form.append(html.INPUT(type="hidden", name="ticket", value="%s" % ticket))
-            self._form.append(html.INPUT(type="submit", name="select_user",
-                                         value=_('Select User')))
-            self._form.append(html.Text(' '))
-            self._form.append(html.INPUT(type="submit", name="cancel",
-                                         value=_('Cancel')))
-            return unicode(self._form)
-
-        return u''
+        ticket = wikiutil.createTicket(self.request)
+        self.make_row('Select User', [self._user_select()], valign="top")
+        form.append(html.INPUT(type="hidden", name="ticket", value="%s" % ticket))
+        self.make_row('', [html.INPUT(type="submit", name="select_user",
+                                      value=_('Select User')),
+                           ' ',
+                           html.INPUT(type="submit", name="cancel",
+                                      value=_('Cancel'))])
+        return unicode(form)