From 006832b71d15279c971af3c4ca6a2d4717473066 Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Sun, 24 Apr 2022 19:28:52 +0200 Subject: [PATCH] Add stats to admin --- src/purchase/admin.py | 15 ++++++++++++--- src/purchase/models.py | 24 ++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/purchase/admin.py b/src/purchase/admin.py index 9edf698..a7e1735 100644 --- a/src/purchase/admin.py +++ b/src/purchase/admin.py @@ -6,19 +6,28 @@ from purchase.models import Basket, BasketItem, PaymentMethod, Product @register(Product) class ProductAdmin(admin.ModelAdmin): - list_display = ["name", "display_order", "unit_price"] + list_display = ["name", "display_order", "unit_price", "sold", "turnover"] list_editable = ["display_order"] search_fields = ["name"] def unit_price(self, instance: Product): - return instance.unit_price_cents / 100 + return instance.unit_price_display + + def sold(self, instance: Product): + return instance.sold + + def turnover(self, instance: Product): + return instance.turnover_display @register(PaymentMethod) class PaymentMethodAdmin(admin.ModelAdmin): - list_display = ["name"] + list_display = ["name", "turnover"] search_fields = ["name"] + def turnover(self, instance: Product): + return instance.turnover_display + class BasketItemInline(admin.TabularInline): model = BasketItem diff --git a/src/purchase/models.py b/src/purchase/models.py index ba2ac4d..a9f3f69 100644 --- a/src/purchase/models.py +++ b/src/purchase/models.py @@ -17,6 +17,14 @@ class PaymentMethod(Model): def __str__(self): return self.name + @property + def turnover(self) -> int: + return sum(basket.price for basket in self.baskets.all()) + + @property + def turnover_display(self) -> str: + return f"{self.turnover / 100}€" + def default_product_display_order(): return Product.objects.last().display_order + 1 @@ -34,6 +42,22 @@ class Product(Model): def __str__(self): return self.name + @property + def unit_price_display(self) -> str: + return f"{self.unit_price_cents / 100}€" + + @property + def turnover(self) -> int: + return sum(items.price for items in self.basket_items.all()) + + @property + def turnover_display(self) -> str: + return f"{self.turnover / 100}€" + + @property + def sold(self): + return sum(items.quantity for items in self.basket_items.all()) + def save(self, *args, **kwargs): super().save() with Image.open(self.image.path) as img: