2014-09-29 13:34:38 +00:00
|
|
|
from django.contrib.auth.hashers import make_password
|
2014-05-08 16:59:35 +00:00
|
|
|
from django.core import validators
|
2014-09-26 15:05:20 +00:00
|
|
|
from django.core.mail import send_mail
|
2014-05-08 16:59:35 +00:00
|
|
|
from django.db import models
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
2014-09-29 13:34:38 +00:00
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
from orchestra.core import services
|
|
|
|
|
|
|
|
|
2014-09-29 13:34:38 +00:00
|
|
|
class User(models.Model):
|
2014-05-08 16:59:35 +00:00
|
|
|
username = models.CharField(_("username"), max_length=64, unique=True,
|
|
|
|
help_text=_("Required. 30 characters or fewer. Letters, digits and "
|
2014-08-22 15:31:44 +00:00
|
|
|
"./-/_ only."),
|
|
|
|
validators=[validators.RegexValidator(r'^[\w.-]+$',
|
2014-05-08 16:59:35 +00:00
|
|
|
_("Enter a valid username."), 'invalid')])
|
2014-09-29 13:34:38 +00:00
|
|
|
password = models.CharField(_("password"), max_length=128)
|
2014-05-08 16:59:35 +00:00
|
|
|
account = models.ForeignKey('accounts.Account', verbose_name=_("Account"),
|
2014-09-10 16:53:09 +00:00
|
|
|
related_name='users')
|
2014-05-08 16:59:35 +00:00
|
|
|
first_name = models.CharField(_("first name"), max_length=30, blank=True)
|
|
|
|
last_name = models.CharField(_("last name"), max_length=30, blank=True)
|
|
|
|
email = models.EmailField(_('email address'), blank=True)
|
|
|
|
is_active = models.BooleanField(_("active"), default=True,
|
2014-09-29 13:34:38 +00:00
|
|
|
help_text=_("Designates whether this account should be treated as active. "
|
|
|
|
"Unselect this instead of deleting accounts."))
|
2014-05-08 16:59:35 +00:00
|
|
|
|
2014-07-09 16:17:43 +00:00
|
|
|
@property
|
|
|
|
def is_main(self):
|
2014-09-29 13:34:38 +00:00
|
|
|
return self.username == self.account.username
|
2014-07-09 16:17:43 +00:00
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
def get_full_name(self):
|
|
|
|
full_name = '%s %s' % (self.first_name, self.last_name)
|
2014-05-13 13:46:40 +00:00
|
|
|
return full_name.strip() or self.username
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
def get_short_name(self):
|
|
|
|
""" Returns the short name for the user """
|
|
|
|
return self.first_name
|
|
|
|
|
2014-09-29 13:34:38 +00:00
|
|
|
def get_description(self):
|
|
|
|
return "{full_name}, {email}".format(full_name=self.get_full_name(), email=self.email)
|
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
def email_user(self, subject, message, from_email=None, **kwargs):
|
|
|
|
""" Sends an email to this User """
|
|
|
|
send_mail(subject, message, from_email, [self.email], **kwargs)
|
|
|
|
|
2014-09-29 13:34:38 +00:00
|
|
|
def set_password(self, raw_password):
|
|
|
|
self.password = make_password(raw_password)
|
2014-05-08 16:59:35 +00:00
|
|
|
|
2014-09-29 13:34:38 +00:00
|
|
|
def check_password(self, raw_password):
|
2014-05-08 16:59:35 +00:00
|
|
|
"""
|
2014-09-29 13:34:38 +00:00
|
|
|
Returns a boolean of whether the raw_password was correct. Handles
|
|
|
|
hashing formats behind the scenes.
|
2014-05-08 16:59:35 +00:00
|
|
|
"""
|
2014-09-29 13:34:38 +00:00
|
|
|
def setter(raw_password):
|
|
|
|
self.set_password(raw_password)
|
|
|
|
self.save(update_fields=["password"])
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
|
2014-09-29 13:34:38 +00:00
|
|
|
services.register(User)
|