blob: b64f223d7fa69e3390f32ef17e8296fccbcdd998 [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
Tony Mackd685bfa2013-05-02 10:09:51 -04006from plstackapi.openstack.manager import OpenStackManager
Siobhan Tully53437282013-04-26 19:30:27 -04007from django.contrib.auth.models import User, AbstractBaseUser, UserManager, BaseUserManager
8
9# Create your models here.
10
11class PLUserManager(BaseUserManager):
12 def create_user(self, email, firstname, lastname, password=None):
13 """
14 Creates and saves a User with the given email, date of
15 birth and password.
16 """
17 if not email:
18 raise ValueError('Users must have an email address')
19
20 user = self.model(
21 email=PLUserManager.normalize_email(email),
22 firstname=firstname,
Tony Mack6f0e3532013-05-05 11:47:16 -040023 lastname=lastname,
24 password=password
Siobhan Tully53437282013-04-26 19:30:27 -040025 )
Tony Mack6f0e3532013-05-05 11:47:16 -040026 #user.set_password(password)
Siobhan Tully53437282013-04-26 19:30:27 -040027 user.is_admin = True
28 user.save(using=self._db)
29 return user
30
31 def create_superuser(self, email, firstname, lastname, password):
32 """
33 Creates and saves a superuser with the given email, date of
34 birth and password.
35 """
36 user = self.create_user(email,
37 password=password,
38 firstname=firstname,
39 lastname=lastname
40 )
41 user.is_admin = True
42 user.save(using=self._db)
43 return user
44
45
46class PLUser(AbstractBaseUser):
47
48 class Meta:
49 app_label = "core"
50
51 email = models.EmailField(
52 verbose_name='email address',
53 max_length=255,
54 unique=True,
55 db_index=True,
56 )
57
Tony Mack53106f32013-04-27 16:43:01 -040058 user_id = models.CharField(help_text="keystone user id", max_length=200)
Siobhan Tully53437282013-04-26 19:30:27 -040059 firstname = models.CharField(help_text="person's given name", max_length=200)
60 lastname = models.CharField(help_text="person's surname", max_length=200)
61
62 phone = models.CharField(null=True, blank=True, help_text="phone number contact", max_length=100)
63 user_url = models.URLField(null=True, blank=True)
64 site = models.ForeignKey(Site, related_name='users', verbose_name="Site this user will be homed too", null=True)
65
66 is_active = models.BooleanField(default=True)
67 is_admin = models.BooleanField(default=True)
68 is_staff = models.BooleanField(default=True)
69
70 objects = PLUserManager()
71
72 USERNAME_FIELD = 'email'
73 REQUIRED_FIELDS = ['firstname', 'lastname']
74
75 def get_full_name(self):
76 # The user is identified by their email address
77 return self.email
78
79 def get_short_name(self):
80 # The user is identified by their email address
81 return self.email
82
83 def __unicode__(self):
84 return self.email
85
86 def has_perm(self, perm, obj=None):
87 "Does the user have a specific permission?"
88 # Simplest possible answer: Yes, always
89 return True
90
91 def has_module_perms(self, app_label):
92 "Does the user have permissions to view the app `app_label`?"
93 # Simplest possible answer: Yes, always
94 return True
95
96 @property
97 def is_staff(self):
98 "Is the user a member of staff?"
99 # Simplest possible answer: All admins are staff
100 return self.is_admin
101
102
Tony Mack53106f32013-04-27 16:43:01 -0400103 def save(self, *args, **kwds):
Tony Mackd685bfa2013-05-02 10:09:51 -0400104 if not hasattr(self, 'os_manager'):
105 setattr(self, 'os_manager', OpenStackManager())
106
107 self.os_manager.save_user(self)
Tony Mack6f0e3532013-05-05 11:47:16 -0400108 if not self.id:
109 self.set_password(self.password)
Tony Mack53106f32013-04-27 16:43:01 -0400110 super(PLUser, self).save(*args, **kwds)
111
112 def delete(self, *args, **kwds):
Tony Mackd685bfa2013-05-02 10:09:51 -0400113 if not hasattr(self, 'os_manager'):
114 setattr(self, 'os_manager', OpenStackManager())
115
116 self.os_manager.delete_user(self)
Tony Mack53106f32013-04-27 16:43:01 -0400117 super(PLUser, self).delete(*args, **kwds)