django-orchestra-test/orchestra/contrib/miscellaneous/models.py

80 lines
3.2 KiB
Python
Raw Normal View History

2014-07-16 15:20:16 +00:00
from django.db import models
from django.utils.functional import cached_property
2014-07-16 15:20:16 +00:00
from django.utils.translation import ugettext_lazy as _
from orchestra.core import services
2015-03-29 16:10:07 +00:00
from orchestra.core.translations import ModelTranslation
2014-10-23 15:38:46 +00:00
from orchestra.core.validators import validate_name
2014-11-12 12:46:41 +00:00
from orchestra.models.fields import NullableCharField
2014-07-16 15:20:16 +00:00
class MiscService(models.Model):
2014-10-23 15:38:46 +00:00
name = models.CharField(_("name"), max_length=32, unique=True, validators=[validate_name],
2015-04-05 10:46:24 +00:00
help_text=_("Raw name used for internal referenciation, i.e. service match definition"))
2014-10-23 15:38:46 +00:00
verbose_name = models.CharField(_("verbose name"), max_length=256, blank=True,
2015-04-05 10:46:24 +00:00
help_text=_("Human readable name"))
2014-10-23 15:38:46 +00:00
description = models.TextField(_("description"), blank=True,
2015-04-05 10:46:24 +00:00
help_text=_("Optional description"))
2014-11-13 15:34:00 +00:00
has_identifier = models.BooleanField(_("has identifier"), default=True,
2015-04-05 10:46:24 +00:00
help_text=_("Designates if this service has a <b>unique text</b> field that "
"identifies it or not."))
2014-09-30 10:20:11 +00:00
has_amount = models.BooleanField(_("has amount"), default=False,
2015-04-05 10:46:24 +00:00
help_text=_("Designates whether this service has <tt>amount</tt> "
"property or not."))
2014-09-30 10:20:11 +00:00
is_active = models.BooleanField(_("active"), default=True,
2015-04-05 10:46:24 +00:00
help_text=_("Whether new instances of this service can be created "
"or not. Unselect this instead of deleting services."))
2014-07-16 15:20:16 +00:00
2015-04-02 16:14:55 +00:00
def __str__(self):
2014-07-16 15:20:16 +00:00
return self.name
2014-10-23 15:38:46 +00:00
def clean(self):
self.verbose_name = self.verbose_name.strip()
def get_verbose_name(self):
return self.verbose_name or self.name
2014-07-16 15:20:16 +00:00
class Miscellaneous(models.Model):
service = models.ForeignKey(MiscService, verbose_name=_("service"),
2015-04-05 10:46:24 +00:00
related_name='instances')
2014-07-16 15:20:16 +00:00
account = models.ForeignKey('accounts.Account', verbose_name=_("account"),
2015-04-05 10:46:24 +00:00
related_name='miscellaneous')
2014-11-13 15:34:00 +00:00
identifier = NullableCharField(_("identifier"), max_length=256, null=True, unique=True,
2015-04-05 10:46:24 +00:00
help_text=_("A unique identifier for this service."))
2014-07-16 15:20:16 +00:00
description = models.TextField(_("description"), blank=True)
amount = models.PositiveIntegerField(_("amount"), default=1)
2014-09-30 10:20:11 +00:00
is_active = models.BooleanField(_("active"), default=True,
2015-04-05 10:46:24 +00:00
help_text=_("Designates whether this service should be treated as "
"active. Unselect this instead of deleting services."))
2014-07-16 15:20:16 +00:00
class Meta:
verbose_name_plural = _("miscellaneous")
2015-04-02 16:14:55 +00:00
def __str__(self):
2014-11-14 15:51:18 +00:00
return self.identifier or self.description[:32] or str(self.service)
@cached_property
def active(self):
try:
return self.is_active and self.account.is_active
except type(self).account.field.rel.to.DoesNotExist:
return self.is_active
2014-11-12 12:46:41 +00:00
2014-11-27 19:17:26 +00:00
def get_description(self):
return ' '.join((str(self.amount), self.service.description or self.service.verbose_name))
2015-03-27 19:50:54 +00:00
@cached_property
def service_class(self):
return self.service
2014-11-12 12:46:41 +00:00
def clean(self):
2014-11-13 15:34:00 +00:00
if self.identifier:
self.identifier = self.identifier.strip()
2014-11-12 12:46:41 +00:00
self.description = self.description.strip()
2014-07-16 15:20:16 +00:00
services.register(Miscellaneous)
2015-03-29 16:10:07 +00:00
ModelTranslation.register(MiscService, ('verbose_name',))