model.py

mail@pastecode.io avatar
unknown
python
2 years ago
4.2 kB
45
Indexable
Never
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, PermissionsMixin
from django.db import models
from django.utils import timezone


class CustomUserManager(BaseUserManager):
    def create_user(self, email, password, **other_fields):

        other_fields.setdefault("is_active", True)

        if not email:
            raise ValueError("Нужно ввести электронную почту")

        email = self.normalize_email(email)

        user = self.model(email=email, password=password, **other_fields)
        user.set_password(password)
        user.save()

        return user

    def create_superuser(self, email, password, **other_fields):
        other_fields.setdefault("is_staff", True)
        other_fields.setdefault("is_superuser", True)
        other_fields.setdefault("is_active", True)

        if other_fields.get("is_staff") is not True:
            raise ValueError("Superuser is_staff not True")

        if other_fields.get("is_superuser") is not True:
            raise ValueError("Superuser not True")

        return self.create_user(email, password, **other_fields)


class User(AbstractBaseUser, PermissionsMixin):
    class Role(models.TextChoices):
        ADMIN = "ADMIN", "Администратор"
        LEGAL_ENTITY = "LEGAL_ENTITY", "Юр. лицо \ ИП"
        OMS = "OMS", "Орган местного самоуправления"

    base_role = Role.ADMIN

    email = models.EmailField("email адрес", unique=True)
    full_user_name = models.CharField("ФИО", max_length=100, blank=True, null=True)
    user_position = models.CharField("Должность", max_length=100, blank=True, null=True)
    user_phone = models.CharField("Номер телефона", max_length=12, blank=True, null=True)

    boss_email = models.EmailField("email адрес руководителя", blank=True, null=True)
    full_boss_name = models.CharField("ФИО руководителя", max_length=100, blank=True, null=True)
    boss_position = models.CharField("Должность руководителя", max_length=100, blank=True, null=True)
    boss_phone = models.CharField("Номер телефона руководителя", max_length=12, blank=True, null=True)

    mail_address = models.CharField("Почтовый адрес", max_length=150, blank=True, null=True)
    legal_address = models.CharField("Юридический адрес", max_length=150, blank=True, null=True)

    kpp = models.CharField("КПП", max_length=9, blank=True, null=True)
    inn = models.CharField("ИНН", max_length=12, blank=True, null=True)
    oktmo = models.CharField("ОКТМО", max_length=11, blank=True, null=True)
    ogrn = models.CharField("ОГРН", max_length=13, blank=True, null=True)

    full_org_name = models.CharField("Полное название организации", max_length=150, blank=True, null=True)
    short_org_name = models.CharField("Короткое название организации", max_length=50, blank=True, null=True)

    date_joined = models.DateTimeField(default=timezone.now)

    user_role = models.CharField("Тип пользователя", max_length=50, choices=Role.choices, default=base_role)

    is_staff = models.BooleanField("Персонал", default=False)
    is_active = models.BooleanField("Активированный пользователь", default=True)

    objects = CustomUserManager()

    USERNAME_FIELD = "email"
    REQUIRED_FIELDS = []

    class Meta:
        verbose_name = "Пользователь"
        verbose_name_plural = "Пользователи"

    def save(self, *args, **kwargs):
        if not self.pk:
            self.user_role = self.base_role
            return super().save(*args, **kwargs)

    def __str__(self):
        return f"{self.get_user_role_display()} | {self.full_org_name} | {self.email}"


class LegalEntity(User):
    base_role = User.Role.LEGAL_ENTITY

    class Meta:
        proxy = True


class OMS(User):
    base_role = User.Role.OMS

    class Meta:
        proxy = True