Added fields to changelist view. Added hyperlinks and mailto links

This commit is contained in:
John Mullins
2023-08-29 16:16:38 +10:00
parent 4eb2909a16
commit eb03670290
2 changed files with 36 additions and 21 deletions
+19 -5
View File
@@ -1,6 +1,7 @@
import csv import csv
from django.contrib.admin import DateFieldListFilter from django.contrib.admin import DateFieldListFilter
from django.contrib.admin.views.decorators import staff_member_required
from django.forms import widgets from django.forms import widgets
from django.contrib import admin from django.contrib import admin
from django.http import HttpResponse from django.http import HttpResponse
@@ -10,7 +11,7 @@ from django.utils.http import urlencode
from import_export.admin import ImportExportModelAdmin from import_export.admin import ImportExportModelAdmin
from .adminClone import CloneModelAdmin from .adminClone import CloneModelAdmin
from .context_helpers import bus_roll_context from .context_helpers import bus_roll_context, emergency_contacts_context
from .email_helpers import email_companies_bus_roll, render_to_pdf from .email_helpers import email_companies_bus_roll, render_to_pdf
from .models import * from .models import *
@@ -38,6 +39,9 @@ class BusRollMixin:
def show_bus_roll(self, request, queryset): def show_bus_roll(self, request, queryset):
return render_to_pdf('reports/bus_roll.html', bus_roll_context(queryset)) return render_to_pdf('reports/bus_roll.html', bus_roll_context(queryset))
def show_emergency_contacts(self, request, queryset):
return render_to_pdf('reports/emergency_contacts.html', emergency_contacts_context(queryset))
def email_company(self, request, queryset): def email_company(self, request, queryset):
return email_companies_bus_roll(request, queryset) return email_companies_bus_roll(request, queryset)
@@ -55,7 +59,7 @@ class MyImportExportModelAdmin(ImportExportModelAdmin):
@admin.register(Company) @admin.register(Company)
class CompanyAdmin(MyImportExportModelAdmin, admin.ModelAdmin): class CompanyAdmin(MyImportExportModelAdmin, admin.ModelAdmin):
list_display = ["name", "buses"] list_display = ["name", "contact_name", "email", "buses"]
def buses(self, obj): def buses(self, obj):
count = obj.bus_set.count() count = obj.bus_set.count()
@@ -66,6 +70,9 @@ class CompanyAdmin(MyImportExportModelAdmin, admin.ModelAdmin):
) )
return format_html('<a href="{}">{} Buses</a>', url, count) 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): class DriverInline(admin.StackedInline):
model = Driver model = Driver
@@ -83,7 +90,7 @@ class BusesAdmin(MyImportExportModelAdmin, admin.ModelAdmin, BusRollMixin):
list_filter = ["company"] list_filter = ["company"]
list_display = ["route_name", "company", "contract_number", "route_travellers"] list_display = ["route_name", "company", "contract_number", "route_travellers"]
readonly_fields = ["traveller_count"] readonly_fields = ["traveller_count"]
actions = ["email_company", "show_bus_roll"] actions = ["email_company", "show_bus_roll", "show_emergency_contacts"]
inlines = [DriverInline, BusStopInline] inlines = [DriverInline, BusStopInline]
fieldsets = [ fieldsets = [
(None, {'fields': [ (None, {'fields': [
@@ -222,7 +229,10 @@ class TravellerRouteAdmin(MyImportExportModelAdmin, admin.ModelAdmin):
@admin.register(School) @admin.register(School)
class SchoolAdmin(MyImportExportModelAdmin, admin.ModelAdmin): class SchoolAdmin(MyImportExportModelAdmin, admin.ModelAdmin):
pass list_display = ["__str__", "address", "suburb", "school_email", "phone"]
def school_email(self, obj):
return format_html('<a href="mailto:{}">{}</a>', obj.email, obj.email)
@admin.register(Setting) @admin.register(Setting)
@@ -245,4 +255,8 @@ class ShuttleAdmin(MyImportExportModelAdmin, admin.ModelAdmin):
@admin.register(Driver) @admin.register(Driver)
class DriverAdmin(MyImportExportModelAdmin, admin.ModelAdmin): class DriverAdmin(MyImportExportModelAdmin, admin.ModelAdmin):
pass list_display = ["__str__", "route", "phone_number"]
def route(self, obj):
url = reverse("admin:coord_bus_change", args=(obj.bus.id,))
return format_html('<a href="{}">{}</a>', url, obj.bus)
@@ -1,4 +1,8 @@
<style> <style>
@page {
size: letter landscape;
margin: 0.5cm;
}
table.stopHeader th { table.stopHeader th {
text-align: left; text-align: left;
@@ -11,32 +15,29 @@
table.traveller td { table.traveller td {
table-layout: fixed; padding-top: 3px;
border-bottom: 2px solid #000; border: 1px solid #000;
border-right-style: dashed;
} }
hr {
border: 2px solid;
}
</style> </style>
{% for route in routes %} {% for route in routes %}
<h1>{{ route.bus }}</h1> <h1 style="font-size: 2.5em">{{ route.bus.company }} - {{ route.bus }}</h1>
<hr> {% for driver in route.drivers %}
<p><b>Driver:</b> {{ driver }} ({{ driver.phone_number }})</p>
{% endfor %}
<table class="traveller"> <table class="traveller">
<tr> <tr>
<th>Student</th> <th style="width: 25%">Student</th>
<th>Parent A</th> <th style="width: 25%">Parent A</th>
<th>Parent B</th> <th style="width: 25%">Parent B</th>
<th>Emergency Contact A</th> <th style="width: 25%">Emergency Contact A</th>
<th>Emergency Contact B</th> <th style="width: 25%">Emergency Contact B</th>
<th>Driver notes</th> <th style="width: 40%">Driver notes</th>
</tr> </tr>
{% for traveller in route.travellers %} {% for traveller in route.travellers %}
<tr> <tr>
<td>{{ traveller.traveller }} ({{ traveller.traveller.school.shortName }})</td> <td>{{ traveller.traveller.last_name }}, {{ traveller.traveller.first_name }} ({{ traveller.traveller.school.shortName }})</td>
<td>{{ traveller.parent_a }}</td> <td>{{ traveller.parent_a }}</td>
<td>{{ traveller.parent_b }}</td> <td>{{ traveller.parent_b }}</td>
<td>{{ traveller.contact_a }}</td> <td>{{ traveller.contact_a }}</td>