Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 1 | import os |
| 2 | import datetime |
| 3 | from django.db import models |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame^] | 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 |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 9 | |
| 10 | # Create your models here. |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame^] | 11 | has_openstack = False |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 12 | |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame^] | 13 | class UserManager(BaseUserManager): |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 14 | def create_user(self, email, firstname, lastname, password=None): |
| 15 | """ |
| 16 | Creates and saves a User with the given email, date of |
| 17 | birth and password. |
| 18 | """ |
| 19 | if not email: |
| 20 | raise ValueError('Users must have an email address') |
| 21 | |
| 22 | user = self.model( |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame^] | 23 | email=UserManager.normalize_email(email), |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 24 | firstname=firstname, |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame^] | 25 | lastname=lastname, |
| 26 | password=password |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 27 | ) |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame^] | 28 | #user.set_password(password) |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 29 | user.is_admin = True |
| 30 | user.save(using=self._db) |
| 31 | return user |
| 32 | |
| 33 | def create_superuser(self, email, firstname, lastname, password): |
| 34 | """ |
| 35 | Creates and saves a superuser with the given email, date of |
| 36 | birth and password. |
| 37 | """ |
| 38 | user = self.create_user(email, |
| 39 | password=password, |
| 40 | firstname=firstname, |
| 41 | lastname=lastname |
| 42 | ) |
| 43 | user.is_admin = True |
| 44 | user.save(using=self._db) |
| 45 | return user |
| 46 | |
| 47 | |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame^] | 48 | class User(AbstractBaseUser): |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 49 | |
| 50 | class Meta: |
| 51 | app_label = "core" |
| 52 | |
| 53 | email = models.EmailField( |
| 54 | verbose_name='email address', |
| 55 | max_length=255, |
| 56 | unique=True, |
| 57 | db_index=True, |
| 58 | ) |
| 59 | |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame^] | 60 | kuser_id = models.CharField(help_text="keystone user id", max_length=200) |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 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) |
| 63 | |
| 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) |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame^] | 67 | key = models.ForeignKey(Key, related_name='user', null=True, blank=True) |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 68 | |
| 69 | is_active = models.BooleanField(default=True) |
| 70 | is_admin = models.BooleanField(default=True) |
| 71 | is_staff = models.BooleanField(default=True) |
| 72 | |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame^] | 73 | objects = UserManager() |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 74 | |
| 75 | USERNAME_FIELD = 'email' |
| 76 | REQUIRED_FIELDS = ['firstname', 'lastname'] |
| 77 | |
| 78 | def get_full_name(self): |
| 79 | # The user is identified by their email address |
| 80 | return self.email |
| 81 | |
| 82 | def get_short_name(self): |
| 83 | # The user is identified by their email address |
| 84 | return self.email |
| 85 | |
| 86 | def __unicode__(self): |
| 87 | return self.email |
| 88 | |
| 89 | def has_perm(self, perm, obj=None): |
| 90 | "Does the user have a specific permission?" |
| 91 | # Simplest possible answer: Yes, always |
| 92 | return True |
| 93 | |
| 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 |
| 97 | return True |
| 98 | |
| 99 | @property |
| 100 | def is_staff(self): |
| 101 | "Is the user a member of staff?" |
| 102 | # Simplest possible answer: All admins are staff |
| 103 | return self.is_admin |
| 104 | |
| 105 | |
Tony Mack | 53106f3 | 2013-04-27 16:43:01 -0400 | [diff] [blame] | 106 | def save(self, *args, **kwds): |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame^] | 107 | if has_openstack: |
| 108 | if not hasattr(self, 'os_manager'): |
| 109 | setattr(self, 'os_manager', OpenStackManager()) |
Tony Mack | d685bfa | 2013-05-02 10:09:51 -0400 | [diff] [blame] | 110 | |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame^] | 111 | self.os_manager.save_user(self) |
| 112 | if not self.id: |
| 113 | self.set_password(self.password) |
| 114 | super(User, self).save(*args, **kwds) |
Tony Mack | 53106f3 | 2013-04-27 16:43:01 -0400 | [diff] [blame] | 115 | |
| 116 | def delete(self, *args, **kwds): |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame^] | 117 | if has_openstack: |
| 118 | if not hasattr(self, 'os_manager'): |
| 119 | setattr(self, 'os_manager', OpenStackManager()) |
Tony Mack | d685bfa | 2013-05-02 10:09:51 -0400 | [diff] [blame] | 120 | |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame^] | 121 | self.os_manager.delete_user(self) |
| 122 | super(User, self).delete(*args, **kwds) |