Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from django.core.exceptions import ValidationError
- from django.db import models
- from django.utils.translation import gettext_lazy as _
- from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin
- from django.utils import timezone
- from my_holiday.accounts.managers import MyHolidayUserManager
- def validate_password(password):
- if len(password) < 6:
- raise ValidationError(_("Password must be at least 6 characters long."))
- has_letter = any(char.isalpha() for char in password)
- if not has_letter:
- raise ValidationError(_("Password must contain at least one letter."))
- has_digit = any(char.isdigit() for char in password)
- if not has_digit:
- raise ValidationError(_("Password must contain at least one digit."))
- has_underscore = '_' in password
- if not has_underscore:
- raise ValidationError(_("Password must contain at least one underscore."))
- class MyHolidayUser(AbstractBaseUser, PermissionsMixin):
- email = models.EmailField(
- _("email address"),
- unique=True,
- error_messages={
- "unique": _("A user with that email already exists."),
- },
- )
- date_joined = models.DateTimeField(_("date joined"), default=timezone.now)
- is_staff = models.BooleanField(
- default=False,
- )
- is_active = models.BooleanField(
- default=True,
- )
- USERNAME_FIELD = "email"
- objects = MyHolidayUserManager()
- def save(self, *args, **kwargs):
- validate_password(self.password)
- super().save(*args, **kwargs)
- def __str__(self):
- return self.email
- class Profile(models.Model):
- MAX_LENGTH_FIRST_NAME = 50
- MAX_LENGTH_LAST_NAME = 50
- first_name = models.CharField(
- max_length=MAX_LENGTH_FIRST_NAME,
- null=True,
- blank=True,
- verbose_name='First Name',
- )
- last_name = models.CharField(
- max_length=MAX_LENGTH_LAST_NAME,
- null=True,
- blank=True,
- verbose_name='Last Name',
- )
- age = models.IntegerField(
- null=True,
- blank=True,
- verbose_name='Age',
- )
- profile_photo = models.URLField(
- null=True,
- blank=True,
- verbose_name='Profile Photo',
- )
- user = models.OneToOneField(
- MyHolidayUser,
- primary_key=True,
- on_delete=models.CASCADE,
- )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement