74 lines
2.1 KiB
Python
74 lines
2.1 KiB
Python
|
import datetime
|
||
|
|
||
|
from django.conf import settings
|
||
|
from django.contrib.auth.models import (AbstractBaseUser, BaseUserManager,
|
||
|
PermissionsMixin)
|
||
|
from django.db import models
|
||
|
|
||
|
# Create your models here.
|
||
|
|
||
|
class UserAccountManager(BaseUserManager):
|
||
|
""" Manages the functions needed to create a user """
|
||
|
def create_user(self, email, password=None, **extra_fields):
|
||
|
""" Creates and saves a new user """
|
||
|
if not email:
|
||
|
raise ValueError("Users must have an email address")
|
||
|
|
||
|
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):
|
||
|
user = self.create_user(email, password, **extra_fields)
|
||
|
|
||
|
user.is_superuser = True
|
||
|
user.is_staff = True
|
||
|
user.save(using=self._db)
|
||
|
|
||
|
return user
|
||
|
|
||
|
|
||
|
class UserAccount(AbstractBaseUser, PermissionsMixin):
|
||
|
""" General user account fields """
|
||
|
|
||
|
username = models.CharField(max_length=2555, null=True, unique=True)
|
||
|
email = models.EmailField(max_length=255, unique=True)
|
||
|
first_name = models.CharField(max_length=255)
|
||
|
last_name = models.CharField(max_length=255)
|
||
|
avatar = models.ImageField(upload_to='avatar/', null=True, blank=True)
|
||
|
is_active = models.BooleanField(default=True)
|
||
|
is_staff = models.BooleanField(default=False)
|
||
|
get_newsletter = models.BooleanField(default=True, db_index=True)
|
||
|
|
||
|
objects = UserAccountManager()
|
||
|
|
||
|
USERNAME_FIELD = "email"
|
||
|
REQUIRED_FIELDS = ["first_name", "last_name"]
|
||
|
|
||
|
class Meta:
|
||
|
ordering = ['-id']
|
||
|
verbose_name = "User"
|
||
|
verbose_name_plural = "Users"
|
||
|
|
||
|
|
||
|
def get_short_name(self):
|
||
|
return self.first_name
|
||
|
|
||
|
def __str__(self):
|
||
|
return self.email
|
||
|
|
||
|
def get_or_create_userprofile(self):
|
||
|
from profiles.models import UserProfile
|
||
|
profile, _ = UserProfile.objects.get_or_create(user=self)
|
||
|
return profile
|
||
|
|
||
|
def get_avatar(self):
|
||
|
profile = self.get_or_create_userprofile()
|
||
|
return profile.avatar
|
||
|
|
||
|
|
||
|
|