checkout/src/purchase/admin.py

73 lines
2.2 KiB
Python
Raw Normal View History

2022-04-24 16:21:39 +02:00
from django.contrib import admin
from django.contrib.admin import register
2022-04-25 23:04:49 +02:00
from django.utils.translation import gettext_lazy as _
2022-04-24 16:21:39 +02:00
from purchase.models import Basket, BasketItem, PaymentMethod, Product
from purchase.templatetags.purchase import currency
2022-04-24 16:21:39 +02:00
@register(Product)
class ProductAdmin(admin.ModelAdmin):
2022-04-24 19:28:52 +02:00
list_display = ["name", "display_order", "unit_price", "sold", "turnover"]
list_editable = ["display_order"]
2022-04-24 16:21:39 +02:00
search_fields = ["name"]
def get_queryset(self, request):
return super().get_queryset(request).with_sold().with_turnover()
2022-04-25 23:04:49 +02:00
@admin.display(description=_("unit price"))
2022-04-24 16:21:39 +02:00
def unit_price(self, instance: Product):
return currency(instance.unit_price_cents)
2022-04-24 19:28:52 +02:00
2022-04-25 23:04:49 +02:00
@admin.display(description=_("sold"))
2022-04-24 19:28:52 +02:00
def sold(self, instance: Product):
return instance.sold
2022-04-25 23:04:49 +02:00
@admin.display(description=_("turnover"))
2022-04-24 19:28:52 +02:00
def turnover(self, instance: Product):
return currency(instance.turnover)
2022-04-24 16:21:39 +02:00
@register(PaymentMethod)
class PaymentMethodAdmin(admin.ModelAdmin):
2022-04-24 19:28:52 +02:00
list_display = ["name", "turnover"]
2022-04-24 16:21:39 +02:00
search_fields = ["name"]
def get_queryset(self, request):
return super().get_queryset(request).with_turnover()
2022-04-25 23:04:49 +02:00
@admin.display(description=_("turnover"))
2022-04-24 19:28:52 +02:00
def turnover(self, instance: Product):
return currency(instance.turnover)
2022-04-24 19:28:52 +02:00
2022-04-24 16:21:39 +02:00
class BasketItemInline(admin.TabularInline):
model = BasketItem
fields = ["product", "quantity", "price"]
extra = 0
readonly_fields = ["price"]
def get_queryset(self, request):
return super().get_queryset(request).priced()
2022-04-25 23:04:49 +02:00
@admin.display(description=_("price"))
2022-04-24 18:59:04 +02:00
def price(self, instance) -> str:
return currency(instance.price)
2022-04-24 16:21:39 +02:00
@register(Basket)
class BasketAdmin(admin.ModelAdmin):
2022-04-24 18:59:04 +02:00
list_display = ["id", "payment_method", "created_at", "price"]
fields = ["created_at", "payment_method", "price"]
2022-04-24 18:59:04 +02:00
list_filter = ["payment_method"]
2022-04-24 16:21:39 +02:00
date_hierarchy = "created_at"
readonly_fields = ["created_at", "price"]
2022-04-24 16:21:39 +02:00
inlines = [BasketItemInline]
def get_queryset(self, request):
return super().get_queryset(request).priced()
2022-04-25 23:04:49 +02:00
@admin.display(description=_("price"))
2022-04-24 18:59:04 +02:00
def price(self, instance) -> str:
return currency(instance.price)