mirror of
https://github.com/ae-utbm/sith.git
synced 2025-07-10 11:59:23 +00:00
fix account creation view tests
This commit is contained in:
@ -1,5 +1,3 @@
|
||||
import re
|
||||
|
||||
from django import forms
|
||||
from django.core.validators import EmailValidator
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
@ -7,12 +5,18 @@ from django.utils.translation import gettext_lazy as _
|
||||
from antispam.models import ToxicDomain
|
||||
|
||||
|
||||
class AntiSpamEmailValidator(EmailValidator):
|
||||
def __call__(self, value: str):
|
||||
super().__call__(value)
|
||||
domain_part = value.rsplit("@", 1)[1]
|
||||
if ToxicDomain.objects.filter(domain=domain_part).exists():
|
||||
raise forms.ValidationError(_("Email domain is not allowed."))
|
||||
|
||||
|
||||
validate_antispam_email = AntiSpamEmailValidator()
|
||||
|
||||
|
||||
class AntiSpamEmailField(forms.EmailField):
|
||||
"""An email field that email addresses with a known toxic domain."""
|
||||
|
||||
def run_validators(self, value: str):
|
||||
super().run_validators(value)
|
||||
# Domain part should exist since email validation is guaranteed to run first
|
||||
domain = re.search(EmailValidator.domain_regex, value)
|
||||
if ToxicDomain.objects.filter(domain=domain[0]).exists():
|
||||
raise forms.ValidationError(_("Email domain is not allowed."))
|
||||
default_validators = [validate_antispam_email]
|
||||
|
Reference in New Issue
Block a user