Add stats to admin

This commit is contained in:
Gabriel Augendre 2022-04-24 19:28:52 +02:00
parent 78681e146d
commit 006832b71d
2 changed files with 36 additions and 3 deletions

View file

@ -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

View file

@ -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: