Commit e3e2f58b authored by Noe Nieto's avatar Noe Nieto 💬

Moved the validator from model to form; recovery email field is now required;...

Moved the validator from model to form; recovery email field is now required; liberty/ldh_middleware#79
parent 0250e0eb
Pipeline #11510 failed with stage
......@@ -17,7 +17,8 @@ from registration.forms import validators
# Others
from cart.models import ChosenReward
from ldapregister.forms import RegistrationForm
from purist.models import AccountType
from purist.models import AccountType, get_woo_connection
User = get_user_model()
......@@ -29,6 +30,13 @@ def validate_reserved_names(value):
raise ValidationError(validators.RESERVED_NAME, code='invalid')
def woo_email_available_validator(value):
wc = get_woo_connection()
result = wc.get('customers?email={}'.format(value))
if result.ok and len(result.json()) > 0:
raise ValidationError(_('This recovery email is already in use'))
class CartRegistrationForm(RegistrationForm):
class Meta(RegistrationForm.Meta):
fields = (
......@@ -45,7 +53,7 @@ class CartRegistrationForm(RegistrationForm):
help_text=_('Optional. Enter an email address were we can send you '
'recovery information.'),
validators=[
validators.validate_confusables_email,
validators.validate_confusables_email, woo_email_available_validator
],
required=False,
)
......
......@@ -43,16 +43,6 @@ def get_woo_connection():
)
def woo_email_available_validator(value):
wc = get_woo_connection()
result = wc.get('customers?email={}'.format(value))
if result.ok and len(result.json()) > 0:
raise validators.ValidationError(
_('%(email)s is already being used'),
params={'email':value}
)
@deconstructible
class UsernameValidator(validators.RegexValidator):
regex = r'^[A-Za-z][A-Za-z0-9]*$'
......@@ -86,12 +76,12 @@ class User(AbstractUser):
max_length=150,
unique=True,
help_text=_('Required. Start with a letter, followed by letters or numbers.'),
validators=[username_validator, woo_email_available_validator],
validators=[username_validator],
error_messages={
'unique': _("An account with that address already exists."),
},
)
email = models.EmailField(_('email address'), blank=True, unique=True)
email = models.EmailField(_('email address'), blank=False, unique=True)
tunnel_user = models.CharField(max_length=250, default=None, null=True)
tunnel_password = models.CharField(max_length=250, default=None, null=True)
account_type = EnumIntegerField(enum=AccountType,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment