Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 1 | import os |
| 2 | import datetime |
| 3 | from django.db import models |
| 4 | from core.models import PlCoreBase |
| 5 | from core.models import Site |
| 6 | from django.contrib.auth.models import User, AbstractBaseUser, UserManager, BaseUserManager |
| 7 | |
| 8 | # Create your models here. |
| 9 | |
| 10 | class PLUserManager(BaseUserManager): |
| 11 | def create_user(self, email, firstname, lastname, password=None): |
| 12 | """ |
| 13 | Creates and saves a User with the given email, date of |
| 14 | birth and password. |
| 15 | """ |
| 16 | if not email: |
| 17 | raise ValueError('Users must have an email address') |
| 18 | |
| 19 | user = self.model( |
| 20 | email=PLUserManager.normalize_email(email), |
| 21 | firstname=firstname, |
| 22 | lastname=lastname |
| 23 | ) |
| 24 | |
| 25 | user.set_password(password) |
| 26 | user.is_admin = True |
| 27 | user.save(using=self._db) |
| 28 | return user |
| 29 | |
| 30 | def create_superuser(self, email, firstname, lastname, password): |
| 31 | """ |
| 32 | Creates and saves a superuser with the given email, date of |
| 33 | birth and password. |
| 34 | """ |
| 35 | user = self.create_user(email, |
| 36 | password=password, |
| 37 | firstname=firstname, |
| 38 | lastname=lastname |
| 39 | ) |
| 40 | user.is_admin = True |
| 41 | user.save(using=self._db) |
| 42 | return user |
| 43 | |
| 44 | |
| 45 | class PLUser(AbstractBaseUser): |
| 46 | |
| 47 | class Meta: |
| 48 | app_label = "core" |
| 49 | |
| 50 | email = models.EmailField( |
| 51 | verbose_name='email address', |
| 52 | max_length=255, |
| 53 | unique=True, |
| 54 | db_index=True, |
| 55 | ) |
| 56 | |
| 57 | |
| 58 | firstname = models.CharField(help_text="person's given name", max_length=200) |
| 59 | lastname = models.CharField(help_text="person's surname", max_length=200) |
| 60 | |
| 61 | phone = models.CharField(null=True, blank=True, help_text="phone number contact", max_length=100) |
| 62 | user_url = models.URLField(null=True, blank=True) |
| 63 | site = models.ForeignKey(Site, related_name='users', verbose_name="Site this user will be homed too", null=True) |
| 64 | |
| 65 | is_active = models.BooleanField(default=True) |
| 66 | is_admin = models.BooleanField(default=True) |
| 67 | is_staff = models.BooleanField(default=True) |
| 68 | |
| 69 | objects = PLUserManager() |
| 70 | |
| 71 | USERNAME_FIELD = 'email' |
| 72 | REQUIRED_FIELDS = ['firstname', 'lastname'] |
| 73 | |
| 74 | def get_full_name(self): |
| 75 | # The user is identified by their email address |
| 76 | return self.email |
| 77 | |
| 78 | def get_short_name(self): |
| 79 | # The user is identified by their email address |
| 80 | return self.email |
| 81 | |
| 82 | def __unicode__(self): |
| 83 | return self.email |
| 84 | |
| 85 | def has_perm(self, perm, obj=None): |
| 86 | "Does the user have a specific permission?" |
| 87 | # Simplest possible answer: Yes, always |
| 88 | return True |
| 89 | |
| 90 | def has_module_perms(self, app_label): |
| 91 | "Does the user have permissions to view the app `app_label`?" |
| 92 | # Simplest possible answer: Yes, always |
| 93 | return True |
| 94 | |
| 95 | @property |
| 96 | def is_staff(self): |
| 97 | "Is the user a member of staff?" |
| 98 | # Simplest possible answer: All admins are staff |
| 99 | return self.is_admin |
| 100 | |
| 101 | |