blob: 6688cfe2dad38cc911919c94978d17bf3d39a8d8 [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 Mack85d18832013-05-09 17:02:31 -040058 user_id = models.CharField(null=True, blank=True, 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)
Tony Mack85d18832013-05-09 17:02:31 -040067 is_admin = models.BooleanField(default=False)
Siobhan Tully53437282013-04-26 19:30:27 -040068 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
Tony Mack53106f32013-04-27 16:43:01 -040096 def save(self, *args, **kwds):
Tony Mackd685bfa2013-05-02 10:09:51 -040097 if not hasattr(self, 'os_manager'):
98 setattr(self, 'os_manager', OpenStackManager())
99
100 self.os_manager.save_user(self)
Tony Mack6f0e3532013-05-05 11:47:16 -0400101 if not self.id:
102 self.set_password(self.password)
Tony Mack53106f32013-04-27 16:43:01 -0400103 super(PLUser, self).save(*args, **kwds)
104
105 def delete(self, *args, **kwds):
Tony Mackd685bfa2013-05-02 10:09:51 -0400106 if not hasattr(self, 'os_manager'):
107 setattr(self, 'os_manager', OpenStackManager())
108
109 self.os_manager.delete_user(self)
Tony Mack53106f32013-04-27 16:43:01 -0400110 super(PLUser, self).delete(*args, **kwds)