Advertisement
horozov86

Validation of password

Mar 25th, 2024
808
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.40 KB | None | 0 0
  1. from django.core.exceptions import ValidationError
  2. from django.db import models
  3. from django.utils.translation import gettext_lazy as _
  4. from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin
  5. from django.utils import timezone
  6.  
  7. from my_holiday.accounts.managers import MyHolidayUserManager
  8.  
  9. def validate_password(password):
  10.     if len(password) < 6:
  11.         raise ValidationError(_("Password must be at least 6 characters long."))
  12.    
  13.     has_letter = any(char.isalpha() for char in password)
  14.     if not has_letter:
  15.         raise ValidationError(_("Password must contain at least one letter."))
  16.    
  17.     has_digit = any(char.isdigit() for char in password)
  18.     if not has_digit:
  19.         raise ValidationError(_("Password must contain at least one digit."))
  20.    
  21.     has_underscore = '_' in password
  22.     if not has_underscore:
  23.         raise ValidationError(_("Password must contain at least one underscore."))
  24.  
  25. class MyHolidayUser(AbstractBaseUser, PermissionsMixin):
  26.     email = models.EmailField(
  27.         _("email address"),
  28.         unique=True,
  29.         error_messages={
  30.             "unique": _("A user with that email already exists."),
  31.         },
  32.     )
  33.  
  34.     date_joined = models.DateTimeField(_("date joined"), default=timezone.now)
  35.  
  36.     is_staff = models.BooleanField(
  37.         default=False,
  38.     )
  39.  
  40.     is_active = models.BooleanField(
  41.         default=True,
  42.     )
  43.  
  44.     USERNAME_FIELD = "email"
  45.  
  46.     objects = MyHolidayUserManager()
  47.  
  48.     def save(self, *args, **kwargs):
  49.         validate_password(self.password)
  50.         super().save(*args, **kwargs)
  51.  
  52.     def __str__(self):
  53.         return self.email
  54.  
  55. class Profile(models.Model):
  56.     MAX_LENGTH_FIRST_NAME = 50
  57.     MAX_LENGTH_LAST_NAME = 50
  58.  
  59.     first_name = models.CharField(
  60.         max_length=MAX_LENGTH_FIRST_NAME,
  61.         null=True,
  62.         blank=True,
  63.         verbose_name='First Name',
  64.     )
  65.  
  66.     last_name = models.CharField(
  67.         max_length=MAX_LENGTH_LAST_NAME,
  68.         null=True,
  69.         blank=True,
  70.         verbose_name='Last Name',
  71.     )
  72.  
  73.     age = models.IntegerField(
  74.         null=True,
  75.         blank=True,
  76.         verbose_name='Age',
  77.     )
  78.  
  79.     profile_photo = models.URLField(
  80.         null=True,
  81.         blank=True,
  82.         verbose_name='Profile Photo',
  83.     )
  84.  
  85.     user = models.OneToOneField(
  86.         MyHolidayUser,
  87.         primary_key=True,
  88.         on_delete=models.CASCADE,
  89.     )
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement