# -*- coding: utf-8 -*- from __future__ import unicode_literals from django.db import models from django.conf import settings from django.utils import timezone 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 ItemQuerySet(models.QuerySet): def _get_borrowed_pks(self): return Loan.objects.running().values_list('item', flat=True) def available(self): return self.exclude(pk__in=self._get_borrowed_pks()) def borrowed(self): return self.filter(pk__in=self._get_borrowed_pks()) 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') storage = models.ForeignKey( 'Storage', related_name='items', verbose_name='Lieu de stockage', null=True, blank=True, help_text='Laisser vide si inconnu') mac_address = MACAddressField( verbose_name='addresse MAC', blank=True, null=True, help_text="préférable au n° de série si possible") serial = models.CharField( verbose_name='N° de série', max_length=250, blank=True, help_text='ou toute autre référence unique)') buy_date = models.DateField(verbose_name='date d’achat') owner = models.ForeignKey( settings.AUTH_USER_MODEL, verbose_name='Propriétaire', related_name='items', null=True, blank=True, help_text="dans le cas de matériel n'appartenant pas à l'association") comment = models.TextField(verbose_name='commentaire', blank=True, null=True) objects = ItemQuerySet().as_manager() 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.running().exists(): return False return True is_available.boolean = True is_available.short_description = 'disponible' class Meta: verbose_name = 'objet' def give_back(self, storage=None): self.storage = storage self.save() self.loans.running().update( loan_date_end=timezone.now()) class LoanQuerySet(models.QuerySet): @staticmethod def _running_filter(): return ( models.Q(loan_date_end__gt=timezone.now()) | models.Q(loan_date_end__isnull=True)) def running(self): return self.filter(self._running_filter()) def finished(self): return self.exclude(self._running_filter()) 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.DateField(verbose_name='date de prêt') loan_date_end = models.DateField(verbose_name='date de fin de prêt', null=True, blank=True) notes = models.TextField(null=True, blank=True) def __unicode__(self): return 'prêt de {item} à {user}'.format( item=self.item, user=self.user) def user_can_close(self, user): return (not self.item.is_available()) and (self.user == user) class Meta: verbose_name = 'prêt d’objet' verbose_name_plural = 'prêts d’objets' objects = LoanQuerySet().as_manager() class Storage(models.Model): name = models.CharField(max_length=100, verbose_name='nom') notes = models.TextField( blank=True, help_text='Lisible par tous les adhérents') def __unicode__(self): return self.name def items_count(self): return self.items.count() items_count.short_description = 'Nb. items stockés' class Meta: verbose_name = 'lieu de stockage' verbose_name_plural = 'lieux de stockage'