django-orchestra/orchestra/apps/miscellaneous/models.py

48 lines
1.8 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
class MiscService(models.Model):
name = models.CharField(_("name"), max_length=256)
2014-09-30 10:20:11 +00:00
description = models.TextField(_("description"), blank=True)
has_amount = models.BooleanField(_("has amount"), default=False,
2014-07-18 15:32:27 +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,
2014-07-16 15:20:16 +00:00
help_text=_("Whether new instances of this service can be created "
"or not. Unselect this instead of deleting services."))
def __unicode__(self):
return self.name
class Miscellaneous(models.Model):
service = models.ForeignKey(MiscService, verbose_name=_("service"),
related_name='instances')
account = models.ForeignKey('accounts.Account', verbose_name=_("account"),
related_name='miscellaneous')
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,
2014-07-16 15:20:16 +00:00
help_text=_("Designates whether this service should be treated as "
"active. Unselect this instead of deleting services."))
class Meta:
verbose_name_plural = _("miscellaneous")
def __unicode__(self):
return "{0}-{1}".format(str(self.service), str(self.account))
@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-07-16 15:20:16 +00:00
services.register(Miscellaneous)