|
| 1 | +import re |
| 2 | +from django.core.exceptions import ValidationError |
| 3 | +from django.utils.translation import ugettext_lazy as _ |
| 4 | + |
| 5 | + |
| 6 | +class NumberValidator(object): |
| 7 | + def validate(self, password, user=None): |
| 8 | + if not re.findall('\d', password): |
| 9 | + raise ValidationError( |
| 10 | + _("The password must contain at least 1 digit, 0-9."), |
| 11 | + code='password_no_number', |
| 12 | + ) |
| 13 | + |
| 14 | + def get_help_text(self): |
| 15 | + return _( |
| 16 | + "Your password must contain at least 1 digit, 0-9." |
| 17 | + ) |
| 18 | + |
| 19 | + |
| 20 | +class UppercaseValidator(object): |
| 21 | + def validate(self, password, user=None): |
| 22 | + if not re.findall('[A-Z]', password): |
| 23 | + raise ValidationError( |
| 24 | + _("The password must contain at least 1 uppercase letter, A-Z."), |
| 25 | + code='password_no_upper', |
| 26 | + ) |
| 27 | + |
| 28 | + def get_help_text(self): |
| 29 | + return _( |
| 30 | + "Your password must contain at least 1 uppercase letter, A-Z." |
| 31 | + ) |
| 32 | + |
| 33 | + |
| 34 | +class LowercaseValidator(object): |
| 35 | + def validate(self, password, user=None): |
| 36 | + if not re.findall('[a-z]', password): |
| 37 | + raise ValidationError( |
| 38 | + _("The password must contain at least 1 lowercase letter, a-z."), |
| 39 | + code='password_no_lower', |
| 40 | + ) |
| 41 | + |
| 42 | + def get_help_text(self): |
| 43 | + return _( |
| 44 | + "Your password must contain at least 1 lowercase letter, a-z." |
| 45 | + ) |
| 46 | + |
| 47 | + |
| 48 | +class SymbolValidator(object): |
| 49 | + def validate(self, password, user=None): |
| 50 | + if not re.findall('[()[\]{}|\\`~!@#$%^&*_\-+=;:\'",<>./?]', password): |
| 51 | + raise ValidationError( |
| 52 | + _("The password must contain at least 1 symbol: " + |
| 53 | + "()[]{}|\`~!@#$%^&*_-+=;:'\",<>./?"), |
| 54 | + code='password_no_symbol', |
| 55 | + ) |
| 56 | + |
| 57 | + def get_help_text(self): |
| 58 | + return _( |
| 59 | + "Your password must contain at least 1 symbol: " + |
| 60 | + "()[]{}|\`~!@#$%^&*_-+=;:'\",<>./?" |
| 61 | + ) |
0 commit comments