Refactor to /opt/planetstack, final tweaks to make sure planetstack can run in non...
[plstackapi.git] / planetstack / core / models / user.py
1 import os
2 import datetime
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
9
10 # Create your models here.
11 has_openstack = False
12
13 class UserManager(BaseUserManager):
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(
23             email=UserManager.normalize_email(email),
24             firstname=firstname,
25             lastname=lastname,
26             password=password
27         )
28         #user.set_password(password)
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
48 class User(AbstractBaseUser):
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
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)
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)
67     key = models.ForeignKey(Key, related_name='user', null=True, blank=True)
68
69     is_active = models.BooleanField(default=True)
70     is_admin = models.BooleanField(default=True)
71     is_staff = models.BooleanField(default=True)
72
73     objects = UserManager()
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
106     def save(self, *args, **kwds):
107         if has_openstack:
108             if not hasattr(self, 'os_manager'):
109                 setattr(self, 'os_manager', OpenStackManager())
110
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)   
115
116     def delete(self, *args, **kwds):
117         if has_openstack:
118             if not hasattr(self, 'os_manager'):
119                 setattr(self, 'os_manager', OpenStackManager())
120
121             self.os_manager.delete_user(self)
122         super(User, self).delete(*args, **kwds)