rebelstuff

Keep track of your stuff
git clone https://git.ce9e.org/rebelstuff.git

commit
b583b90962b79f6052c736c77d02378fb1e9dff7
parent
1eb11d11de9bbd89834c2158af9ff62bfdeffed8
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2019-08-22 22:59
define stuff.available()

Diffstat

M rebelstuff/admin.py 2 +-
M rebelstuff/models.py 15 +++++++++++++++

2 files changed, 16 insertions, 1 deletions


diff --git a/rebelstuff/admin.py b/rebelstuff/admin.py

@@ -4,7 +4,7 @@ from . import models
    4     4 
    5     5 
    6     6 class StuffAdmin(admin.ModelAdmin):
    7    -1     list_display = ['name', 'amount']
   -1     7     list_display = ['name', 'amount', 'available']
    8     8     search_fields = ['name']
    9     9 
   10    10 

diff --git a/rebelstuff/models.py b/rebelstuff/models.py

@@ -1,3 +1,5 @@
   -1     1 import datetime
   -1     2 
    1     3 from django.db import models
    2     4 from django.utils.translation import gettext_lazy as _
    3     5 
@@ -10,6 +12,19 @@ class Stuff(models.Model):
   10    12     def __str__(self):
   11    13         return self.name
   12    14 
   -1    15     def available(self):
   -1    16         day = datetime.date.today()
   -1    17 
   -1    18         items = BookingItem.objects.filter(
   -1    19             booking__start__lte=day,
   -1    20             booking__end__gte=day,
   -1    21             stuff=self,
   -1    22         )
   -1    23 
   -1    24         agg = items.aggregate(models.Sum('amount'))
   -1    25         booked = agg['amount__sum'] or 0
   -1    26         return self.amount - booked
   -1    27 
   13    28 
   14    29 class Booking(models.Model):
   15    30     name = models.CharField(_('Name'), max_length=64)