200 lines
5.6 KiB
Python
200 lines
5.6 KiB
Python
import csv
|
|
|
|
from django.contrib.admin.utils import unquote
|
|
from django.core.exceptions import PermissionDenied
|
|
from django.forms import widgets
|
|
from django.contrib import admin
|
|
from django.http import HttpResponse, Http404
|
|
from django.urls import reverse
|
|
from django.utils.translation import gettext_lazy
|
|
from django.utils.html import format_html
|
|
from django.utils.http import urlencode
|
|
from import_export.admin import ImportExportModelAdmin
|
|
|
|
from .adminClone import ClonableModelAdmin
|
|
from .models import *
|
|
from .views import bus_roll
|
|
|
|
|
|
class ExportCsvMixin:
|
|
def export_as_csv(self, request, queryset):
|
|
meta = self.model._meta
|
|
field_names = [field.name for field in meta.fields]
|
|
|
|
response = HttpResponse(content_type='text/csv')
|
|
response['Content-Disposition'] = 'attachment; filename={}.csv'.format(meta)
|
|
writer = csv.writer(response)
|
|
|
|
writer.writerow(field_names)
|
|
for obj in queryset:
|
|
row = writer.writerow([getattr(obj, field) for field in field_names])
|
|
|
|
return response
|
|
|
|
export_as_csv.short_description = "Export Selected"
|
|
|
|
|
|
class BusRollMixin:
|
|
|
|
def bus_roll(self, request, queryset):
|
|
return bus_roll(request, queryset)
|
|
|
|
def email_company(self, request, queryset):
|
|
pass
|
|
|
|
email_company.short_description = "Email Bus Roll to Company"
|
|
|
|
|
|
class HiddenNowTime(widgets.TimeInput):
|
|
pass
|
|
|
|
|
|
class MyImportExportModelAdmin(ImportExportModelAdmin):
|
|
def has_import_permission(self, request):
|
|
return request.user.is_superuser
|
|
|
|
|
|
@admin.register(Company)
|
|
class CompanyAdmin(admin.ModelAdmin):
|
|
list_display = ["name", "buses"]
|
|
|
|
def buses(self, obj):
|
|
count = obj.bus_set.count()
|
|
url = (
|
|
reverse("admin:coord_bus_changelist")
|
|
+ "?"
|
|
+ urlencode({"company__id": f"{obj.id}"})
|
|
)
|
|
return format_html('<a href="{}">{} Buses</a>', url, count)
|
|
|
|
|
|
class DriverInline(admin.StackedInline):
|
|
model = Driver
|
|
extra = 1
|
|
|
|
|
|
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"]
|
|
actions = ["email_company", "bus_roll"]
|
|
inlines = [DriverInline, BusStopInline]
|
|
|
|
|
|
@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"]
|
|
|
|
# change_form_template = 'admin/change_form_busStops.html'
|
|
|
|
def export_as_csv(self, request, queryset):
|
|
pass
|
|
|
|
|
|
@admin.register(Suburb)
|
|
class SuburbsAdmin(MyImportExportModelAdmin, admin.ModelAdmin):
|
|
list_filter = ["state"]
|
|
|
|
|
|
class TravellerRouteInline(admin.TabularInline):
|
|
model = TravellerRoute
|
|
extra = 0
|
|
|
|
|
|
@admin.register(Traveller)
|
|
class TravellerAdmin(MyImportExportModelAdmin, admin.ModelAdmin):
|
|
list_display = ["first_name", "last_name", "school", "year_level", "residential_address", "residential_suburb"]
|
|
list_filter = ["school", "eligibility_status", "bus_stops__bus", "residential_suburb"]
|
|
search_fields = ["first_name", "last_name", "residential_address"]
|
|
inlines = [TravellerRouteInline]
|
|
readonly_fields = ["fare_paying"]
|
|
fieldsets = [
|
|
(None, {
|
|
'fields': [
|
|
"school",
|
|
"first_name",
|
|
"last_name",
|
|
"dob",
|
|
"year_level",
|
|
]
|
|
}),
|
|
('Address', {
|
|
'classes': ('collapse',),
|
|
'fields': [
|
|
"residential_address",
|
|
"residential_suburb",
|
|
"postal_address",
|
|
"postal_suburb",
|
|
]
|
|
}),
|
|
('Office Use', {
|
|
'classes': ('collapse',),
|
|
'fields': [
|
|
"distance_to_school",
|
|
"travel_start_date",
|
|
"travel_end_date",
|
|
"eligibility_status",
|
|
"fare_paying",
|
|
"assessment_date",
|
|
"application_form_completed",
|
|
"parent_notified",
|
|
"seat_number",
|
|
]
|
|
}),
|
|
('Adult Contacts', {
|
|
'classes': ('collapse',),
|
|
'fields': [
|
|
"parent_A_firstname",
|
|
"parent_A_lastname",
|
|
"parent_A_phone",
|
|
"parent_A_email",
|
|
"parent_B_firstname",
|
|
"parent_B_lastname",
|
|
"parent_B_phone",
|
|
"parent_B_email",
|
|
"emergency_contact_A_firstname",
|
|
"emergency_contact_A_lastname",
|
|
"emergency_contact_A_phone",
|
|
"emergency_contact_A_relation",
|
|
"emergency_contact_B_firstname",
|
|
"emergency_contact_B_lastname",
|
|
"emergency_contact_B_phone",
|
|
"emergency_contact_B_relation"
|
|
]
|
|
}),
|
|
(None, {'fields': ["notes", "shuttle"]})
|
|
]
|
|
# list_display_links = None
|
|
|
|
actions = ["yearly_rollover"]
|
|
|
|
def yearly_rollover(self, request, queryset):
|
|
pass
|
|
|
|
|
|
@admin.register(TravellerRoute)
|
|
class TravellerRouteAdmin(MyImportExportModelAdmin, admin.ModelAdmin):
|
|
list_display = ["traveller", "busStop"]
|
|
|
|
|
|
@admin.register(School)
|
|
class SchoolAdmin(MyImportExportModelAdmin, admin.ModelAdmin):
|
|
pass
|
|
|
|
|
|
@admin.register(Setting)
|
|
class SettingAdmin(MyImportExportModelAdmin, admin.ModelAdmin):
|
|
list_display = ["name", "value"]
|
|
|
|
|
|
admin.site.register(Shuttle)
|
|
admin.site.register(Driver)
|