improve osusers model classes

- add __str__ methods
- add verbose names and help texts
This commit is contained in:
Jan Dittberner 2014-05-24 23:40:54 +02:00
parent 9d447cac0b
commit a85ad4e420

View file

@ -1,46 +1,104 @@
from django.db import models
from django.core.exceptions import ValidationError
from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import ugettext as _
from model_utils.models import TimeStampedModel
@python_2_unicode_compatible
class Group(TimeStampedModel, models.Model):
groupname = models.CharField(max_length=16, unique=True)
gid = models.PositiveSmallIntegerField(unique=True, primary_key=True)
descr = models.TextField(blank=True)
passwd = models.CharField(max_length=128, blank=True)
groupname = models.CharField(
_('Group name'), max_length=16, unique=True)
gid = models.PositiveSmallIntegerField(
_('Group ID'), unique=True, primary_key=True)
descr = models.TextField(_('Description'), blank=True)
passwd = models.CharField(
_('Group password'), max_length=128, blank=True)
class Meta:
verbose_name = _('Group')
verbose_name_plural = _('Groups')
def __str__(self):
return '{0} ({1})'.format(self.groupname, self.gid)
@python_2_unicode_compatible
class User(TimeStampedModel, models.Model):
username = models.CharField(max_length=64, unique=True)
uid = models.PositiveSmallIntegerField(unique=True, primary_key=True)
group = models.ForeignKey(Group)
gecos = models.CharField(max_length=128, blank=True)
homedir = models.CharField(max_length=256)
shell = models.CharField(max_length=64)
username = models.CharField(
_('User name'), max_length=64, unique=True)
uid = models.PositiveSmallIntegerField(
_('User ID'), unique=True, primary_key=True)
group = models.ForeignKey(Group, verbose_name=_('Group'))
gecos = models.CharField(_('Gecos field'), max_length=128, blank=True)
homedir = models.CharField(_('Home directory'), max_length=256)
shell = models.CharField(_('Login shell'), max_length=64)
class Meta:
verbose_name = _('User')
verbose_name_plural = _('Users')
def __str__(self):
return '{0} ({1})'.format(self.username, self.uid)
@python_2_unicode_compatible
class Shadow(TimeStampedModel, models.Model):
user = models.OneToOneField(User, primary_key=True)
passwd = models.CharField(max_length=128)
changedays = models.PositiveSmallIntegerField(blank=True, null=True)
minage = models.PositiveSmallIntegerField(blank=True, null=True)
maxage = models.PositiveSmallIntegerField(blank=True, null=True)
gracedays = models.PositiveSmallIntegerField(blank=True, null=True)
inactdays = models.PositiveSmallIntegerField(blank=True, null=True)
expiredays = models.PositiveSmallIntegerField(blank=True, null=True,
default=None)
user = models.OneToOneField(User, primary_key=True, verbose_name=_('User'))
passwd = models.CharField(_('Encrypted password'), max_length=128)
changedays = models.PositiveSmallIntegerField(
_('Date of last change'),
help_text=_('This is expressed in days since Jan 1, 1970'),
blank=True, null=True)
minage = models.PositiveSmallIntegerField(
_('Minimum age'),
help_text=_('Minimum number of days before the password can be'
' changed'),
blank=True, null=True)
maxage = models.PositiveSmallIntegerField(
_('Maximum age'),
help_text=_('Maximum number of days after which the password has to'
' be changed'),
blank=True, null=True)
gracedays = models.PositiveSmallIntegerField(
_('Grace period'),
help_text=_('The number of days before the password is going to'
' expire'),
blank=True, null=True)
inactdays = models.PositiveSmallIntegerField(
_('Inactivity period'),
help_text=_('The number of days after the password has expired during'
' which the password should still be accepted'),
blank=True, null=True)
expiredays = models.PositiveSmallIntegerField(
_('Account expiration date'),
help_text=_('The date of expiration of the account, expressed as'
' number of days since Jan 1, 1970'),
blank=True, null=True, default=None)
class Meta:
verbose_name = _('Shadow password')
verbose_name_plural = _('Shadow passwords')
def __str__(self):
return 'for user {0}'.format(self.user)
@python_2_unicode_compatible
class AdditionalGroup(TimeStampedModel, models.Model):
user = models.ForeignKey(User)
group = models.ForeignKey(Group)
class Meta:
unique_together = ('user', 'group')
verbose_name = _('Additional group')
verbose_name_plural = _('Additional groups')
def clean(self):
if self.user.group == self.group:
raise ValidationError(_(
"You can not use a user's primary group."))
def __str__(self):
return '{0} in {1}'.format(self.user, self.group)