diff --git a/src/users/forms.py b/src/users/forms.py index 6a8cb328..6c23c859 100644 --- a/src/users/forms.py +++ b/src/users/forms.py @@ -142,45 +142,32 @@ def __init__(self, *args, **kwargs): self.fields["username"].widget.attrs["placeholder"] = _("username / email") self.fields["password"].widget.attrs["placeholder"] = _("password") - def clean_username(self): - username_or_email = self.cleaned_data.get("username") + def clean(self): + cleaned_data = super(LoginForm, self).clean() + + username_or_email = cleaned_data.get("username", "") + search_by = {} + if "@" in username_or_email: - if not User.objects.filter(email=username_or_email).exists(): - raise forms.ValidationError(_("Username/email not found")) - user = User.objects.get(email=username_or_email) - if user: - return user.username + search_by["email"] = username_or_email else: - if not User.objects.filter(username=username_or_email).exists(): - raise forms.ValidationError(_("Username/email not found")) + search_by["username"] = username_or_email - # Already is a valid username - return username_or_email + user = User.objects.get(**search_by) + if not user: + raise forms.ValidationError(_("Username/email not found")) - def clean_password(self): - password = self.cleaned_data.get("password") - username_or_email = self.cleaned_data.get("username") - user = None - username_or_email_wrong = False + cleaned_data["username"] = user.username - if "@" in username_or_email: - if User.objects.filter(email=username_or_email).exists(): - username = User.objects.get(email=username_or_email).username - user = authenticate(username=username, password=password) - else: - username_or_email_wrong = True - # raise forms.ValidationError(_('Email Wrong!')) - else: - if User.objects.filter(username=username_or_email).exists(): - user = authenticate(username=username_or_email, password=password) - else: - username_or_email_wrong = True - # raise forms.ValidationError(_('Username Wrong!')) + password = cleaned_data.get("password") - if not user and not username_or_email_wrong: + logged_user = authenticate(username=user.username, password=password) + if not logged_user: raise forms.ValidationError(_("Wrong password!")) - return password + self.confirm_login_allowed(logged_user) + + return cleaned_data class RecoverPasswordForm(forms.Form): diff --git a/src/users/jinja2/users/login.jinja2 b/src/users/jinja2/users/login.jinja2 index 4eb8e754..2b720ba0 100644 --- a/src/users/jinja2/users/login.jinja2 +++ b/src/users/jinja2/users/login.jinja2 @@ -27,8 +27,14 @@

{{ field }}

-

{{ field.errors }}

{% endfor%} + {% if form.non_field_errors() %} + + {% endif %}