[views, wip] Add register view, with extra fields. Fix i18n accordingly (concerns #3020518).
authorVladimir Popescu <vladimir.popescu@logilab.fr>
Wed, 17 Jul 2013 14:20:03 +0200
changeset 314 7bef71b7fff3
parent 313 e539166c4e6d
child 319 321e13aa96c0
[views, wip] Add register view, with extra fields. Fix i18n accordingly (concerns #3020518).
views/register.py
views/secondary.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/views/register.py	Wed Jul 17 14:20:03 2013 +0200
@@ -0,0 +1,142 @@
+# -*- coding: utf-8 -*-
+# copyright 2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+# contact http://www.logilab.fr -- mailto:contact@logilab.fr
+# copyright 2013 CEA (Saclay, FRANCE), all rights reserved.
+#
+# This program is free software: you can redistribute it and/or modify it under
+# the terms of the GNU Lesser General Public License as published by the Free
+# Software Foundation, either version 2.1 of the License, or (at your option)
+# any later version.
+#
+# This program is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
+# details.
+#
+# You should have received a copy of the GNU Lesser General Public License along
+# with this program. If not, see <http://www.gnu.org/licenses/>.
+
+"""cubicweb-brainomics views/forms/actions/components for web ui"""
+from cubicweb import crypto
+
+from logilab.common.decorators import clear_cache
+from logilab.mtconverter import xml_escape
+
+from cubicweb.web import ValidationError, Redirect
+from cubicweb.web import formwidgets as fw, formfields as ff
+
+from cubes.registration.views import (RegistrationFormView,
+                                      RegistrationForm,
+                                      RegistrationSendMailController,
+                                      RegistrationConfirmController, qname)
+
+
+###############################################################################
+### REGISTRATION VIEW #########################################################
+###############################################################################
+class BrainomicsRegistrationFormView(RegistrationFormView):
+
+    def call(self):
+        # Redefine the RegistrationView to include (if existing)
+        # the license of the data
+        super(BrainomicsRegistrationFormView, self).call()
+        card = self._cw.execute('Any X WHERE X is Card, X title %(t)s', {'t': 'license'})
+        if card:
+            msg = self._cw._(u'By signing up to this site you implicitly accept its '
+                             'terms and conditions, which are stated '
+                             'in the License agreement shown below')
+            self.w(u'<span class="badge badge-important">%s</span>' % xml_escape(msg))
+            self.w(u'<div>%s</div>' % card.get_entity(0, 0).content)
+
+
+###############################################################################
+### REGISTRATION FORMS ########################################################
+###############################################################################
+class BrainomicsRegistrationForm(RegistrationForm):
+    affiliation = ff.StringField(widget=fw.TextInput(), role='subject',
+                                label=_('affiliation'),
+                                help=_('Please provide affiliation information'),
+                                required=True)
+
+    extra_infos = ff.StringField(widget=fw.TextInput(), role='subject',
+                                 label=_('extra infos'),
+                                 help=_('Provide additional information if desired'))
+
+    acceptance = ff.BooleanField(widget=fw.Radio(), role='subject',
+                                 label=_('license acceptance'),
+                                 help=_('Please click to accept license (see below)'),
+                                 required=True)
+
+
+class BrainomicsRegistrationSendMailController(RegistrationSendMailController):
+
+    def checked_data(self):
+        data = super(BrainomicsRegistrationSendMailController, self).checked_data()
+        if not data.get(qname('acceptance')):
+            license_error = self._cw._('License error')
+            no_license = xml_escape(self._cw._('To register, you have to accept the license'))
+            raise ValidationError(None, {'License error': no_license})
+        return data
+
+
+###############################################################################
+### REGISTRATION CONTROLLER ###################################################
+###############################################################################
+class BrainomicsRegistrationConfirmController(RegistrationConfirmController):
+
+    # Overridden to add the ``affiliation`` and ``extra_infos`` kwargs to ``register_user`` call
+    def publish(self, rset=None):
+        req = self._cw
+        try:
+            data = crypto.decrypt(req.form['key'],
+                                  req.vreg.config['registration-cypher-seed'])
+            login = data[qname('login')]
+            password = data.pop(qname('upassword'))
+        except:
+            msg = req._(u'Invalid registration data. Please try registering again.')
+            raise Redirect(req.build_url(u'register', __message=msg))
+        if self._cw.user.login == login:
+            # already logged in (e.g. regstration link replayed twice in the browser)
+            raise Redirect(self.success_redirect_url())
+        req.form = data # hijack for proper validation error handling
+        err_raised = False
+        try:
+            self.appli.repo.register_user(login, password,
+                                          email=data.get(qname('email_address')),
+                                          firstname=data.get(qname('firstname')),
+                                          surname=data.get(qname('surname')),
+                                          affiliation=data.get(qname('affiliation')),
+                                          extra_infos=data.get(qname('extra_infos')))
+        except ValidationError, err:
+            err_raised = True
+            if err.errors.keys() != ['login-subject']:
+                raise
+        # force new authentication (anon until there)
+        clear_cache(req, 'get_authorization')
+        req.form['__login'] = login
+        req.form['__password'] = password
+        if req.cnx:
+            req.cnx.close()
+        req.cnx = None
+        try:
+            self.appli.session_handler.set_session(req)
+        except Redirect:
+            pass
+        if req.user.login != 'login' and err_raised:
+            # if both authentication and register_user() failed, the problem
+            # is probably that we do have a true login conflict, just re-raise
+            # the original validation error
+            raise
+        assert req.user.login == login
+        raise Redirect(self.success_redirect_url())
+
+
+
+
+def registration_callback(vreg):
+    vreg.register_and_replace(BrainomicsRegistrationForm, RegistrationForm)
+    vreg.register_and_replace(BrainomicsRegistrationSendMailController,
+                              RegistrationSendMailController)
+    vreg.register_and_replace(BrainomicsRegistrationConfirmController,
+                              RegistrationConfirmController)
+    vreg.register_and_replace(BrainomicsRegistrationFormView, RegistrationFormView)
--- a/views/secondary.py	Wed Jul 17 13:57:38 2013 +0200
+++ b/views/secondary.py	Wed Jul 17 14:20:03 2013 +0200
@@ -21,8 +21,6 @@
 from cubicweb.view import EntityView
 from cubicweb.web.views.baseviews import ListView
 
-from cubes.registration.views import RegistrationFormView
-
 
 ###############################################################################
 ### LIST VIEW #################################################################
@@ -54,28 +52,7 @@
                 w(entity.content)
         w(u'</div>')
 
-
-###############################################################################
-### REGISTRATION VIEW #########################################################
-###############################################################################
-class BrainomicsRegistrationFormView(RegistrationFormView):
+def registration_callback(vreg):
+    vreg.register_all(globals().values(), __name__, (BrainomicsListView,))
+    vreg.register_and_replace(BrainomicsListView, ListView)
 
-    def call(self):
-        # Redefine the RegistrationView to include (if existing)
-        # the license of the data
-        super(BrainomicsRegistrationFormView, self).call()
-        card = self._cw.execute('Any X WHERE X is Card, X title %(t)s', {'t': 'license'})
-        if card:
-            msg = self._cw._(u'By signing up to this site you implicitly accept its '
-                             'terms and conditions, which are stated '
-                             'in the License agreement shown below')
-            self.w(u'<span class="badge badge-important">%s</span>' % xml_escaoe(msg))
-            self.w(u'<div>%s</div>' % card.get_entity(0, 0).content)
-
-
-def registration_callback(vreg):
-    vreg.register_all(globals().values(), __name__, (BrainomicsListView,
-                                                     BrainomicsRegistrationFormView))
-    vreg.register_and_replace(BrainomicsListView, ListView)
-    vreg.register_and_replace(BrainomicsRegistrationFormView, RegistrationFormView)
-