Created
December 17, 2012 09:42
-
-
Save rvause/4317098 to your computer and use it in GitHub Desktop.
User model for Django with an email/password login, no username
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import models | |
from django.core.mail import send_mail | |
from django.contrib import auth | |
from django.contrib.auth.models import ( | |
AbstractBaseUser, | |
BaseUserManager, | |
Group, | |
Permission, | |
_user_get_all_permissions, | |
_user_has_perm, | |
_user_has_module_perms, | |
) | |
from django.utils.translation import ugettext_lazy as _ | |
from django.utils import timezone | |
class UserManager(BaseUserManager): | |
def create_user(self, email, password=None, **kw): | |
""" | |
Creates a user with a given email address and password | |
""" | |
if not email: | |
raise ValueError('The given email address must be set') | |
u = self.model( | |
email=UserManager.normalize_email(email), | |
**kw | |
) | |
u.set_password(password) | |
u.save(using=self._db) | |
return u | |
def create_superuser(self, email, password, **kw): | |
""" | |
Creates a superuser with a given email address and password | |
""" | |
u = self.create_user(email, password, **kw) | |
u.is_staff = True | |
u.is_active = True | |
u.is_superuser = True | |
u.save(using=self._db) | |
return u | |
class User(AbstractBaseUser): | |
""" | |
Model for users across the site | |
""" | |
email = models.EmailField(_('email_address'), max_length=255, unique=True) | |
first_name = models.CharField(_('first name'), max_length=30, blank=True) | |
last_name = models.CharField(_('last name'), max_length=30, blank=True) | |
is_active = models.BooleanField( | |
_('active'), | |
default=True, | |
help_text=_('Disable this instead of deleting a user') | |
) | |
is_staff = models.BooleanField( | |
_('staff status'), | |
default=False, | |
help_text=_('Allow the user access to the admin site') | |
) | |
is_superuser = models.BooleanField( | |
_('superuser status'), | |
default=False, | |
help_text=_('User has all permissions') | |
) | |
date_joined = models.DateTimeField(_('date joined'), default=timezone.now) | |
groups = models.ManyToManyField( | |
Group, | |
verbose_name=_('groups'), | |
blank=True, | |
help_text=_('Groups a user belongs to and inherits permissions from') | |
) | |
user_permissions = models.ManyToManyField( | |
Permission, | |
verbose_name=_('user permissions'), | |
blank=True, | |
help_text=_('Permissions for this user') | |
) | |
objects = UserManager() | |
USERNAME_FIELD = 'email' | |
class Meta: | |
verbose_name = _('user') | |
verbose_name_plural = _('users') | |
def get_full_name(self): | |
""" | |
Return the first name and last name | |
""" | |
full_name = '%s %s' % (self.first_name, self.last_name) | |
return full_name.strip() | |
def get_short_name(self): | |
""" | |
Since the user is identified by their email address return that | |
""" | |
return self.email | |
def get_group_permissions(self, obj=None): | |
""" | |
Returns a list of permissions that this user has through their groups | |
""" | |
permissions = set() | |
for backend in auth.get_backends(): | |
if hasattr(backend, 'get_group_permissions'): | |
if obj is not None: | |
permissions.update(backend.get_group_permissions( | |
self, obj | |
)) | |
else: | |
permissions.update(backend.get_group_permissions(self)) | |
return permissions | |
def get_all_permissions(self, obj=None): | |
return _user_get_all_permissions(self, obj) | |
def has_perm(self, perm, obj=None): | |
""" | |
Returns True if the user has the given permission | |
""" | |
if self.is_active and self.is_superuser: | |
return True | |
return _user_has_perm(self, perm, obj) | |
def has_perms(self, perm_list, obj=None): | |
""" | |
Returns True if the user has each of the permissions in the give list | |
""" | |
for perm in perm_list: | |
if not self.has_perm(perm, obj): | |
return False | |
return True | |
def has_module_perms(self, app_label): | |
""" | |
Returns True if the user has any permissions in the give app label | |
""" | |
if self.is_active and self.is_superuser: | |
return True | |
return _user_has_module_perms(self, app_label) | |
def email_user(self, subject, message, from_email=None): | |
""" | |
Sends an email to the user | |
""" | |
send_mail(subject, message, from_email, [self.email]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment