django-orchestra/orchestra/contrib/lists/models.py

86 lines
2.8 KiB
Python
Raw Normal View History

from django.core.exceptions import ValidationError
2014-05-08 16:59:35 +00:00
from django.db import models
2015-03-18 21:51:12 +00:00
from django.utils.functional import cached_property
from django.utils.translation import gettext_lazy as _
2014-05-08 16:59:35 +00:00
from orchestra.core.validators import validate_name
from . import settings
2015-04-23 14:34:04 +00:00
class ListQuerySet(models.QuerySet):
def create(self, **kwargs):
""" Sets password if provided, all within a single DB operation """
password = kwargs.pop('password')
instance = self.model(**kwargs)
if password:
instance.set_password(password)
instance.save()
return instance
2014-10-09 17:04:12 +00:00
# TODO address and domain, perhaps allow only domain?
2014-05-08 16:59:35 +00:00
class List(models.Model):
name = models.CharField(_("name"), max_length=64, unique=True, validators=[validate_name],
2015-04-05 10:46:24 +00:00
help_text=_("Default list address <name>@%s") % settings.LISTS_DEFAULT_DOMAIN)
address_name = models.CharField(_("address name"), max_length=64,
2015-04-05 10:46:24 +00:00
validators=[validate_name], blank=True)
address_domain = models.ForeignKey(settings.LISTS_DOMAIN_MODEL, on_delete=models.SET_NULL,
2015-04-05 10:46:24 +00:00
verbose_name=_("address domain"), blank=True, null=True)
2014-05-08 16:59:35 +00:00
admin_email = models.EmailField(_("admin email"),
2015-04-05 10:46:24 +00:00
help_text=_("Administration email address"))
2014-05-08 16:59:35 +00:00
account = models.ForeignKey('accounts.Account', verbose_name=_("Account"),
related_name='lists', on_delete=models.CASCADE)
2015-03-18 21:51:12 +00:00
# TODO also admin
2015-04-08 14:41:09 +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-10-15 12:47:28 +00:00
password = None
2015-04-23 14:34:04 +00:00
objects = ListQuerySet.as_manager()
2014-05-08 16:59:35 +00:00
class Meta:
unique_together = ('address_name', 'address_domain')
2015-04-02 16:14:55 +00:00
def __str__(self):
2014-10-09 17:04:12 +00:00
return self.name
2014-10-09 17:04:12 +00:00
@property
def address(self):
if self.address_name and self.address_domain:
return "%s@%s" % (self.address_name, self.address_domain)
return ''
2015-03-18 21:51:12 +00:00
@cached_property
def active(self):
return self.is_active and self.account.is_active
def clean(self):
if self.address_name and not self.address_domain_id:
raise ValidationError({
'address_domain': _("Domain should be selected for provided address name."),
})
2015-05-09 17:08:45 +00:00
def disable(self):
self.is_active = False
self.save(update_fields=('is_active',))
2016-03-31 16:18:38 +00:00
def enable(self):
self.is_active = False
self.save(update_fields=('is_active',))
2014-10-20 15:51:24 +00:00
def get_address_name(self):
return self.address_name or self.name
2014-05-08 16:59:35 +00:00
def get_username(self):
return self.name
2014-05-08 16:59:35 +00:00
def set_password(self, password):
self.password = password
2014-10-23 15:38:46 +00:00
def get_absolute_url(self):
context = {
'name': self.name
}
return settings.LISTS_LIST_URL % context