poulpe/equipment/models.py
2018-07-26 15:53:42 +02:00

74 lines
2.3 KiB
Python

from django.db import models
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext_lazy as _
from event.models import Activity, EventSpecificMixin
from taggit.managers import TaggableManager
class Equipment(EventSpecificMixin, models.Model):
name = models.CharField(
_("nom du matériel"),
max_length=200,
)
stock = models.PositiveSmallIntegerField(_("quantité disponible"))
description = models.TextField(_("description"))
activities = models.ManyToManyField(
Activity,
related_name="equipment",
through="EquipmentAttribution",
)
tags = TaggableManager()
class Meta:
verbose_name = _("matériel")
verbose_name_plural = _("matériels")
def __str__(self):
return self.name
class IdField(models.PositiveIntegerField):
pass
class EquipmentAttribution(models.Model):
equipment = models.ForeignKey(Equipment)
activity = models.ForeignKey(Activity)
amount = models.PositiveSmallIntegerField(_("quantité attribuée"))
remarks = models.TextField(_("remarques concernant l'attribution"))
class Meta:
verbose_name = _("attribution de matériel")
verbose_name_plural = _("attributions de matériel")
def __str__(self):
return "%s (%d) -> %s" % (self.equipment.name,
self.amout,
self.activity.get_herited('title'))
def save(self, *args, **kwargs):
if self.equipment.event and self.equipment.event != self.activity.event:
raise ValidationError
super(EquipmentAttribution, self).save(*args, **kwargs)
class EquipmentRemark(models.Model):
remark = models.TextField(_("remarque sur le matériel"))
equipment = models.ForeignKey(
Equipment,
related_name="remarks",
help_text=_("Matériel concerné par la remarque"),
)
ids = IdField()
is_broken = models.BooleanField()
is_lost = models.BooleanField()
class Meta:
verbose_name = _("remarque sur matériel")
verbose_name_plural = _("remarques sur le matériel")
def __str__(self):
return "%s : %s" % (self.equipment.name,
self.remark)