123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- # -*- coding: utf-8 -*-
- from __future__ import unicode_literals
- from datetime import date
- 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')
- 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)
- 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
- is_available.boolean = True
- is_available.short_description = 'disponible'
- class Meta:
- verbose_name = 'objet'
- def give_back(self):
- self.loans.filter(loan_date_end=None).update(loan_date_end=date.today())
- class LoanQuerySet(models.QuerySet):
- running_filter = (
- models.Q(loan_date_end__gt=date.today()) |
- 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)
- 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)
- 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
- class Meta:
- verbose_name = 'lieu de stockage'
- verbose_name_plural = 'lieux de stockage'
|