Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 1 | import os |
| 2 | import datetime |
| 3 | from django.db import models |
| 4 | from plstackapi.core.models import PlCoreBase |
| 5 | from plstackapi.core.models import Site |
Tony Mack | 8484bdb | 2013-04-14 20:26:03 -0400 | [diff] [blame] | 6 | from plstackapi.openstack.driver import OpenStackDriver |
| 7 | |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 8 | |
| 9 | # Create your models here. |
| 10 | |
| 11 | class User(PlCoreBase): |
Tony Mack | 8484bdb | 2013-04-14 20:26:03 -0400 | [diff] [blame] | 12 | user_id = models.CharField(max_length=256, unique=True, blank=True) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 13 | 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 Mack | 8484bdb | 2013-04-14 20:26:03 -0400 | [diff] [blame] | 15 | email = models.EmailField(help_text="e-mail address", null=True) |
| 16 | password = models.CharField(max_length=256, null=True, blank=True) |
| 17 | |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 18 | 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 Mack | 8484bdb | 2013-04-14 20:26:03 -0400 | [diff] [blame] | 25 | |
| 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) |