From e712f9fdb863473e044294efce333ad1439e7b6f Mon Sep 17 00:00:00 2001 From: imperosol Date: Wed, 16 Oct 2024 23:10:12 +0200 Subject: [PATCH] improve counter dump admin --- counter/admin.py | 12 +++++++++++- counter/models.py | 8 ++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/counter/admin.py b/counter/admin.py index 09f0e273..b3e6a91a 100644 --- a/counter/admin.py +++ b/counter/admin.py @@ -63,15 +63,25 @@ class BillingInfoAdmin(admin.ModelAdmin): @admin.register(AccountDump) class AccountDumpAdmin(admin.ModelAdmin): + date_hierarchy = "warning_mail_sent_at" list_display = ( "customer", "warning_mail_sent_at", "warning_mail_error", "dump_operation", + "amount", ) - autocomplete_fields = ("customer",) + autocomplete_fields = ("customer", "dump_operation") list_filter = ("warning_mail_error",) + def get_queryset(self, request): + # the `amount` property requires to know the customer and the dump_operation + return ( + super() + .get_queryset(request) + .select_related("customer", "customer__user", "dump_operation") + ) + @admin.register(Counter) class CounterAdmin(admin.ModelAdmin): diff --git a/counter/models.py b/counter/models.py index 1666572b..9c83b6c1 100644 --- a/counter/models.py +++ b/counter/models.py @@ -279,6 +279,14 @@ class AccountDump(models.Model): status = "ongoing" if self.dump_operation is None else "finished" return f"{self.customer} - {status}" + @cached_property + def amount(self): + return ( + self.dump_operation.unit_price + if self.dump_operation + else self.customer.amount + ) + class ProductType(models.Model): """A product type.