Skip to content

Instantly share code, notes, and snippets.

@paolostefan
Last active April 20, 2016 13:32
Show Gist options
  • Save paolostefan/59a3b683ddd4f793e6a031bfdb8e068a to your computer and use it in GitHub Desktop.
Save paolostefan/59a3b683ddd4f793e6a031bfdb8e068a to your computer and use it in GitHub Desktop.
Custom user model
# coding=utf-8
from django.db import models
from django.contrib.auth.models import BaseUserManager, AbstractBaseUser
class CustomUserManager(BaseUserManager):
def create_user(self, email, username, date_of_birth, password=None):
"""
Creates and saves a User with the given email, date of
birth and password.
:param username:
:param password:
:param date_of_birth:
:param email:
"""
if not email:
raise ValueError('Users must have an email address')
user = self.model(
email=self.normalize_email(email),
username=username,
date_of_birth=date_of_birth
)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, email, username, date_of_birth, password):
"""
Creates and saves a superuser with the given email, date of
birth and password.
:param username:
:param email:
:param password:
:param date_of_birth:
"""
user = self.create_user(email,
username=username,
password=password,
date_of_birth=date_of_birth
)
user.is_admin = True
user.save(using=self._db)
return user
class CustomUser(AbstractBaseUser):
# Fields
email = models.EmailField(verbose_name='email address', max_length=96, unique=True)
username = models.TextField(max_length=32, unique=True)
first_name = models.TextField(max_length=24, db_index=True, blank=True, null=True)
last_name = models.TextField(max_length=24, db_index=True, blank=True, null=True)
date_of_birth = models.DateField()
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
subscribed_on = models.DateTimeField(auto_now_add=True, db_index=True)
# End fields
objects = CustomUserManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['date_of_birth', 'username']
def get_full_name(self):
# The user is identified by their email address
return self.email
def get_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 True
def has_module_perms(self, app_label):
"Does the user have permissions to view the app `app_label`?"
# Simplest possible answer: Yes, always
return True
@property
def is_staff(self):
"Is the user a member of staff?"
# Simplest possible answer: All admins are staff
return self.is_admin
class CustomGroup(models.Model):
"""
Custom user group
"""
name = models.CharField(max_length=48, unique=True)
boss = models.ForeignKey(CustomUser, related_name='groups_owned')
members = models.ManyToManyField(CustomUser, through='CustomUserInGroup')
def __unicode__(self):
return self.name
class CustomUserInGroup(models.Model):
"""
Link between CustomUser and CustomGroup
"""
group = models.ForeignKey(CustomGroup, related_name='customs')
custom = models.ForeignKey(CustomUser, related_name='groups')
joined_on = models.DateTimeField(auto_now_add=True, blank=True, null=True)
class Meta:
unique_together = ('group','custom')
def __unicode__(self):
return "{} in {}".format(self.custom, self.group)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment