From c7ad114c5cccc9db08ead9dbce8f152ba14e75fc Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Fri, 28 Jun 2019 15:15:12 +0200 Subject: [PATCH] Add count and cost of consumables to buy --- manuels/admin.py | 18 +++++++++++++++++- manuels/models.py | 11 +++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/manuels/admin.py b/manuels/admin.py index 3fc91ef..f470bb3 100644 --- a/manuels/admin.py +++ b/manuels/admin.py @@ -34,7 +34,15 @@ class TeacherResource(resources.ModelResource): @admin.register(Level) class LevelAdmin(admin.ModelAdmin): - list_display = ['name', 'order', 'book_count', 'consumable_count', 'consumable_price'] + list_display = [ + 'name', + 'order', + 'book_count', + 'consumable_count', + 'consumable_price', + 'non_acquired_consumable_count', + 'non_acquired_consumable_price' + ] list_editable = ['order'] list_display_links = ['name'] @@ -50,6 +58,14 @@ class LevelAdmin(admin.ModelAdmin): return f'{obj.consumable_price:.2f}€' consumable_price.short_description = 'Coût des consommables' + def non_acquired_consumable_count(self, obj: Level): + return obj.non_acquired_consumable_count + non_acquired_consumable_count.short_description = 'Nombre de consommable(s) à acheter cette année' + + def non_acquired_consumable_price(self, obj: Level): + return f'{obj.non_acquired_consumable_price:.2f}€' + non_acquired_consumable_price.short_description = 'Coût des consommables à acheter cette année' + class BookResource(resources.ModelResource): class Meta: diff --git a/manuels/models.py b/manuels/models.py index cc87d8d..6888064 100644 --- a/manuels/models.py +++ b/manuels/models.py @@ -120,6 +120,10 @@ class Level(BaseModel): def consumable_count(self): return self.book_set.filter(consumable=True).count() + @property + def non_acquired_consumable_count(self): + return self.book_set.filter(consumable=True, previously_acquired=False).count() + @property def consumable_price(self): price = self.book_set.filter(consumable=True).aggregate(Sum('price')).get('price__sum', 0) @@ -127,6 +131,13 @@ class Level(BaseModel): return 0 return price + @property + def non_acquired_consumable_price(self): + price = self.book_set.filter(consumable=True, previously_acquired=False).aggregate(Sum('price')).get('price__sum', 0) + if price is None: + return 0 + return price + class Editor(BaseModel): class Meta: