blob: c21601c4b761aa71a23d85a7fd8a1cd426333fe8 [file] [log] [blame]
Siobhan Tully4bc09f22013-04-10 21:15:21 -04001import os
2import datetime
3from django.db import models
4from plstackapi.core.models import PlCoreBase
5from plstackapi.core.models import Site
Tony Mack8484bdb2013-04-14 20:26:03 -04006from plstackapi.openstack.driver import OpenStackDriver
7
Siobhan Tully4bc09f22013-04-10 21:15:21 -04008
9# Create your models here.
10
11class User(PlCoreBase):
Tony Mack8484bdb2013-04-14 20:26:03 -040012 user_id = models.CharField(max_length=256, unique=True, blank=True)
Siobhan Tully4bc09f22013-04-10 21:15:21 -040013 firstname = models.CharField(help_text="person's given name", max_length=200)
14 lastname = models.CharField(help_text="person's surname", max_length=200)
Tony Mack8484bdb2013-04-14 20:26:03 -040015 email = models.EmailField(help_text="e-mail address", null=True)
16 password = models.CharField(max_length=256, null=True, blank=True)
17
Siobhan Tully4bc09f22013-04-10 21:15:21 -040018 phone = models.CharField(null=True, blank=True, help_text="phone number contact", max_length=100)
19 user_url = models.URLField(null=True, blank=True)
20 is_admin = models.BooleanField(default=False)
21 enabled = models.BooleanField(default=True, help_text="Status for this User")
22 site = models.ForeignKey(Site, related_name='users', verbose_name="Site this user will be homed too")
23
24 def __unicode__(self): return u'%s' % (self.email)
Tony Mack8484bdb2013-04-14 20:26:03 -040025
26 def save(self, *args, **kwds):
27 driver = OpenStackDriver()
28 if not self.user_id:
29 name = self.email[:self.email.find('@')]
30 user_fields = {'name': name,
31 'email': self.email,
32 'password': self.password,
33 'enabled': self.enabled}
34 user = driver.create_user(**user_fields)
35 self.user_id = user.id
36
37 self.password = None
38 super(User, self).save(*args, **kwds)
39
40 def delete(self, *args, **kwds):
41 driver = OpenStackDriver()
42 if self.user_id:
43 driver.delete_user(self.user_id)
44 super(User, self).delete(*args, **kwds)