2014-07-16 15:20:16 +00:00
|
|
|
from django.db import models
|
2014-10-21 09:27:31 +00:00
|
|
|
from django.utils.functional import cached_property
|
2023-10-24 16:59:02 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2014-07-16 15:20:16 +00:00
|
|
|
|
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."))
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2015-04-02 16:14:55 +00:00
|
|
|
def __str__(self):
|
2014-07-16 15:20:16 +00:00
|
|
|
return self.name
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2014-10-23 15:38:46 +00:00
|
|
|
def clean(self):
|
|
|
|
self.verbose_name = self.verbose_name.strip()
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2014-10-23 15:38:46 +00:00
|
|
|
def get_verbose_name(self):
|
|
|
|
return self.verbose_name or self.name
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2016-03-31 16:18:38 +00:00
|
|
|
def disable(self):
|
|
|
|
self.is_active = False
|
|
|
|
self.save(update_fields=('is_active',))
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2016-03-31 16:18:38 +00:00
|
|
|
def enable(self):
|
|
|
|
self.is_active = False
|
|
|
|
self.save(update_fields=('is_active',))
|
2014-07-16 15:20:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Miscellaneous(models.Model):
|
2021-04-22 08:28:00 +00:00
|
|
|
service = models.ForeignKey(MiscService, on_delete=models.CASCADE,
|
|
|
|
verbose_name=_("service"), related_name='instances')
|
|
|
|
account = models.ForeignKey('accounts.Account', on_delete=models.CASCADE,
|
|
|
|
verbose_name=_("account"), related_name='miscellaneous')
|
2014-11-13 15:34:00 +00:00
|
|
|
identifier = NullableCharField(_("identifier"), max_length=256, null=True, unique=True,
|
2015-07-27 12:55:35 +00:00
|
|
|
db_index=True, 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."))
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2014-07-16 15:20:16 +00:00
|
|
|
class Meta:
|
|
|
|
verbose_name_plural = _("miscellaneous")
|
2021-04-22 08:28:00 +00:00
|
|
|
|
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)
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2014-10-21 09:27:31 +00:00
|
|
|
@cached_property
|
|
|
|
def active(self):
|
2015-04-09 14:32:10 +00:00
|
|
|
return self.is_active and self.service.is_active and self.account.is_active
|
2021-04-22 08:28:00 +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))
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2016-03-31 16:18:38 +00:00
|
|
|
def disable(self):
|
|
|
|
self.is_active = False
|
|
|
|
self.save(update_fields=('is_active',))
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2016-03-31 16:18:38 +00:00
|
|
|
def enable(self):
|
|
|
|
self.is_active = False
|
|
|
|
self.save(update_fields=('is_active',))
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2015-03-27 19:50:54 +00:00
|
|
|
@cached_property
|
|
|
|
def service_class(self):
|
|
|
|
return self.service
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2014-11-12 12:46:41 +00:00
|
|
|
def clean(self):
|
2014-11-13 15:34:00 +00:00
|
|
|
if self.identifier:
|
2016-10-28 10:31:11 +00:00
|
|
|
self.identifier = self.identifier.strip().lower()
|
2014-11-12 12:46:41 +00:00
|
|
|
self.description = self.description.strip()
|