From af286766ea112c5d3510dd817ce082f1e6e42440 Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 7 Sep 2021 07:17:00 +0200 Subject: [PATCH] setting for two mail fields on signup --- InvenTree/InvenTree/forms.py | 30 ++++++++++++++++++- InvenTree/common/models.py | 6 ++++ .../templates/InvenTree/settings/login.html | 1 + 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/InvenTree/InvenTree/forms.py b/InvenTree/InvenTree/forms.py index dedb078623..beec4cc758 100644 --- a/InvenTree/InvenTree/forms.py +++ b/InvenTree/InvenTree/forms.py @@ -13,7 +13,7 @@ from crispy_forms.helper import FormHelper from crispy_forms.layout import Layout, Field from crispy_forms.bootstrap import PrependedText, AppendedText, PrependedAppendedText, StrictButton, Div -from allauth.account.forms import SignupForm +from allauth.account.forms import SignupForm, set_form_field_order from allauth.socialaccount.adapter import DefaultSocialAccountAdapter from part.models import PartCategory @@ -216,8 +216,36 @@ class CustomSignupForm(SignupForm): """ def __init__(self, *args, **kwargs): kwargs['email_required'] = InvenTreeSetting.get_setting('LOGIN_MAIL_REQUIRED') + super().__init__(*args, **kwargs) + # check for two mail fields + if InvenTreeSetting.get_setting('LOGIN_SIGNUP_MAIL_TWICE'): + self.fields["email2"] = forms.EmailField( + label=_("E-mail (again)"), + widget=forms.TextInput( + attrs={ + "type": "email", + "placeholder": _("E-mail address confirmation"), + } + ), + ) + + # reorder fields + set_form_field_order(self, ["username", "email", "email2", "password1", "password2", ]) + + def clean(self): + cleaned_data = super().clean() + + # check for two mail fields + if InvenTreeSetting.get_setting('LOGIN_SIGNUP_MAIL_TWICE'): + email = cleaned_data.get("email") + email2 = cleaned_data.get("email2") + if (email and email2) and email != email2: + self.add_error("email2", _("You must type the same email each time.")) + + return cleaned_data + class CustomSocialAccountAdapter(DefaultSocialAccountAdapter): """ diff --git a/InvenTree/common/models.py b/InvenTree/common/models.py index a209c5d5ed..21e2d37c7f 100644 --- a/InvenTree/common/models.py +++ b/InvenTree/common/models.py @@ -858,6 +858,12 @@ class InvenTreeSetting(BaseInvenTreeSetting): 'default': True, 'validator': bool, }, + 'LOGIN_SIGNUP_MAIL_TWICE': { + 'name': _('Mail twice'), + 'description': _('On signup ask users twice for their mail'), + 'default': False, + 'validator': bool, + }, } class Meta: diff --git a/InvenTree/templates/InvenTree/settings/login.html b/InvenTree/templates/InvenTree/settings/login.html index 471903554b..bcc2f16767 100644 --- a/InvenTree/templates/InvenTree/settings/login.html +++ b/InvenTree/templates/InvenTree/settings/login.html @@ -22,6 +22,7 @@ {% trans 'Signup' %} + {% include "InvenTree/settings/setting.html" with key="LOGIN_SIGNUP_MAIL_TWICE" icon="fa-info-circle" %} {% include "InvenTree/settings/setting.html" with key="LOGIN_SIGNUP_SSO_AUTO" icon="fa-info-circle" %}