cmsmanage/rentals/admin.py

69 lines
2.0 KiB
Python
Raw Permalink Normal View History

from django import forms
from django.contrib import admin
from django.db.models import Prefetch, QuerySet
from django.http import HttpRequest
from membershipworks.models import Member
from .models import LockerBank, LockerInfo, LockerUnit
class LockerInfoInline(admin.TabularInline):
model = LockerInfo
extra = 0
2022-01-24 23:37:04 -05:00
class LockerUnitInline(admin.TabularInline):
model = LockerUnit
extra = 0
2022-01-24 23:37:04 -05:00
@admin.register(LockerBank)
class LockerBankAdmin(admin.ModelAdmin):
inlines = [LockerUnitInline]
prepopulated_fields = {"slug": ("name",)}
def get_queryset(self, request: HttpRequest) -> QuerySet[LockerBank]:
return super().get_queryset(request).prefetch_related("units")
2022-01-24 23:37:04 -05:00
@admin.register(LockerUnit)
class LockerUnitAdmin(admin.ModelAdmin):
inlines = [LockerInfoInline]
def get_queryset(self, request: HttpRequest) -> QuerySet[LockerUnit]:
return super().get_queryset(request).prefetch_related("bank")
@admin.register(LockerInfo)
class LockerInfoAdmin(admin.ModelAdmin):
search_fields = ["renter__username", "locker_unit__bank__name"]
list_filter = ["locker_unit__bank", "locker_unit"]
show_facets = admin.ShowFacets.ALWAYS
list_display = [
"locker_unit",
"address",
"blind_code",
"bitting_code",
"renter",
"notes",
]
list_display_links = ["locker_unit", "address"]
def get_queryset(self, request: HttpRequest) -> QuerySet[LockerInfo]:
return (
super()
.get_queryset(request)
.select_related("locker_unit", "locker_unit__bank")
.prefetch_related(
Prefetch("renter", queryset=Member.objects.only("account_name"))
)
)
def get_changelist_formset(self, request, **kwargs):
kwargs["widgets"] = {
"blind_code": forms.TextInput(attrs={"size": 6}),
"bitting_code": forms.TextInput(attrs={"size": 6}),
}
return super().get_changelist_formset(request, **kwargs)