rewrite admin panel

This commit is contained in:
2022-09-04 13:34:37 +02:00
parent b55ff2f4ea
commit a2b5da7e89

View File

@@ -1,8 +1,202 @@
from decimal import Decimal
from typing import Union
from django.contrib import admin
from django.contrib.auth import get_user_model
from django.contrib.auth.admin import UserAdmin
from django.utils.safestring import mark_safe
from gaehsnitz.models import Payment
from gaehsnitz.models import Donation, Payment, Drink, Consumption, Payback
User = get_user_model()
@admin.register(Payment, site=admin.site)
def euro(value: Union[int, float, Decimal]) -> str:
return f"{value:.2f}"
class DonationInline(admin.TabularInline):
model = Donation
extra = 0
class ConsumptionInline(admin.TabularInline):
model = Consumption
extra = 0
class PaymentInline(admin.TabularInline):
model = Payment
extra = 0
class PaybackInline(admin.TabularInline):
model = Payback
extra = 0
@admin.register(User)
class CustomUserAdmin(UserAdmin):
list_display = ("username", "balance")
ordering = ("username",)
list_filter = []
fieldsets = (
(None, {"fields": (
"username",
"password",
)}),
("BALANCE", {"fields": (
"donations_made",
"consumed_drinks_price",
"other_payments_made",
"paybacks_received",
"balance",
)}),
)
readonly_fields = (
"donations_made",
"consumed_drinks_price",
"other_payments_made",
"paybacks_received",
"balance",
)
inlines = (DonationInline, ConsumptionInline, PaymentInline, PaybackInline)
def donations_made(self, user: User):
return euro(user.donations_made)
def consumed_drinks_price(self, user: User):
return euro(user.consumed_drinks_price)
def other_payments_made(self, user: User):
return euro(user.other_payments_made)
def paybacks_received(self, user: User):
return euro(user.paybacks_received)
def balance(self, user: User):
val = user.balance
if val < 0:
color = "#FF8844"
elif val < 20:
color = "#EEEE33"
elif val < 40:
color = "#66FF33"
else:
color = "#5599FF"
return mark_safe(
f'<span style="color: {color};"><b>'
f'{euro(val)}'
f'</b></span>'
)
@admin.register(Donation)
class DonationAdmin(admin.ModelAdmin):
list_display = ("date", "from_user", "amount", "note")
ordering = ("-date",)
@admin.display(ordering="amount")
def amount(self, donation: Donation):
return euro(donation.amount)
@admin.register(Payment)
class PaymentAdmin(admin.ModelAdmin):
list_display = ("topic", "amount", "date", "other_party",)
list_display = ("date", "purpose", "amount", "from_user")
ordering = ("-date",)
@admin.display(ordering="amount")
def amount(self, payment: Payment):
return euro(payment.amount)
@admin.register(Payback)
class PaybackAdmin(admin.ModelAdmin):
list_display = ("date", "to_user", "amount", "note")
ordering = ("-date",)
@admin.display(ordering="amount")
def amount(self, payback: Payback):
return euro(payback.amount)
@admin.register(Drink)
class DrinkAdmin(admin.ModelAdmin):
list_display = ("name", "purchase_price_per_crate", "crates_purchased", "purchase_price_total")
fieldsets = (
(None, {"fields": (
"name",
)}),
("crates", {"fields": (
"crates_ordered",
"crates_purchased",
"crates_returned",
)}),
("bottles", {"fields": (
"bottles_per_crate",
"bottles_total",
"bottles_returned",
)}),
("amount", {"fields": (
"bottle_size",
"amount_per_crate",
"amount_total",
)}),
("purchase", {"fields": (
"purchase_price_per_crate",
"purchase_price_per_bottle",
"purchase_price_total",
)}),
("deposit", {"fields": (
"deposit_per_crate",
"deposit_total",
"deposit_refund",
"deposit_kept",
)}),
("sales", {"fields": (
"sale_price_per_bottle",
"bottles_sold",
"sales_purchase_value",
"sale_price_total",
"bottles_given_away",
"giveaway_purchase_value",
"balance",
)}),
)
readonly_fields = (
"bottles_total", "bottles_returned",
"amount_per_crate", "amount_total",
"purchase_price_per_bottle", "purchase_price_total",
"deposit_total", "deposit_refund", "deposit_kept",
"bottles_sold", "sales_purchase_value", "sale_price_total",
"bottles_given_away", "giveaway_purchase_value",
"balance",
)
def purchase_price_per_bottle(self, drink: Drink):
return euro(drink.purchase_price_per_bottle)
def purchase_price_total(self, drink: Drink):
return euro(drink.purchase_price_total)
def deposit_total(self, drink: Drink):
return euro(drink.deposit_total)
def deposit_refund(self, drink: Drink):
return euro(drink.deposit_refund)
def deposit_kept(self, drink: Drink):
return euro(drink.deposit_kept)
def sales_purchase_value(self, drink: Drink):
return euro(drink.sales_purchase_value)
def sale_price_total(self, drink: Drink):
return euro(drink.sale_price_total)
def giveaway_purchase_value(self, drink: Drink):
return euro(drink.giveaway_purchase_value)
def balance(self, drink: Drink):
return mark_safe(f"<b>{euro(drink.balance)}</b>")