123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- from phonenumber_field.phonenumber import PhoneNumber, to_python, validate_region
- from phonenumber_field.validators import validate_international_phonenumber
- from django import forms
- from django.db import models
- from django.utils.translation import gettext, gettext_lazy as _, pgettext
- from django.contrib.auth.forms import UserCreationForm, AuthenticationForm, password_validation
- from allauth.account.forms import (
- SignupForm as allauthSignupForm,
- PasswordField,
- LoginForm,
- set_form_field_order,
- exceptions,
- validators,
- get_adapter,
- perform_login)
- from allauth.account import app_settings
- from phonenumber_field.widgets import PhoneNumberInternationalFallbackWidget, PhonePrefixSelect, PhoneNumberPrefixWidget
- from phonenumber_field.formfields import PhoneNumberField
- from .models import User
- class SignupForm(UserCreationForm, PasswordField):
- class Meta:
- model = User
- fields = ['firstname', 'lastname', 'email', 'phone_number']
- labels = {
- 'firstname': '',
- 'lastname': '',
- 'email': '',
- 'phone_number': '',
- }
- widgets = {
- 'firstname' : forms.TextInput(attrs = {'placeholder': _('Firstname')}),
- 'lastname' : forms.TextInput(attrs = {'placeholder': _('Lastname')}),
- 'email' : forms.TextInput(attrs = {'placeholder': _('Email')}),
- 'phone_number' : PhoneNumberPrefixWidget(attrs = {'placeholder': _('Phone')}),
- }
- password1 = forms.CharField(
- label=_(""),
- strip=False,
- widget=forms.PasswordInput(attrs={"autocomplete": "new-password", "placeholder": _('Password')}),
- help_text=password_validation.password_validators_help_text_html(),
- )
- password2 = forms.CharField(
- label=_(""),
- widget=forms.PasswordInput(attrs={"autocomplete": "new-password", "placeholder": _('Repeat password')}),
- strip=False,
- help_text=_("Enter the same password as before, for verification."),
- )
- field_order = ['firstname', 'lastname', 'email', 'phone_number', 'password1', 'password2',]
- def __init__(self, *args, **kwargs):
- super(SignupForm, self).__init__(*args, **kwargs)
-
- #self.fields['email'].required = False
- class CustomLoginForm(LoginForm):
- error_messages = {
- "account_inactive": _("This account is currently inactive."),
- "email_password_mismatch": _(
- "The e-mail address and/or password you specified are not correct."
- ),
- "username_password_mismatch": _(
- "The phone number and/or password you specified are not correct."
- ),
- }
- def __init__(self, *args, **kwargs):
- self.request = kwargs.pop("request", None)
- super(LoginForm, self).__init__(*args, **kwargs)
- login_widget = forms.TextInput(
- attrs={"placeholder": _("Phone or e-mail"), "autocomplete": "email",})
- login_field = forms.CharField(
- label=pgettext("field label", ""), widget=login_widget) #Login label
- self.fields['password'].label = '' #Password label
- self.fields["login"] = login_field
- set_form_field_order(self, ["login", "password", "remember"])
- if app_settings.SESSION_REMEMBER is not None:
- del self.fields["remember"]
- def user_credentials(self):
- """
- Provides the credentials required to authenticate the user for
- login.
- """
- credentials = {}
- login = self.cleaned_data["login"]
-
- if self._is_login_email(login):
- credentials["email"] = login
- credentials["phone_number"] = login
- credentials["password"] = self.cleaned_data["password"]
- return credentials
- def clean_login(self):
- login = self.cleaned_data["login"]
- return login.strip()
- def _is_login_email(self, login):
- try:
- validators.validate_email(login)
- ret = True
- except exceptions.ValidationError:
- ret = False
- return ret
- def clean(self):
- super(LoginForm, self).clean()
- if self._errors:
- return
- credentials = self.user_credentials()
- user = get_adapter(self.request).authenticate(self.request, **credentials)
- if user:
- self.user = user
- else:
- # Show error "password mismatch". If email: email and password are wrong. If phone: phone and password
- login = self.cleaned_data["login"]
- if self._is_login_email(login):
- auth_method = app_settings.AuthenticationMethod.EMAIL
- else:
- auth_method = app_settings.AuthenticationMethod.USERNAME
- raise forms.ValidationError(
- self.error_messages["%s_password_mismatch" % auth_method]
- )
- return self.cleaned_data
- def login(self, request, redirect_url=None):
- email = self.user_credentials().get("email")
- ret = perform_login(
- request,
- self.user,
- email_verification=app_settings.EMAIL_VERIFICATION,
- redirect_url=redirect_url,
- email=email,
- )
- remember = app_settings.SESSION_REMEMBER
- if remember is None:
- remember = self.cleaned_data["remember"]
- if remember:
- request.session.set_expiry(app_settings.SESSION_COOKIE_AGE)
- else:
- request.session.set_expiry(0)
- return ret
- ########################################################################
- '''
- def login(self, *args, **kwargs):
- # Add your own processing here.
- # You must return the original result.
- return super(CustomLoginForm, self).login(*args, **kwargs)
- def user_credentials(self):
- """
- Provides the credentials required to authenticate the user for
- login.
- """
- credentials = {}
- login = self.cleaned_data["login"]
- if app_settings.AUTHENTICATION_METHOD == AuthenticationMethod.EMAIL:
- credentials["email"] = login
- elif app_settings.AUTHENTICATION_METHOD == AuthenticationMethod.USERNAME:
- credentials["username"] = login
- else:
- if self._is_login_email(login):
- credentials["email"] = login
- credentials["username"] = login
- credentials["password"] = self.cleaned_data["password"]
- return credentials
- '''
|