2021-03-01 09:56:10 +00:00
|
|
|
from django.db import models
|
|
|
|
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
|
|
|
|
from django.contrib.auth import get_user_model
|
2021-03-21 15:50:50 +00:00
|
|
|
from django.utils import timezone
|
2021-03-01 09:56:10 +00:00
|
|
|
|
2021-03-22 17:42:07 +00:00
|
|
|
from django_countries.fields import CountryField
|
|
|
|
|
2021-03-01 09:56:10 +00:00
|
|
|
class UserManager(BaseUserManager):
|
|
|
|
use_in_migrations = True
|
|
|
|
|
2021-03-21 15:50:50 +00:00
|
|
|
def _create_user(self, email, display_name, password, **extra_fields):
|
|
|
|
values = [email, display_name]
|
|
|
|
field_value_map = dict(zip(self.model.REQUIRED_FIELDS, values))
|
|
|
|
for field_name, value in field_value_map.items():
|
|
|
|
if not value:
|
|
|
|
raise ValueError('The {} value must be set'.format(field_name))
|
|
|
|
|
2021-03-01 09:56:10 +00:00
|
|
|
email = self.normalize_email(email)
|
2021-03-21 15:50:50 +00:00
|
|
|
user = self.model(
|
|
|
|
email=email,
|
|
|
|
display_name=display_name,
|
|
|
|
**extra_fields
|
|
|
|
)
|
2021-03-01 09:56:10 +00:00
|
|
|
user.set_password(password)
|
|
|
|
user.save(using=self._db)
|
|
|
|
return user
|
|
|
|
|
2021-03-21 15:50:50 +00:00
|
|
|
def create_user(self, email, display_name, password=None, **extra_fields):
|
|
|
|
return self._create_user(email, display_name, password, **extra_fields)
|
2021-03-01 09:56:10 +00:00
|
|
|
|
2021-03-21 15:50:50 +00:00
|
|
|
def create_superuser(self, email, display_name, password=None, **extra_fields):
|
|
|
|
extra_fields.setdefault('is_superuser', True)
|
|
|
|
return self._create_user(email, display_name, password, **extra_fields)
|
2021-03-01 09:56:10 +00:00
|
|
|
|
2021-03-21 15:50:50 +00:00
|
|
|
class User(AbstractBaseUser):
|
|
|
|
email = models.EmailField(unique=True)
|
|
|
|
is_active = models.BooleanField(default=True)
|
|
|
|
date_joined = models.DateTimeField(default=timezone.now)
|
|
|
|
last_login = models.DateTimeField(null=True)
|
|
|
|
is_superuser = models.BooleanField(default=False)
|
2021-03-01 09:56:10 +00:00
|
|
|
|
2021-03-21 15:50:50 +00:00
|
|
|
objects = UserManager()
|
2021-03-01 09:56:10 +00:00
|
|
|
|
|
|
|
USERNAME_FIELD = 'email'
|
2021-03-21 15:50:50 +00:00
|
|
|
REQUIRED_FIELDS = ['display_name']
|
2021-03-01 09:56:10 +00:00
|
|
|
|
|
|
|
def get_full_name(self):
|
|
|
|
return self.email
|
|
|
|
|
|
|
|
def get_short_name(self):
|
|
|
|
return self.email
|
|
|
|
|
2021-03-21 15:50:50 +00:00
|
|
|
def has_permission(self, *args, **kwargs):
|
|
|
|
return self.is_superuser
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_staff(self):
|
|
|
|
return self.is_superuser
|
|
|
|
|
|
|
|
has_module_perms = has_permission
|
2021-03-22 17:42:07 +00:00
|
|
|
has_perm = has_permission
|
|
|
|
|
|
|
|
class Profile(models.Model):
|
|
|
|
user = models.OneToOneField(User, models.CASCADE)
|
|
|
|
company = models.CharField(max_length=64, null=True, blank=True)
|
|
|
|
vat_id = models.CharField(max_length=32, null=True, blank=True)
|
|
|
|
first_name = models.CharField(max_length=64)
|
|
|
|
last_name = models.CharField(max_length=64)
|
|
|
|
street = models.CharField(max_length=64)
|
|
|
|
city = models.CharField(max_length=64)
|
|
|
|
zip = models.CharField(max_length=16)
|
|
|
|
state = models.CharField(max_length=64)
|
|
|
|
country = CountryField()
|
2021-03-23 18:23:04 +00:00
|
|
|
verified = models.BooleanField(default=False)
|
|
|
|
enabled = models.BooleanField(default=True)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def full_name(self):
|
|
|
|
if self.company:
|
|
|
|
return self.company
|
|
|
|
else:
|
|
|
|
return " ".join([self.first_name, self.last_name])
|
2021-03-22 17:42:07 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|