poulpe/equipment/models.py

74 lines
2.3 KiB
Python
Raw Normal View History

2016-10-14 13:02:25 +02:00
from django.db import models
2017-07-21 16:18:19 +02:00
from django.core.exceptions import ValidationError
2016-10-14 13:02:25 +02:00
from django.utils.translation import ugettext_lazy as _
2017-07-21 16:18:19 +02:00
from event.models import Activity, EventSpecificMixin
2016-10-14 13:02:25 +02:00
2018-08-02 17:55:27 +02:00
from .fields import IdField
2018-07-26 15:53:42 +02:00
from taggit.managers import TaggableManager
2016-10-14 13:02:25 +02:00
class Equipment(EventSpecificMixin, models.Model):
2016-10-14 13:02:25 +02:00
name = models.CharField(
2017-07-15 17:44:15 +02:00
_("nom du matériel"),
2016-10-14 13:02:25 +02:00
max_length=200,
)
2017-07-15 17:44:15 +02:00
stock = models.PositiveSmallIntegerField(_("quantité disponible"))
description = models.TextField(_("description"))
2016-10-14 13:02:25 +02:00
activities = models.ManyToManyField(
Activity,
related_name="equipment",
through="EquipmentAttribution",
)
2018-07-26 15:53:42 +02:00
tags = TaggableManager()
2016-10-14 13:02:25 +02:00
class Meta:
2017-07-21 16:18:19 +02:00
verbose_name = _("matériel")
verbose_name_plural = _("matériels")
2016-10-14 13:02:25 +02:00
def __str__(self):
return self.name
2016-10-14 13:02:25 +02:00
class EquipmentAttribution(models.Model):
equipment = models.ForeignKey(Equipment)
2016-10-14 13:02:25 +02:00
activity = models.ForeignKey(Activity)
2018-08-02 17:55:27 +02:00
amount = models.BigIntegerField(_("quantité attribuée"))
2017-07-15 17:44:15 +02:00
remarks = models.TextField(_("remarques concernant l'attribution"))
2016-10-14 13:02:25 +02:00
class Meta:
verbose_name = _("attribution de matériel")
verbose_name_plural = _("attributions de matériel")
def __str__(self):
2016-10-21 18:49:46 +02:00
return "%s (%d) -> %s" % (self.equipment.name,
self.amout,
self.activity.get_herited('title'))
2016-10-14 13:02:25 +02:00
2017-07-21 16:18:19 +02:00
def save(self, *args, **kwargs):
if self.equipment.event and self.equipment.event != self.activity.event:
raise ValidationError
super(EquipmentAttribution, self).save(*args, **kwargs)
2016-10-14 13:02:25 +02:00
class EquipmentRemark(models.Model):
2017-07-15 17:44:15 +02:00
remark = models.TextField(_("remarque sur le matériel"))
2016-10-14 13:02:25 +02:00
equipment = models.ForeignKey(
Equipment,
2016-10-14 13:02:25 +02:00
related_name="remarks",
help_text=_("Matériel concerné par la remarque"),
)
2018-07-26 15:53:42 +02:00
ids = IdField()
2016-10-14 13:02:25 +02:00
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):
2016-10-21 18:49:46 +02:00
return "%s : %s" % (self.equipment.name,
self.remark)