django-orchestra-test/orchestra/apps/users/models.py

56 lines
2.2 KiB
Python
Raw Normal View History

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 _
from orchestra.core import services
2014-09-29 14:45:51 +00:00
from . import settings
2014-05-08 16:59:35 +00:00
class User(models.Model):
2014-09-29 14:45:51 +00:00
""" System users """
2014-05-08 16:59:35 +00:00
username = models.CharField(_("username"), max_length=64, unique=True,
2014-09-29 14:45:51 +00:00
help_text=_("Required. 30 characters or fewer. Letters, digits and ./-/_ only."),
2014-08-22 15:31:44 +00:00
validators=[validators.RegexValidator(r'^[\w.-]+$',
2014-05-08 16:59:35 +00:00
_("Enter a valid username."), 'invalid')])
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-09-29 14:45:51 +00:00
home = models.CharField(_("home"), max_length=256, blank=True,
help_text=_("Home directory relative to account's ~primary_user"))
shell = models.CharField(_("shell"), max_length=32,
choices=settings.USERS_SHELLS, default=settings.USERS_DEFAULT_SHELL)
groups = models.ManyToManyField('users.User', blank=True,
help_text=_("A new group will be created for the user. "
"Which additional groups would you like them to be a member of?"))
2014-05-08 16:59:35 +00:00
is_active = models.BooleanField(_("active"), default=True,
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-09-29 14:45:51 +00:00
def __unicode__(self):
return self.username
2014-07-09 16:17:43 +00:00
@property
def is_main(self):
return self.username == self.account.username
2014-07-09 16:17:43 +00:00
def set_password(self, raw_password):
self.password = make_password(raw_password)
2014-05-08 16:59:35 +00:00
def check_password(self, raw_password):
2014-05-08 16:59:35 +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
"""
def setter(raw_password):
self.set_password(raw_password)
self.save(update_fields=["password"])
2014-09-29 14:45:51 +00:00
def get_is_active(self):
return self.account.is_active and self.is_active
2014-05-08 16:59:35 +00:00
services.register(User)