checkout/src/purchase/admin.py

53 lines
1.4 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
from purchase.models import Basket, BasketItem, PaymentMethod, Product
@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 unit_price(self, instance: Product):
2022-04-24 19:28:52 +02:00
return instance.unit_price_display
def sold(self, instance: Product):
return instance.sold
def turnover(self, instance: Product):
return instance.turnover_display
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"]
2022-04-24 19:28:52 +02:00
def turnover(self, instance: Product):
return instance.turnover_display
2022-04-24 16:21:39 +02:00
class BasketItemInline(admin.TabularInline):
model = BasketItem
fields = ["product", "quantity", "price"]
extra = 0
readonly_fields = ["price"]
2022-04-24 18:59:04 +02:00
def price(self, instance) -> str:
return instance.price_display
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"]
list_filter = ["payment_method"]
2022-04-24 16:21:39 +02:00
date_hierarchy = "created_at"
readonly_fields = ["created_at"]
inlines = [BasketItemInline]
2022-04-24 18:59:04 +02:00
def price(self, instance) -> str:
return instance.price_display