3 from django.db import models
4 from core.models import PlCoreBase
5 from core.models import Site
6 from core.models import Key
7 from openstack.manager import OpenStackManager
8 from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
10 # Create your models here.
13 class UserManager(BaseUserManager):
14 def create_user(self, email, firstname, lastname, password=None):
16 Creates and saves a User with the given email, date of
20 raise ValueError('Users must have an email address')
23 email=UserManager.normalize_email(email),
28 #user.set_password(password)
30 user.save(using=self._db)
33 def create_superuser(self, email, firstname, lastname, password):
35 Creates and saves a superuser with the given email, date of
38 user = self.create_user(email,
44 user.save(using=self._db)
48 class User(AbstractBaseUser):
53 email = models.EmailField(
54 verbose_name='email address',
60 kuser_id = models.CharField(help_text="keystone user id", max_length=200)
61 firstname = models.CharField(help_text="person's given name", max_length=200)
62 lastname = models.CharField(help_text="person's surname", max_length=200)
64 phone = models.CharField(null=True, blank=True, help_text="phone number contact", max_length=100)
65 user_url = models.URLField(null=True, blank=True)
66 site = models.ForeignKey(Site, related_name='users', verbose_name="Site this user will be homed too", null=True)
67 key = models.ForeignKey(Key, related_name='user', null=True, blank=True)
69 is_active = models.BooleanField(default=True)
70 is_admin = models.BooleanField(default=True)
71 is_staff = models.BooleanField(default=True)
73 objects = UserManager()
75 USERNAME_FIELD = 'email'
76 REQUIRED_FIELDS = ['firstname', 'lastname']
78 def get_full_name(self):
79 # The user is identified by their email address
82 def get_short_name(self):
83 # The user is identified by their email address
86 def __unicode__(self):
89 def has_perm(self, perm, obj=None):
90 "Does the user have a specific permission?"
91 # Simplest possible answer: Yes, always
94 def has_module_perms(self, app_label):
95 "Does the user have permissions to view the app `app_label`?"
96 # Simplest possible answer: Yes, always
101 "Is the user a member of staff?"
102 # Simplest possible answer: All admins are staff
106 def save(self, *args, **kwds):
108 if not hasattr(self, 'os_manager'):
109 setattr(self, 'os_manager', OpenStackManager())
111 self.os_manager.save_user(self)
113 self.set_password(self.password)
114 super(User, self).save(*args, **kwds)
116 def delete(self, *args, **kwds):
118 if not hasattr(self, 'os_manager'):
119 setattr(self, 'os_manager', OpenStackManager())
121 self.os_manager.delete_user(self)
122 super(User, self).delete(*args, **kwds)