Base
This commit is contained in:
@ -0,0 +1,3 @@
|
||||
from .password_validators import SpecialCharacterValidator, UppercaseValidator, LengthValidator
|
||||
|
||||
__all__ = ['SpecialCharacterValidator', 'UppercaseValidator', 'LengthValidator']
|
||||
@ -0,0 +1,43 @@
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.utils.translation import gettext as _
|
||||
|
||||
class SpecialCharacterValidator:
|
||||
def __init__(self, special_chars='@$!%*?&'):
|
||||
self.special_chars = special_chars
|
||||
|
||||
def validate(self, password, user=None):
|
||||
if not any(char in self.special_chars for char in password):
|
||||
raise ValidationError(
|
||||
_("Le mot de passe doit contenir au moins un caractère spécial (%(special_chars)s)."),
|
||||
code='password_no_symbol',
|
||||
params={'special_chars': self.special_chars},
|
||||
)
|
||||
|
||||
def get_help_text(self):
|
||||
return _(f"Votre mot de passe doit contenir au moins un caractère spécial ({self.special_chars}).")
|
||||
|
||||
class UppercaseValidator:
|
||||
def validate(self, password, user=None):
|
||||
if not any(char.isupper() for char in password):
|
||||
raise ValidationError(
|
||||
_("Le mot de passe doit contenir au moins une lettre majuscule."),
|
||||
code='password_no_upper',
|
||||
)
|
||||
|
||||
def get_help_text(self):
|
||||
return _("Votre mot de passe doit contenir au moins une lettre majuscule.")
|
||||
|
||||
class LengthValidator:
|
||||
def __init__(self, min_length=8):
|
||||
self.min_length = min_length
|
||||
|
||||
def validate(self, password, user=None):
|
||||
if len(password) < self.min_length:
|
||||
raise ValidationError(
|
||||
_("Le mot de passe doit contenir au moins %(min_length)d caractères."),
|
||||
code='password_too_short',
|
||||
params={'min_length': self.min_length},
|
||||
)
|
||||
|
||||
def get_help_text(self):
|
||||
return _(f"Votre mot de passe doit contenir au moins {self.min_length} caractères.")
|
||||
Reference in New Issue
Block a user