12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- # -*- 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.DateField(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
- is_available.boolean = True
- is_available.short_description = 'disponible'
- 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.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)
- class Meta:
- verbose_name = 'prêt d’objet'
- verbose_name_plural = 'prêts d’objets'
|