# -*- coding: utf-8 -*- from __future__ import unicode_literals from django.db import models from django.conf import settings from .fields import MACAddressField class ItemType(models.Model): name = models.CharField(max_length=100, verbose_name='nom') def __unicode__(self): return self.name class Meta: verbose_name = 'type d’objet' verbose_name_plural = 'types d’objet' class Item(models.Model): type = models.ForeignKey(ItemType, verbose_name='type de matériel', related_name='items') designation = models.CharField(max_length=100, verbose_name='désignation') mac_address = MACAddressField(verbose_name='addresse MAC', blank=True, null=True) buy_date = models.DateTimeField(verbose_name='date d’achat') user_in_charge = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name='membre responsable', related_name='items') comment = models.TextField(verbose_name='commentaire', blank=True, null=True) def __unicode__(self): return self.designation def get_current_loan(self): """ Returns the current Loan for this Item, if exists, or None. """ try: return self.loans.get(loan_date_end__isnull=True) except Loan.DoesNotExist: return None def is_available(self): """ Returns the status of the Item. If a Loan without an end date exists, returns False (else True). """ if self.loans.filter(loan_date_end__isnull=True).count(): return False return True class Meta: verbose_name = 'objet' class Loan(models.Model): item = models.ForeignKey(Item, verbose_name='objet', related_name='loans') user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name='membre', related_name='loans') loan_date = models.DateTimeField(verbose_name='date de prêt') loan_date_end = models.DateTimeField(verbose_name='date de fin de prêt', null=True, blank=True) location = models.CharField(max_length=100, verbose_name='emplacement', null=True, blank=True) def __unicode__(self): return 'prêt de {item} à {user}'.format(item=self.item, user=self.user) class Meta: verbose_name = 'prêt d’objet' verbose_name_plural = 'prêts d’objets'