<gh_stars>0
from django.db import models

# Create your models here.
from django.db import models
from django.utils import timezone
from main.utils import country_choicer,UploadToPathAndRename
from django.contrib.auth.models import (
    BaseUserManager, AbstractBaseUser,PermissionsMixin
)



class UserManager(BaseUserManager):
    def create_user(self, email, password=None):
        """
        Creates and saves a User with the given email and password.
        """
        if not email:
            raise ValueError('Users must have an email address')

        user = self.model(
            email=self.normalize_email(email),
        )

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

    def create_staffuser(self, email, password):
        """
        Creates and saves a staff user with the given email and password.
        """
        user = self.create_user(
            email,
            password=password,
        )
        user.staff = True
        user.save(using=self._db)
        return user

    def create_superuser(self, email, password):
        """
        Creates and saves a superuser with the given email and password.
        """
        user = self.create_user(
            email,
            password=password,
        )
        user.staff = True
        user.admin = True
        user.save(using=self._db)
        return user

class User(AbstractBaseUser,PermissionsMixin):
    first_name = models.CharField(max_length=300,default=None,null=True,blank=True)
    last_name = models.CharField(max_length=300,default=None,null=True,blank=True)
    date_of_birth = models.DateField('Date of Birth', default=None, null=True,blank=True)
    phone_number = models.CharField(max_length=20,default=None,null=True,blank=True)
    email = models.EmailField(
        verbose_name='email address',
        max_length=255,
        unique=True,
    )
    avatar = models.ImageField(
        'Avatar',
        blank=True,
        null=True,
        upload_to=UploadToPathAndRename('users/avatars')
    )
    
    SEX_CHOICES = (
        ('male', 'Male'),
        ('female', 'Female'),
        ('others', 'Others')
    )

    gender = models.CharField(
        'Gender',
        choices=SEX_CHOICES,
        max_length=6,
        null=True,
        blank=True
    )

    country = models.CharField(
        'Country',
        max_length=2,
        choices=country_choicer(),
        blank=True
    )
    state = models.CharField('State', max_length=254, blank=True)
    city = models.CharField('City', max_length=254, blank=True)
    date_joined = models.DateTimeField('date joined', default=timezone.now)
    active = models.BooleanField(default=True)
    staff = models.BooleanField(default=False) # a admin user; non super-user
    admin = models.BooleanField(default=False) # a superuser
    # notice the absence of a "Password field", that is built in.
    objects = UserManager()

    username = None

    USERNAME_FIELD = 'email'
    
    REQUIRED_FIELDS = [] # Email & Password are required by default.
    
    @property
    def full_name(self):
        try:
            return self.first_name+" "+self.last_name
        except:
            try:
                return self.first_name
            except:
                return "No Name"
    @property
    def short_name(self):
        # The user is identified by their email address
        return self.email

    def __str__(self):              # __unicode__ on Python 2
        return self.email

    # def has_perm(self, perm, obj=None):
    #     "Does the user have a specific permission?"
    #     # Simplest possible answer: Yes, always
    #     return False

    # def has_module_perms(self, app_label):
    #     "Does the user have permissions to view the app `app_label`?"
    #     # Simplest possible answer: Yes, always
    #     return False

    @property
    def is_staff(self):
        "Is the user a member of staff?"
        return self.staff

    @property
    def is_admin(self):
        "Is the user a admin member?"
        return self.admin

    @property
    def is_active(self):
        "Is the user active?"
        return self.active
    @property
    def is_superuser(self):
        "Is the user active?"
        return self.admin

from django.db import models

# Create your models here.
from django.db import models
from django.utils import timezone
from main.utils import country_choicer,UploadToPathAndRename
from django.contrib.auth.models import (
    BaseUserManager, AbstractBaseUser,PermissionsMixin
)



class UserManager(BaseUserManager):
    def create_user(self, email, password=None):
        """
        Creates and saves a User with the given email and password.
        """
        if not email:
            raise ValueError('Users must have an email address')

        user = self.model(
            email=self.normalize_email(email),
        )

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

    def create_staffuser(self, email, password):
        """
        Creates and saves a staff user with the given email and password.
        """
        user = self.create_user(
            email,
            password=password,
        )
        user.staff = True
        user.save(using=self._db)
        return user

    def create_superuser(self, email, password):
        """
        Creates and saves a superuser with the given email and password.
        """
        user = self.create_user(
            email,
            password=password,
        )
        user.staff = True
        user.admin = True
        user.save(using=self._db)
        return user

class User(AbstractBaseUser,PermissionsMixin):
    first_name = models.CharField(max_length=300,default=None,null=True,blank=True)
    last_name = models.CharField(max_length=300,default=None,null=True,blank=True)
    date_of_birth = models.DateField('Date of Birth', default=None, null=True,blank=True)
    phone_number = models.CharField(max_length=20,default=None,null=True,blank=True)
    email = models.EmailField(
        verbose_name='email address',
        max_length=255,
        unique=True,
    )
    avatar = models.ImageField(
        'Avatar',
        blank=True,
        null=True,
        upload_to=UploadToPathAndRename('users/avatars')
    )
    
    SEX_CHOICES = (
        ('male', 'Male'),
        ('female', 'Female'),
        ('others', 'Others')
    )

    gender = models.CharField(
        'Gender',
        choices=SEX_CHOICES,
        max_length=6,
        null=True,
        blank=True
    )

    country = models.CharField(
        'Country',
        max_length=2,
        choices=country_choicer(),
        blank=True
    )
    state = models.CharField('State', max_length=254, blank=True)
    city = models.CharField('City', max_length=254, blank=True)
    date_joined = models.DateTimeField('date joined', default=timezone.now)
    active = models.BooleanField(default=True)
    staff = models.BooleanField(default=False) # a admin user; non super-user
    admin = models.BooleanField(default=False) # a superuser
    # notice the absence of a "Password field", that is built in.
    objects = UserManager()

    username = None

    USERNAME_FIELD = 'email'
    
    REQUIRED_FIELDS = [] # Email & Password are required by default.
    
    @property
    def full_name(self):
        try:
            return self.first_name+" "+self.last_name
        except:
            try:
                return self.first_name
            except:
                return "No Name"
    @property
    def short_name(self):
        # The user is identified by their email address
        return self.email

    def __str__(self):              # __unicode__ on Python 2
        return self.email

    # def has_perm(self, perm, obj=None):
    #     "Does the user have a specific permission?"
    #     # Simplest possible answer: Yes, always
    #     return False

    # def has_module_perms(self, app_label):
    #     "Does the user have permissions to view the app `app_label`?"
    #     # Simplest possible answer: Yes, always
    #     return False

    @property
    def is_staff(self):
        "Is the user a member of staff?"
        return self.staff

    @property
    def is_admin(self):
        "Is the user a admin member?"
        return self.admin

    @property
    def is_active(self):
        "Is the user active?"
        return self.active
    @property
    def is_superuser(self):
        "Is the user active?"
        return self.admin
# Create your models here. Creates and saves a User with the given email and password. Creates and saves a staff user with the given email and password. Creates and saves a superuser with the given email and password. # a admin user; non super-user # a superuser # notice the absence of a "Password field", that is built in. # Email & Password are required by default. # The user is identified by their email address # __unicode__ on Python 2 # def has_perm(self, perm, obj=None): #     "Does the user have a specific permission?" #     # Simplest possible answer: Yes, always #     return False # def has_module_perms(self, app_label): #     "Does the user have permissions to view the app `app_label`?" #     # Simplest possible answer: Yes, always #     return False