blob: 0e8f9d9e1ae7b3cbb771db6abfd8274376f01a81 [file] [log] [blame]
Siobhan Tully53437282013-04-26 19:30:27 -04001import os
2import datetime
3from django.db import models
Tony Mack0e723b92013-04-27 11:08:19 -04004from plstackapi.core.models import PlCoreBase
5from plstackapi.core.models import Site
Siobhan Tully53437282013-04-26 19:30:27 -04006from django.contrib.auth.models import User, AbstractBaseUser, UserManager, BaseUserManager
7
8# Create your models here.
9
10class 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
45class 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