2020-12-07 21:41:55 +01:00
|
|
|
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')),
|
2020-12-27 01:33:07 +01:00
|
|
|
('Moderaotr',_('Moderator')),
|
2020-12-07 21:41:55 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
2020-12-27 01:33:07 +01:00
|
|
|
extra_fields.setdefault('status','Moderator')
|
2020-12-07 21:41:55 +01:00
|
|
|
|
|
|
|
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):
|
2020-12-27 01:33:07 +01:00
|
|
|
id = models.AutoField(primary_key= True)
|
2020-12-07 21:41:55 +01:00
|
|
|
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)
|
2020-12-07 21:45:56 +01:00
|
|
|
#last_login = models.DateTimeField(_("Last Login"),auto_now_add= Tr )
|
2020-12-07 21:41:55 +01:00
|
|
|
status = models.CharField(max_length=32, choices=STATUS, default='BaseAcc')
|
|
|
|
|
|
|
|
USERNAME_FIELD = 'email'
|
|
|
|
|
|
|
|
objects = UserManager()
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.email
|
|
|
|
|
2020-12-27 01:33:07 +01:00
|
|
|
|
2020-12-07 21:41:55 +01:00
|
|
|
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)
|
2020-12-07 21:45:56 +01:00
|
|
|
#add fields Country, state, postal code, city, address1, address2
|
2020-12-07 21:41:55 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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,)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|