Changeset - 24b61c257aab
[Not reviewed]
default
0 2 0
Thomas De Schampheleire - 9 years ago 2016-12-03 21:56:54
thomas.de.schampheleire@gmail.com
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.
2 files changed with 24 insertions and 22 deletions:
0 comments (0 inline, 0 general)
kallithea/controllers/login.py
Show inline comments
 
@@ -78,25 +78,25 @@ class LoginController(BaseController):
 
                raise HTTPBadRequest()
 
        else:
 
            c.came_from = url('home')
 

	
 
        ip_allowed = AuthUser.check_ip_allowed(self.authuser, self.ip_addr)
 

	
 
        # redirect if already logged in
 
        if self.authuser.is_authenticated and ip_allowed:
 
            raise HTTPFound(location=c.came_from)
 

	
 
        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
 
                username = c.form_result['username']
 
                user = User.get_by_username_or_email(username, case_insensitive=True)
 
            except formencode.Invalid as errors:
 
                defaults = errors.value
 
                # remove password from filling in form again
 
                defaults.pop('password', None)
 
                return htmlfill.render(
 
                    render('/login.html'),
 
                    defaults=errors.value,
kallithea/model/forms.py
Show inline comments
 
@@ -37,48 +37,50 @@ import logging
 

	
 
import formencode
 
from formencode import All
 

	
 
from pylons.i18n.translation import _
 

	
 
from kallithea import BACKENDS
 
from kallithea.model import validators as v
 

	
 
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):
 
    class _PasswordChangeForm(formencode.Schema):
 
        allow_extra_fields = True
 
        filter_extra_fields = True
 

	
 
        current_password = v.ValidOldPassword(username)(not_empty=True)
 
        new_password = All(v.ValidPassword(), v.UnicodeString(strip=False, min=6))
 
        new_password_confirmation = All(v.ValidPassword(), v.UnicodeString(strip=False, min=6))
 

	
 
        chained_validators = [v.ValidPasswordsMatch('new_password',
0 comments (0 inline, 0 general)