gestiojeux/loans/models.py
Sylvain Gay 00148cd5ca Add loan tables
Add two views for loans :
- inventory:ongoing_loans
- inventory:all_loans (permission inventory.can_see_loan_details required)
2024-05-06 16:24:16 +02:00

34 lines
1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from django.db import models
from autoslug import AutoSlugField
from django.utils.timezone import now
class AbstractLoan(models.Model):
lent_object = None # Fill this with a foreign key in subclasses
slug = AutoSlugField(unique=True, populate_from="lent_object")
borrow_date = models.DateTimeField(
auto_now_add=True, verbose_name="Date demprunt")
return_date = models.DateTimeField(null=True, verbose_name="Date de retour")
mail = models.EmailField()
lent_object_slug_field = "slug"
class Meta:
abstract = True
ordering=["borrow_date"]
verbose_name = "emprunt"
verbose_name_plural = "emprunts"
def __str__(self):
return self.slug
def return_object(self):
self.return_date = now()
self.save()
@classmethod
def ongoing_loans(cls, obj = None):
ongoing = cls.objects.filter(return_date=None)
if obj != None:
return ongoing.filter(lent_object=obj)
else:
return ongoing