diff --git a/accounts_ext/forms.py b/accounts_ext/forms.py index e06d7b8..e721157 100644 --- a/accounts_ext/forms.py +++ b/accounts_ext/forms.py @@ -13,11 +13,12 @@ from django.contrib.auth.forms import ( AuthenticationForm as AuthenticationFormBase, PasswordResetForm as PasswordResetFormBase, SetPasswordForm as SetPasswordFormBase, - UsernameField + UserChangeForm as UserChangeFormBase, + UserCreationForm as UserCreationFormBase ) - -from core.models import STATUS_ACTIVE -from .models import User, Profile, Company, COMPANY_STATUS_INDIVIDUAL, COMPANY_STATUS_LEGAL +from django.contrib.auth.forms import UsernameField +from core.models import STATUS_ACTIVE, STATUS_CHOICES +from .models import User, Profile, Company logger = logging.getLogger(__name__) @@ -50,7 +51,7 @@ class RegistrationForm(RegistrationFormUniqueEmail): agreement = forms.BooleanField() # captcha = CaptchaField(required=not settings.DEBUG) - captcha = CaptchaField(required=False) + captcha = CaptchaField(required=not settings.DEBUG) title = _('Регистрация') @@ -75,7 +76,7 @@ class RegistrationForm(RegistrationFormUniqueEmail): class Meta: model = User - fields = ('username','email',) + fields = ('username', 'email',) labels = { 'username': _('Логин'), } @@ -92,7 +93,7 @@ class RegistrationCompanyForm(forms.ModelForm): def save(self, user, commit=True): self.instance.user = user - self.instance.status = COMPANY_STATUS_INDIVIDUAL if self.cleaned_data['is_individual'] else COMPANY_STATUS_LEGAL + self.instance.set_company_type(self.cleaned_data['is_individual']) return super().save(commit) class Meta: @@ -160,7 +161,8 @@ class PasswordResetRequestForm(PasswordResetFormBase): 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"), + 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'), @@ -189,8 +191,10 @@ class SetPasswordForm(SetPasswordFormBase): 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=_('Подтверждение пароля')), + 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'), @@ -204,7 +208,6 @@ class SetPasswordForm(SetPasswordFormBase): class ProfileForm(forms.Form): - email = forms.CharField(required=False) field_template = 'bootstrap/field_admin.html' def __init__(self, *args, **kwargs): @@ -215,15 +218,38 @@ class ProfileForm(forms.Form): self.helper.form_class = 'formajax form-replace-block' self.helper.layout = Layout( Div( - Field('email', template=self.field_template), 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('legal_address', template=self.field_template), + Field('address', template=self.field_template), css_class="setProfile" ), Div( @@ -231,17 +257,38 @@ class ProfileForm(forms.Form): css_class="settings-button" ), ) - self.initial['email'] = self.instance.user.email - self.fields['email'].widget.attrs['readonly'] = True - self.fields['birthday'].widget.attrs['class'] = 'date inputText' - - def clean_email(self): - instance = getattr(self, 'instance', None) - if instance and instance.pk: - return instance.user.email - else: - return self.cleaned_data['email'] + super().__init__(*args, **kwargs) class Meta: - model = Profile + model = Company exclude = ('user', 'create_at', 'updated_at', 'status',) + + +class UserChangeForm(UserChangeFormBase): + status = forms.ChoiceField(choices=STATUS_CHOICES) + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + super.init_fields(self.fields) + + class Meta: + model = get_user_model() + fields = '__all__' + + +class UserCreationForm(UserCreationFormBase): + + def clean_email(self): + email = super().clean_email() + if DisposableEmailChecker().is_disposable(email): + raise forms.ValidationError(_('Введите email с валидными доменом')) + return email + + class Meta(UserCreationFormBase.Meta): + model = get_user_model() + fields = ('email', 'username', 'status',) + field_classes = { + 'email': forms.EmailField, + 'username': UsernameField, + 'status': forms.ChoiceField + }