Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 1 | import os |
| 2 | import datetime |
| 3 | from django.db import models |
Tony Mack | 0e723b9 | 2013-04-27 11:08:19 -0400 | [diff] [blame] | 4 | from plstackapi.core.models import PlCoreBase |
| 5 | from plstackapi.core.models import Site |
Tony Mack | d685bfa | 2013-05-02 10:09:51 -0400 | [diff] [blame] | 6 | from plstackapi.openstack.manager import OpenStackManager |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 7 | from django.contrib.auth.models import User, AbstractBaseUser, UserManager, BaseUserManager |
| 8 | |
| 9 | # Create your models here. |
| 10 | |
| 11 | class 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 Mack | 6f0e353 | 2013-05-05 11:47:16 -0400 | [diff] [blame] | 23 | lastname=lastname, |
| 24 | password=password |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 25 | ) |
Tony Mack | 6f0e353 | 2013-05-05 11:47:16 -0400 | [diff] [blame] | 26 | #user.set_password(password) |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 27 | 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 | |
| 46 | class 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 Mack | 53106f3 | 2013-04-27 16:43:01 -0400 | [diff] [blame] | 58 | user_id = models.CharField(help_text="keystone user id", max_length=200) |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 59 | 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 Mack | 53106f3 | 2013-04-27 16:43:01 -0400 | [diff] [blame] | 103 | def save(self, *args, **kwds): |
Tony Mack | d685bfa | 2013-05-02 10:09:51 -0400 | [diff] [blame] | 104 | if not hasattr(self, 'os_manager'): |
| 105 | setattr(self, 'os_manager', OpenStackManager()) |
| 106 | |
| 107 | self.os_manager.save_user(self) |
Tony Mack | 6f0e353 | 2013-05-05 11:47:16 -0400 | [diff] [blame] | 108 | if not self.id: |
| 109 | self.set_password(self.password) |
Tony Mack | 53106f3 | 2013-04-27 16:43:01 -0400 | [diff] [blame] | 110 | super(PLUser, self).save(*args, **kwds) |
| 111 | |
| 112 | def delete(self, *args, **kwds): |
Tony Mack | d685bfa | 2013-05-02 10:09:51 -0400 | [diff] [blame] | 113 | if not hasattr(self, 'os_manager'): |
| 114 | setattr(self, 'os_manager', OpenStackManager()) |
| 115 | |
| 116 | self.os_manager.delete_user(self) |
Tony Mack | 53106f3 | 2013-04-27 16:43:01 -0400 | [diff] [blame] | 117 | super(PLUser, self).delete(*args, **kwds) |