Untitled
raw download clone
TEXT
views 22
,
size 3533 b
from django.db import models
from django.core.exceptions import ValidationError
from django.core.validators import validate_email, EmailValidator
from django.contrib.auth.password_validation import validate_password
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
# from validators.common import max_length_validator
from django.contrib.auth.models import PermissionsMixin
from enum import Enum


class CustomUserManager(BaseUserManager):
    def create_user(self, email,  password, is_psychologist=False):
        user = self.model(
            email=self.normalize_email(email),
            is_psychologist=is_psychologist
        )

        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self, email, password):
        user = self.create_user(
            email=self.normalize_email(email),
            password=password,
        )

        user.is_superuser = True
        user.is_admin = True
        user.is_staff = True
        user.save(using=self._db)
        return user


class PsychologistStatusChoice(Enum):   # A subclass of Enum
    waiting = "waiting"
    need_moderate = "need_moderate"
    accepted = "accepted"
    declined = "declined"


class CustomUser(AbstractBaseUser, PermissionsMixin):
    email               = models.CharField(max_length=60, unique=True,
                                            error_messages={'unique': "Такой имейл уже есть"},
                                            validators=[EmailValidator(
                                                message="Неправильно введен Email"
                                            )])
    first_name          = models.CharField(max_length=50, null=True, blank=True)
    last_name           = models.CharField(max_length=50, null=True, blank=True)
    avatar              = models.ImageField(upload_to='avatars', default='avatars/avatar.jpg')
    location_city       = models.CharField(max_length=50, null=True, blank=True)
    location_country    = models.CharField(max_length=50, null=True, blank=True)
    date_joined		    = models.DateTimeField(verbose_name='date joined', auto_now_add=True)
    last_login			= models.DateTimeField(verbose_name='last login', auto_now=True)
    is_psychologist     = models.BooleanField(default=False)
    is_active			= models.BooleanField(default=True)
    is_admin            = models.BooleanField(default=False)
    is_staff            = models.BooleanField(default=False)
    is_superuser        = models.BooleanField(default=False)

    USERNAME_FIELD = 'email'

    objects = CustomUserManager()

    def __str__(self):
        return self.email

    # For checking permissions. to keep it simple all admin have ALL permissons
    def has_perm(self, perm, obj=None):
        return self.is_admin

    # Does this user have permission to view this app?
    def has_module_perms(self, app_label):
        return True


class Psychologist(models.Model):
    user = models.OneToOneField(CustomUser, on_delete=models.CASCADE)
    status = models.CharField(max_length=50, default='waiting',
                              choices=[(tag.name, tag.value) for tag in PsychologistStatusChoice])


class Image(models.Model):
    name = models.CharField(max_length=255, null=False)
    image = models.ImageField(null=False)
    psychologist = models.ForeignKey(Psychologist, on_delete=models.CASCADE)
close fullscreen
Login or Register to edit or fork this paste. It's free.