from django.db import models from django.core.exceptions import ValidationError from django.utils.translation import ugettext_lazy as _ from django.contrib.auth.models import Group from event.models import Activity, EventSpecificMixin from .fields import IdField from taggit.managers import TaggableManager from datetime import date class EquipmentCategory(models.Model): name = models.CharField( _("nom"), max_length=200, ) parent = models.ForeignKey( 'self', blank=True, null=True, default=None, help_text=_("merci de ne pas faire de référence cyclique"), ) class Meta: verbose_name = _("catégories") verbose_name_plural = _("catégories") def __str__(self): return self.name 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", ) owner = models.ForeignKey( Group, blank=True, null=True, ) category = models.ForeignKey( EquipmentCategory, on_delete=models.PROTECT, ) class Meta: verbose_name = _("matériel") verbose_name_plural = _("matériels") def __str__(self): return self.name class EquipmentAttribute(models.Model): name = models.CharField( _("nom"), max_length=200, unique=True, ) class Meta: verbose_name = _("attribut") verbose_name_plural = _("attributs") def __str__(self): return self.name class EquipmentAttributeValue(models.Model): equipment = models.ForeignKey( Equipment, on_delete=models.CASCADE, related_name="attributes", help_text=_("Matériel concerné par le defaut"), ) attribute = models.ForeignKey( EquipmentAttribute, on_delete=models.CASCADE, ) value = models.CharField( _("valeur"), max_length=200, ) class Meta: verbose_name = _("attribut de matériel") verbose_name_plural = _("attributs de matériel") def __str__(self): return "{attr}={value}".format(attr=self.attribute.name, value=self.value) class EquipmentAttribution(models.Model): equipment = models.ForeignKey(Equipment) activity = models.ForeignKey(Activity) amount = models.BigIntegerField(_("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 EquipmentDefault(models.Model): remark = models.TextField(_("remarque sur le défaut")) equipment = models.ForeignKey( Equipment, on_delete=models.CASCADE, related_name="remarks", help_text=_("Matériel concerné par le defaut"), ) ids = IdField() is_unusable = models.BooleanField(_("inutilisable")) send_repare = models.BooleanField(_("à envoyer réparareur")) class Meta: verbose_name = _("defaut matériel") verbose_name_plural = _("défauts sur le matériel") def __str__(self): return "%s : %s" % (self.equipment.name, self.remark) class EquipmentLost(models.Model): lost_at = models.DateField( _("perdu le"), default=date.today, ) equipment = models.ForeignKey( Equipment, on_delete=models.CASCADE, related_name="losts", help_text=_("Matériel concerné par la perte"), ) ids = IdField() class EquipmentRevision(models.Model): date = models.DateField( _("date"), default=date.today, ) equipment = models.ForeignKey( Equipment, on_delete=models.CASCADE, related_name="revisions", help_text=_("Matériel concerné par les révisions"), ) remark = models.TextField(_("remarque sur la révision")) ids = IdField() class Meta: verbose_name = _("révision de matériel") verbose_name_plural = _("révisions de matériel") def __str__(self): return "%s : %s" % (self.equipment.name, self.remark)