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
|
2022-04-25 18:37:26 +02:00
|
|
|
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"]
|
2022-04-24 19:19:15 +02:00
|
|
|
list_editable = ["display_order"]
|
2022-04-24 16:21:39 +02:00
|
|
|
search_fields = ["name"]
|
|
|
|
|
2022-04-25 18:37:26 +02:00
|
|
|
def get_queryset(self, request):
|
|
|
|
return super().get_queryset(request).with_sold().with_turnover()
|
|
|
|
|
2022-04-24 16:21:39 +02:00
|
|
|
def unit_price(self, instance: Product):
|
2022-04-25 18:37:26 +02:00
|
|
|
return currency(instance.unit_price_cents)
|
2022-04-24 19:28:52 +02:00
|
|
|
|
|
|
|
def sold(self, instance: Product):
|
|
|
|
return instance.sold
|
|
|
|
|
|
|
|
def turnover(self, instance: Product):
|
2022-04-25 18:37:26 +02:00
|
|
|
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"]
|
|
|
|
|
2022-04-25 18:37:26 +02:00
|
|
|
def get_queryset(self, request):
|
|
|
|
return super().get_queryset(request).with_turnover()
|
|
|
|
|
2022-04-24 19:28:52 +02:00
|
|
|
def turnover(self, instance: Product):
|
2022-04-25 18:37:26 +02:00
|
|
|
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"]
|
|
|
|
|
2022-04-25 18:37:26 +02:00
|
|
|
def get_queryset(self, request):
|
|
|
|
return super().get_queryset(request).priced()
|
|
|
|
|
2022-04-24 18:59:04 +02:00
|
|
|
def price(self, instance) -> str:
|
2022-04-25 18:37:26 +02:00
|
|
|
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"]
|
2022-04-25 18:37:26 +02:00
|
|
|
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"
|
2022-04-25 18:37:26 +02:00
|
|
|
readonly_fields = ["created_at", "price"]
|
2022-04-24 16:21:39 +02:00
|
|
|
inlines = [BasketItemInline]
|
|
|
|
|
2022-04-25 18:37:26 +02:00
|
|
|
def get_queryset(self, request):
|
|
|
|
return super().get_queryset(request).priced()
|
|
|
|
|
2022-04-24 18:59:04 +02:00
|
|
|
def price(self, instance) -> str:
|
2022-04-25 18:37:26 +02:00
|
|
|
return currency(instance.price)
|