Created
April 10, 2013 13:59
-
-
Save rvause/5354903 to your computer and use it in GitHub Desktop.
This file contains hidden or 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.auth.models import ( | |
| AbstractBaseUser, | |
| BaseUserManager, | |
| PermissionsMixin, | |
| ) | |
| 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(PermissionsMixin, 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') | |
| ) | |
| date_joined = models.DateTimeField(_('date joined'), default=timezone.now) | |
| 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 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