87 lines
3.2 KiB
Python
87 lines
3.2 KiB
Python
from django.db import models
|
|
from django.contrib.auth.models import AbstractBaseUser,BaseUserManager,PermissionsMixin
|
|
from phonenumber_field.modelfields import PhoneNumberField
|
|
from address.models import AddressField
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
STATUS =(
|
|
('Company_Account', _('Company account')),
|
|
('Regular_Account',_('Regular account')),
|
|
('Need_verification', _("Wait for verification")),
|
|
('BaseAcc',_('Base account')),
|
|
)
|
|
|
|
class UserManager(BaseUserManager):
|
|
def create_user(self,email, password = None, **extra_fields):
|
|
if not email:
|
|
raise ValueError("Users must havve an email address")
|
|
|
|
email = self.model (
|
|
email = self.normalize_email(email)
|
|
)
|
|
user = self.model(email = email,**extra_fields)
|
|
user.set_password(password)
|
|
user.save(using = self._db)
|
|
return user
|
|
|
|
def create_superuser(self,email,password, **extra_fields):
|
|
extra_fields.setdefault('is_active', True)
|
|
extra_fields.setdefault('is_staff', True)
|
|
extra_fields.setdefault('is_superuser', True)
|
|
|
|
if extra_fields.get('is_staff') is not True:
|
|
raise ValueError(_("Superuser must have is_staff = True"))
|
|
if extra_fields.get('is_superuser') is not True:
|
|
raise ValueError(_("Superuser must have is_superuser = True"))
|
|
return self.create_user(email,password, **extra_fields)
|
|
|
|
|
|
|
|
class Base_User(AbstractBaseUser,PermissionsMixin):
|
|
email = models.EmailField(_('Email'), unique= True)
|
|
#phone = PhoneNumberField(null = False, blank = False, unique = True)
|
|
|
|
is_staff = models.BooleanField(default=False)
|
|
is_active = models.BooleanField(default=True)
|
|
|
|
date_joined = models.DateTimeField(_('Date joined'),auto_now_add=True)
|
|
#last_login = models.DateTimeField(_("Last Login"),auto_now_add= Tr ) #dodać funkcję w view która podczas kazdego logowania będzie zmieniala wartosc tego pola
|
|
status = models.CharField(max_length=32, choices=STATUS, default='BaseAcc')
|
|
|
|
USERNAME_FIELD = 'email'
|
|
|
|
objects = UserManager()
|
|
|
|
def __str__(self):
|
|
return self.email
|
|
|
|
class Meta:
|
|
verbose_name = "User"
|
|
verbose_name_plural = "Users"
|
|
|
|
|
|
class NormalUser(models.Model):
|
|
user = models.OneToOneField(Base_User, on_delete=models.CASCADE, blank= True,null = True )
|
|
|
|
#avatar = models.ImageField(upload_to='media/avatars', null = True, blank = True,)
|
|
first_name = models.CharField(_('First Name'), max_length=30, blank = False)
|
|
last_name = models.CharField(_('Last name'), max_length=30, blank = False)
|
|
birth_date = models.DateField(_('Birth date'),null=True, blank=True)
|
|
#dodac pola Country, state, postal code, city, address1, address2
|
|
|
|
|
|
|
|
class OwnerUser(models.Model):
|
|
user = models.OneToOneField(Base_User, on_delete=models.CASCADE,null = True)
|
|
|
|
first_name = models.CharField(_('First Name'), max_length=30, blank = False)
|
|
last_name = models.CharField(_('Last name'), max_length=30, blank = False)
|
|
company_name = models.CharField(_('Company name'), max_length=30, blank= True)
|
|
|
|
#avatar = models.ImageField(upload_to='media/avatars', null = True, blank = True,)
|
|
|
|
|
|
|
|
#dodac dane typu NIP/PESEL, adres, plik weryfikujący firmę / PESEL
|
|
|