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')), ('Moderator',_('Moderator')), ) 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) extra_fields.setdefault('status','Moderator') 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): id = models.AutoField(primary_key= True) email = models.EmailField(_('Email'), unique= True) #phone = PhoneNumberField(null = False, blank = False, unique = True) first_name = models.CharField(_('First Name'), max_length=30, blank = False, default = '') last_name = models.CharField(_('Last name'), max_length=30, blank = False,default = '') avatar = models.ImageField(upload_to='media/avatars', null = True, blank = 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 ) 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,) birth_date = models.DateField(_('Birth date'),null=True, blank=True) #add fields Country, state, postal code, city, address1, address2 class OwnerUser(models.Model): user = models.OneToOneField(Base_User, on_delete=models.CASCADE,null = True) company_name = models.CharField(_('Company name'), max_length=30, blank= True)