79 lines
2.8 KiB
Python
79 lines
2.8 KiB
Python
from django.contrib import admin
|
|
from django.urls import reverse
|
|
from django.utils.html import format_html
|
|
from django.utils.http import urlencode
|
|
|
|
from common.admin import MyImportExportModelAdmin
|
|
from transport.admin_mixins import BusRollMixin, ShuttleRollMixin
|
|
from transport.models import Driver, BusStop, Company, Bus, Shuttle
|
|
|
|
|
|
@admin.register(Company)
|
|
class CompanyAdmin(MyImportExportModelAdmin, admin.ModelAdmin):
|
|
list_display = ["name", "contact_name", "email", "buses"]
|
|
|
|
def buses(self, obj):
|
|
count = obj.bus_set.count()
|
|
url = (
|
|
reverse("admin:transport_bus_changelist")
|
|
+ "?"
|
|
+ urlencode({"company__id__exact": f"{obj.id}"})
|
|
)
|
|
return format_html('<a href="{}">{} Buses</a>', url, count)
|
|
|
|
def email(self, obj):
|
|
return format_html('<a href="mailto:{}">{}</a>', obj.contact_email, obj.contact_email)
|
|
|
|
|
|
class DriverInline(admin.StackedInline):
|
|
model = Driver
|
|
extra = 0
|
|
|
|
|
|
class BusStopInline(admin.TabularInline):
|
|
model = BusStop
|
|
extra = 0
|
|
ordering = ("am_time",)
|
|
|
|
|
|
@admin.register(Bus)
|
|
class BusesAdmin(MyImportExportModelAdmin, admin.ModelAdmin, BusRollMixin):
|
|
list_filter = ["company"]
|
|
list_display = ["route_name", "company", "contract_number", "seating_capacity", "route_travellers"]
|
|
readonly_fields = ["traveller_count"]
|
|
actions = ["show_bus_roll", "show_bus_roll_on_date", "show_emergency_contacts", "sms_traveller_contacts", "email_bus_roll", "email_emergency_contacts"]
|
|
inlines = [DriverInline, BusStopInline]
|
|
fieldsets = [
|
|
(None, {'fields': [
|
|
"company", "route_name", "contract_number", "registration",
|
|
"traveller_count", "seating_capacity", "make", "model", "notes"
|
|
]})
|
|
]
|
|
|
|
def route_travellers(self, obj):
|
|
url = (
|
|
reverse("admin:traveller_traveller_changelist")
|
|
+ "?"
|
|
+ urlencode({"bus_stops__bus__id__exact": f"{obj.id}"})
|
|
)
|
|
return format_html('<a href="{}">{} Travellers</a>', url, obj.traveller_count())
|
|
|
|
|
|
# @admin.register(BusStop)
|
|
class BusStopAdmin(MyImportExportModelAdmin, admin.ModelAdmin):
|
|
list_filter = ["bus__company", "bus__route_name"]
|
|
list_display = ["__str__", "am_time", "pm_time", "address"]
|
|
search_fields = ["bus__route_name", "address"]
|
|
|
|
@admin.register(Shuttle)
|
|
class ShuttleAdmin(MyImportExportModelAdmin, admin.ModelAdmin, ShuttleRollMixin):
|
|
list_display = ["__str__", "school", "bus", "shuttle_travellers"]
|
|
actions = ["show_shuttle_roll", "email_shuttle_roll"]
|
|
|
|
def shuttle_travellers(self, obj):
|
|
url = (
|
|
reverse("admin:traveller_traveller_changelist")
|
|
+ "?"
|
|
+ urlencode({"shuttle__id__exact": f"{obj.id}"})
|
|
)
|
|
return format_html('<a href="{}">{} Travellers</a>', url, obj.traveller_count()) |