2022-02-18 15:33:03 -05:00
|
|
|
from django import forms
|
2024-01-17 21:17:24 -05:00
|
|
|
from django.contrib import admin
|
2024-08-07 13:01:28 -04:00
|
|
|
from django.db.models import Prefetch, QuerySet
|
|
|
|
from django.http import HttpRequest
|
2022-01-24 22:03:10 -05:00
|
|
|
|
2023-03-28 12:49:42 -04:00
|
|
|
from membershipworks.models import Member
|
2024-01-17 21:17:24 -05:00
|
|
|
|
2022-02-16 16:06:51 -05:00
|
|
|
from .models import LockerBank, LockerInfo, LockerUnit
|
|
|
|
|
|
|
|
|
|
|
|
class LockerInfoInline(admin.TabularInline):
|
|
|
|
model = LockerInfo
|
|
|
|
extra = 0
|
2022-01-24 22:03:10 -05:00
|
|
|
|
2022-01-24 23:37:04 -05:00
|
|
|
|
2022-01-27 14:55:27 -05:00
|
|
|
class LockerUnitInline(admin.TabularInline):
|
|
|
|
model = LockerUnit
|
2022-01-24 22:03:10 -05:00
|
|
|
extra = 0
|
|
|
|
|
2022-01-24 23:37:04 -05:00
|
|
|
|
2022-01-24 22:03:10 -05:00
|
|
|
@admin.register(LockerBank)
|
|
|
|
class LockerBankAdmin(admin.ModelAdmin):
|
2022-01-27 14:55:27 -05:00
|
|
|
inlines = [LockerUnitInline]
|
2022-01-24 22:03:10 -05:00
|
|
|
prepopulated_fields = {"slug": ("name",)}
|
|
|
|
|
2024-08-07 13:01:28 -04:00
|
|
|
def get_queryset(self, request: HttpRequest) -> QuerySet[LockerBank]:
|
|
|
|
return super().get_queryset(request).prefetch_related("units")
|
|
|
|
|
2022-01-24 23:37:04 -05:00
|
|
|
|
2022-01-27 14:55:27 -05:00
|
|
|
@admin.register(LockerUnit)
|
|
|
|
class LockerUnitAdmin(admin.ModelAdmin):
|
2022-02-16 16:06:51 -05:00
|
|
|
inlines = [LockerInfoInline]
|
2022-01-27 14:55:27 -05:00
|
|
|
|
2024-08-07 13:01:28 -04:00
|
|
|
def get_queryset(self, request: HttpRequest) -> QuerySet[LockerUnit]:
|
|
|
|
return super().get_queryset(request).prefetch_related("bank")
|
|
|
|
|
2022-01-27 14:55:27 -05:00
|
|
|
|
2022-02-16 16:06:51 -05:00
|
|
|
@admin.register(LockerInfo)
|
|
|
|
class LockerInfoAdmin(admin.ModelAdmin):
|
|
|
|
search_fields = ["renter__username", "locker_unit__bank__name"]
|
2023-03-28 12:49:42 -04:00
|
|
|
list_filter = ["locker_unit__bank", "locker_unit"]
|
2023-12-04 13:08:36 -05:00
|
|
|
show_facets = admin.ShowFacets.ALWAYS
|
2022-02-18 15:35:03 -05:00
|
|
|
list_display = [
|
|
|
|
"locker_unit",
|
|
|
|
"address",
|
|
|
|
"blind_code",
|
|
|
|
"bitting_code",
|
|
|
|
"renter",
|
|
|
|
"notes",
|
|
|
|
]
|
2022-02-16 16:06:51 -05:00
|
|
|
list_display_links = ["locker_unit", "address"]
|
2022-02-18 13:18:36 -05:00
|
|
|
|
2024-08-07 13:01:28 -04:00
|
|
|
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"))
|
|
|
|
)
|
2023-03-28 12:49:42 -04:00
|
|
|
)
|
2022-02-18 15:33:03 -05:00
|
|
|
|
|
|
|
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)
|