changeset 6362:24b61c257aab

forms: wrap LoginForm inside function like other forms All forms except LoginForm are wrapped inside a function. The original purpose of this wrapping seems to be the ability to pass parameters to tweak the form. But, this also has another desired effect: translation of strings wrapped with _ is no longer attempted when reading the class definition, but only when the function is instantiated. In the former case, it is not guaranteed that a translator is actually available because we are not running in standard application context. Align LoginForm with the others.
author Thomas De Schampheleire <thomas.de.schampheleire@gmail.com>
date Sat, 03 Dec 2016 21:56:54 +0100
parents d27077b9df45
children 81fdf2f62ef2
files kallithea/controllers/login.py kallithea/model/forms.py
diffstat 2 files changed, 24 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/controllers/login.py	Sun Dec 04 22:53:37 2016 +0100
+++ b/kallithea/controllers/login.py	Sat Dec 03 21:56:54 2016 +0100
@@ -87,7 +87,7 @@
 
         if request.POST:
             # import Login Form validator class
-            login_form = LoginForm()
+            login_form = LoginForm()()
             try:
                 c.form_result = login_form.to_python(dict(request.POST))
                 # form checks for username/password, now we're authenticated
--- a/kallithea/model/forms.py	Sun Dec 04 22:53:37 2016 +0100
+++ b/kallithea/model/forms.py	Sat Dec 03 21:56:54 2016 +0100
@@ -46,30 +46,32 @@
 log = logging.getLogger(__name__)
 
 
-class LoginForm(formencode.Schema):
-    allow_extra_fields = True
-    filter_extra_fields = True
-    username = v.UnicodeString(
-        strip=True,
-        min=1,
-        not_empty=True,
-        messages={
-           'empty': _('Please enter a login'),
-           'tooShort': _('Enter a value %(min)i characters long or more')}
-    )
+def LoginForm():
+    class _LoginForm(formencode.Schema):
+        allow_extra_fields = True
+        filter_extra_fields = True
+        username = v.UnicodeString(
+            strip=True,
+            min=1,
+            not_empty=True,
+            messages={
+               'empty': _('Please enter a login'),
+               'tooShort': _('Enter a value %(min)i characters long or more')}
+        )
 
-    password = v.UnicodeString(
-        strip=False,
-        min=3,
-        not_empty=True,
-        messages={
-            'empty': _('Please enter a password'),
-            'tooShort': _('Enter %(min)i characters or more')}
-    )
+        password = v.UnicodeString(
+            strip=False,
+            min=3,
+            not_empty=True,
+            messages={
+                'empty': _('Please enter a password'),
+                'tooShort': _('Enter %(min)i characters or more')}
+        )
 
-    remember = v.StringBoolean(if_missing=False)
+        remember = v.StringBoolean(if_missing=False)
 
-    chained_validators = [v.ValidAuth()]
+        chained_validators = [v.ValidAuth()]
+    return _LoginForm
 
 
 def PasswordChangeForm(username):