Commits

Anonymous committed ec2c58e

AccountManagerPlugin: Early alteration of `IAccountRegistrationInspector`, refs #874.

Better pass the data between implementations of the interface directly.

Comments (0)

Files changed (3)

accountmanagerplugin/trunk/acct_mgr/api.py

     new users from the user editor in AccountManagerAdminPanels too.
     """
 
-    def render_registration_fields(req):
+    def render_registration_fields(req, data):
         """Emit one or multiple additional fields for registration form built.
 
         Returns a dict containing a 'required' and/or 'optional' tuple of
          * Genshi Fragment or valid XHTML markup for registration form
-         * template data object with default values or empty dict
+         * modified or unchanged data object (used to render `register.html`)
         If the return value is just a single tuple, its fragment or markup
         will be inserted into the 'required' section.
         """

accountmanagerplugin/trunk/acct_mgr/register.py

 
     abstract = True
 
-    def render_registration_fields(self, req):
+    def render_registration_fields(self, req, data):
         """Emit one or multiple additional fields for registration form built.
 
         Returns a dict containing a 'required' and/or 'optional' tuple of 
          * Genshi Fragment or valid XHTML markup for registration form
-         * template data object with default values or empty dict
+         * modified or unchanged data object (used to render `register.html`)
         If the return value is just a single tuple, its fragment or markup
         will be inserted into the 'required' section.
         """
-        data = {}
         template = ''
         return template, data
 
     This check is bypassed, if account verification is disabled.
     """
 
-    def render_registration_fields(self, req):
+    def render_registration_fields(self, req, data):
         """Add an email address text input field to the registration form."""
         # Preserve last input for editing on failure instead of typing
         # everything again.
                            Entering your email address will also enable you
                            to reset your password if you ever forget it.
                            """), class_='hint'))
-            return tag(insert, hint), {}
+            return tag(insert, hint), data
         elif reset_password:
             # TRANSLATOR: Registration form hint, if email input is optional.
             hint = tag.p(_("""Entering your email address will enable you to
                            reset your password if you ever forget it. """),
                          class_='hint')
-            return dict(optional=tag(insert, hint)), {}
+            return dict(optional=tag(insert, hint)), data
         else:
             # Always return the email text input itself as optional field.
-            return dict(optional=insert), {}
+            return dict(optional=insert), data
 
     def validate_registration(self, req):
         acctmgr = AccountManager(self.env)
         fragments = dict(required=[], optional=[])
         for inspector in self.acctmgr._register_check:
             try:
-                fragment, f_data = inspector.render_registration_fields(req)
+                fragment, f_data = inspector.render_registration_fields(req,
+                                                                        data)
             except TypeError, e:
                 # Add some robustness by logging the most likely errors.
                 self.env.log.warn("%s.render_registration_fields failed: %s"

accountmanagerplugin/trunk/acct_mgr/tests/register.py

 
         check = BadRegistrationInspector(self.env)
         # Default (empty) response for providing additional fields is safe.
-        field_res = check.render_registration_fields(self.req)
+        field_res = check.render_registration_fields(self.req, {})
         self.assertEqual(len(field_res), 2)
         self.assertEqual((Markup(field_res[0]), field_res[1]),
                          (Markup(''), {}))
         check = BasicCheck(self.env)
         req = self.req
         # Inspector doesn't provide additional fields.
-        field_res = check.render_registration_fields(req)
+        field_res = check.render_registration_fields(req, {})
         self.assertEqual(len(field_res), 2)
         self.assertEqual((Markup(field_res[0]), field_res[1]),
                          (Markup(''), {}))
 
         # Inspector provides the email text input field.
         old_email_input = 'email@foo.bar'
-        req.args.update(dict(email=old_email_input))
-        field_res = check.render_registration_fields(req)
+        acct = dict(username='user', email=old_email_input, name='User')
+        req.args.update(acct)
+        field_res = check.render_registration_fields(req, acct)
         self.assertEqual(len(field_res), 2)
         self.assertTrue(Markup(field_res[0]).startswith('<label>Email:'))
-        # Make sure, that old input is preserved on failure.
+        # Ensure, that old input is restored on failure.
         self.assertTrue(old_email_input in Markup(field_res[0]))
-        self.assertEqual(field_res[1], {})
+        # Ensure, that template data dict is passed unchanged.
+        self.assertEqual(field_res[1], acct)
         req.args.update(dict(email=''))
 
         # 1st: Initially try with account verification disabled by setting.
         check = UsernamePermCheck(self.env)
         req = self.req
         # Inspector doesn't provide additional fields.
-        field_res = check.render_registration_fields(req)
+        field_res = check.render_registration_fields(req, {})
         self.assertEqual(len(field_res), 2)
         self.assertEqual((Markup(field_res[0]), field_res[1]),
                          (Markup(''), {}))
 
 class RegistrationModuleTestCase(_BaseTestCase):
     def setUp(self):
+        class DataChangeRegistrationInspector(GenericRegistrationInspector):
+            def render_registration_fields(self, req, data):
+                data['acctmgr']['email'] = 'default-user@foo.bar'
+                data.update(dict(acctmgr=dict(
+                    username=data['acctmgr'].get('username').upper())))
+                if 'name' in data['acctmgr']:
+                    del data['acctmgr']['name']
+                return '', data
+
+            def validate_registration(self, req):
+                # Don't check.
+                return
+
         _BaseTestCase.setUp(self)
         self.rmod = RegistrationModule(self.env)
         self.reg_template = 'register.html'
         self.req.method = 'POST'
 
+        self.check = DataChangeRegistrationInspector(self.env)
+
     def test_check(self):
         # Default configuration: All provided checks enabled.
         response = self.rmod.process_request(self.req)
         response = self.rmod.process_request(self.req)
         self.assertEqual(response[0], self.reg_template)
 
+    def test_data_update(self):
+        self.req.args['username'] = 'UseR'
+        self.req.args['email'] = 'user@foo.bar'
+        self.env.config.set('account-manager', 'register_check',
+                            'DataChangeRegistrationInspector')
+        response = self.rmod.process_request(self.req)
+        print(response)
+        print(AccountManager(self.env)._register_check)
+        # Added content.
+        self.assertEqual(response[1]['acctmgr']['email'],
+                         'default-user@foo.bar')
+        # Updated content.
+        self.assertEqual(response[1]['acctmgr']['username'], 'USER')
+        # Deleted content.
+        self.assertFalse('name' in response[1]['acctmgr'])
+
 
 def suite():
     suite = unittest.TestSuite()