You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
288 lines
11 KiB
288 lines
11 KiB
import logging
|
|
|
|
from crispy_forms.helper import FormHelper
|
|
from crispy_forms.layout import Layout, Field, Div, Submit, HTML
|
|
from django import forms
|
|
from django.contrib.auth import get_user_model, password_validation
|
|
from django.contrib.auth.forms import (
|
|
AuthenticationForm as AuthenticationFormBase,
|
|
PasswordResetForm as PasswordResetFormBase,
|
|
SetPasswordForm as SetPasswordFormBase,
|
|
UserChangeForm as UserChangeFormBase,
|
|
UserCreationForm as UserCreationFormBase
|
|
)
|
|
from django.contrib.auth.forms import UsernameField
|
|
from django.urls import reverse, reverse_lazy
|
|
from django.utils.translation import ugettext_lazy as _
|
|
from django_email_blacklist import DisposableEmailChecker
|
|
from registration.forms import RegistrationFormUniqueEmail
|
|
from snowpenguin.django.recaptcha2.fields import ReCaptchaField
|
|
from snowpenguin.django.recaptcha2.widgets import ReCaptchaWidget
|
|
|
|
from core.models import STATUS_ACTIVE
|
|
from .models import Profile, Company
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class RegistrationForm(RegistrationFormUniqueEmail):
|
|
email = forms.EmailField(label=_("E-mail"), widget=forms.EmailInput(attrs={'class': 'reg__text'}))
|
|
password1 = forms.CharField(
|
|
label=_('Пароль'),
|
|
strip=False,
|
|
widget=forms.PasswordInput(attrs={'class': 'reg__text-label'}),
|
|
help_text=password_validation.password_validators_help_text_html(),
|
|
)
|
|
password2 = forms.CharField(
|
|
label=_('Подтверждение пароля'),
|
|
widget=forms.PasswordInput(attrs={'class': 'reg__text-label'}),
|
|
strip=False,
|
|
help_text=_('Для потверждения пароля введите его еще раз'),
|
|
)
|
|
first_name = forms.CharField(label=_("Имя"), max_length=255, widget=forms.TextInput(attrs={'class': 'reg__text'}))
|
|
last_name = forms.CharField(label=_("Фамилия"), max_length=255,
|
|
widget=forms.TextInput(attrs={'class': 'reg__text'}))
|
|
patronymic = forms.CharField(label=_("Отчество"), max_length=255,
|
|
widget=forms.TextInput(attrs={'class': 'reg__text'}))
|
|
agreement = forms.BooleanField()
|
|
|
|
captcha = ReCaptchaField(label=_(''), widget=ReCaptchaWidget())
|
|
|
|
title = _('Регистрация')
|
|
|
|
def clean_email(self):
|
|
email = super().clean_email()
|
|
if DisposableEmailChecker().is_disposable(email):
|
|
raise forms.ValidationError(_('Введите email с валидными доменом'))
|
|
return email
|
|
|
|
def save(self, commit=True):
|
|
user = super().save(commit)
|
|
profile = Profile.objects.filter(user=user).first()
|
|
if not profile:
|
|
profile = Profile()
|
|
|
|
profile.first_name = self.cleaned_data['first_name']
|
|
profile.last_name = self.cleaned_data['last_name']
|
|
profile.patronymic = self.cleaned_data['patronymic']
|
|
profile.save()
|
|
|
|
return user
|
|
|
|
class Meta:
|
|
model = get_user_model()
|
|
fields = ('username', 'email',)
|
|
labels = {
|
|
'username': _('Логин'),
|
|
}
|
|
help_texts = {
|
|
'username': _('Не менее 5-и символов'),
|
|
}
|
|
widgets = {
|
|
'username': forms.TextInput(attrs={'class': 'reg__text-label'}),
|
|
}
|
|
|
|
|
|
class RegistrationCompanyForm(forms.ModelForm):
|
|
is_individual = forms.BooleanField(label=_('Я физическое лицо'), required=False)
|
|
|
|
def save(self, user, commit=True):
|
|
company = Company.objects.filter(user=user).first()
|
|
if company:
|
|
self.instance.id = company.id
|
|
self.instance.create_at = company.create_at
|
|
self.instance.updated_at = company.updated_at
|
|
self.instance.user = user
|
|
self.instance.set_company_type(self.cleaned_data['is_individual'])
|
|
return super().save(commit)
|
|
|
|
class Meta:
|
|
model = Company
|
|
fields = ('company_name', 'address', 'inn', 'ogrn')
|
|
labels = {
|
|
'company_name': _('Наименование'),
|
|
'inn': _('ИНН'),
|
|
'ogrn': _('ОГРН'),
|
|
'address': _('Юридический адрес')
|
|
}
|
|
widgets = {
|
|
'company_name': forms.TextInput(attrs={'class': 'reg__text'}),
|
|
'inn': forms.TextInput(attrs={'placeholder': '111222333444', 'class': 'reg__text'}),
|
|
'ogrn': forms.TextInput(attrs={'placeholder': 'С Г Г К К Н Н Х Х Х Х Х Ч', 'class': 'reg__text'}),
|
|
'address': forms.Textarea(attrs={'class': 'reg__text', 'rows': 5, 'cols': 40})
|
|
}
|
|
|
|
|
|
class AuthenticationForm(AuthenticationFormBase):
|
|
username = UsernameField(max_length=255, widget=forms.TextInput(attrs={
|
|
'class': 'reg__text',
|
|
'style': 'text-align:center',
|
|
'placeholder': _('Email')
|
|
}))
|
|
password = forms.CharField(max_length=255, widget=forms.PasswordInput(attrs={
|
|
'class': 'reg__text',
|
|
'style': 'text-align:center',
|
|
'placeholder': _('Пароль')
|
|
}))
|
|
|
|
field_template = 'bootstrap/forms/authentication.html'
|
|
title = _('Вход')
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
self.helper = FormHelper()
|
|
self.helper.layout = Layout(
|
|
Field('username', template=self.field_template),
|
|
Field('password', template=self.field_template),
|
|
Div(
|
|
HTML('<a class="header__link" href="{}">{}</a>'.format(
|
|
reverse_lazy('accounts_ext:reset_password'),
|
|
_('Забыли пароль ?'))
|
|
),
|
|
css_class="text-right"
|
|
),
|
|
Div(
|
|
Div(
|
|
Submit('submit', _('Войти'), css_class='btn-danger'),
|
|
css_class='col-lg-12 text-center'
|
|
),
|
|
css_class='row'
|
|
),
|
|
)
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
class PasswordResetRequestForm(PasswordResetFormBase):
|
|
field_template = 'bootstrap/forms/authentication.html'
|
|
|
|
email_slug = 'reset_password'
|
|
title = _('Сброс пароля')
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
self.helper = FormHelper()
|
|
self.helper.form_action = reverse_lazy('accounts_ext:reset_password')
|
|
self.helper.layout = Layout(
|
|
Field('email', css_class='reg__text reg__text__center', template=self.field_template,
|
|
placeholder="example@email.com"),
|
|
Div(
|
|
Div(
|
|
Submit('submit', _('Сбросить'), css_class='btn-danger'),
|
|
css_class='col-lg-12 text-center'
|
|
),
|
|
css_class="row"
|
|
),
|
|
)
|
|
super().__init__(*args, **kwargs)
|
|
|
|
def get_users(self, email):
|
|
active_users = get_user_model()._default_manager.filter(**{
|
|
'%s__iexact' % get_user_model().get_email_field_name(): email,
|
|
'status': STATUS_ACTIVE,
|
|
})
|
|
return (u for u in active_users if u.has_usable_password())
|
|
|
|
|
|
class SetPasswordForm(SetPasswordFormBase):
|
|
field_template = 'bootstrap/forms/authentication.html'
|
|
|
|
field_order = ('new_password1', 'new_password2')
|
|
|
|
title = _('Сброс пароля')
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
self.helper = FormHelper()
|
|
self.helper.layout = Layout(
|
|
Field('new_password1', css_class='reg__text reg__text__center', template=self.field_template,
|
|
placeholder=_('Пароль')),
|
|
Field('new_password2', css_class='reg__text reg__text__center', template=self.field_template,
|
|
placeholder=_('Подтверждение пароля')),
|
|
Div(
|
|
Div(
|
|
Submit('submit', _('Сбросить пароль'), css_class='btn-danger'),
|
|
css_class='col-lg-12 text-center'
|
|
),
|
|
css_class="row"
|
|
),
|
|
)
|
|
|
|
super().__init__(kwargs.pop('user'), *args, **kwargs)
|
|
|
|
|
|
class ProfileForm(forms.Form):
|
|
field_template = 'bootstrap/field_admin.html'
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.helper = FormHelper()
|
|
self.helper.form_action = reverse('accounts_ext:update')
|
|
self.helper.form_class = 'formajax form-replace-block'
|
|
self.helper.layout = Layout(
|
|
Div(
|
|
Field('first_name', template=self.field_template),
|
|
Field('last_name', template=self.field_template),
|
|
Field('patronymic', template=self.field_template),
|
|
Field('birthday', template=self.field_template),
|
|
Field('phone', template=self.field_template),
|
|
css_class="setProfile"
|
|
),
|
|
Div(
|
|
Submit('submit', _('Save'), css_class='button button--bigOrange'),
|
|
css_class="settings-button"
|
|
),
|
|
)
|
|
self.fields['birthday'].widget.attrs['class'] = 'date inputText'
|
|
|
|
class Meta:
|
|
model = Profile
|
|
exclude = ('user', 'create_at', 'updated_at', 'status',)
|
|
|
|
|
|
class CompanyForm(forms.Form):
|
|
def __init__(self, *args, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.helper = FormHelper()
|
|
self.helper.form_action = reverse('accounts_ext:update')
|
|
self.helper.form_class = 'formajax form-replace-block'
|
|
self.helper.layout = Layout(
|
|
Div(
|
|
Field('company_name', template=self.field_template),
|
|
Field('inn', template=self.field_template),
|
|
Field('ogrn', template=self.field_template),
|
|
Field('address', template=self.field_template),
|
|
css_class="setProfile"
|
|
),
|
|
Div(
|
|
Submit('submit', _('Save'), css_class='button button--bigOrange'),
|
|
css_class="settings-button"
|
|
),
|
|
)
|
|
super().__init__(*args, **kwargs)
|
|
|
|
class Meta:
|
|
model = Company
|
|
exclude = ('user', 'create_at', 'updated_at', 'status',)
|
|
|
|
|
|
class UserChangeForm(UserChangeFormBase):
|
|
class Meta:
|
|
model = get_user_model()
|
|
fields = ('status', 'referral_user',)
|
|
|
|
|
|
class UserCreationForm(UserCreationFormBase):
|
|
def clean_email(self):
|
|
email = self.cleaned_data['email']
|
|
if DisposableEmailChecker().is_disposable(email):
|
|
raise forms.ValidationError(_('Введите email с валидными доменом'))
|
|
return email
|
|
|
|
class Meta:
|
|
model = get_user_model()
|
|
fields = ('email', 'username', 'status', 'referral_user', 'is_superuser')
|
|
field_classes = {
|
|
'email': forms.EmailField,
|
|
'username': UsernameField,
|
|
'status': forms.ChoiceField,
|
|
'is_superuser': forms.BooleanField
|
|
}
|
|
|