Email roll to schools function
This commit is contained in:
@@ -13,8 +13,8 @@ from import_export.admin import ImportExportModelAdmin
|
|||||||
|
|
||||||
from .adminClone import CloneModelAdmin
|
from .adminClone import CloneModelAdmin
|
||||||
from .context_helpers import bus_roll_context, emergency_contacts_context, traveller_roll_context, \
|
from .context_helpers import bus_roll_context, emergency_contacts_context, traveller_roll_context, \
|
||||||
traveller_route_context
|
traveller_route_context, school_roll_context
|
||||||
from .email_helpers import email_companies_bus_roll, render_to_pdf, email_school_roll_csv
|
from .email_helpers import email_companies_bus_roll, render_to_pdf, email_school_roll
|
||||||
from .models import *
|
from .models import *
|
||||||
|
|
||||||
|
|
||||||
@@ -34,8 +34,13 @@ class BusRollMixin:
|
|||||||
|
|
||||||
class SchoolRollMixin:
|
class SchoolRollMixin:
|
||||||
|
|
||||||
def export_travellers_to_csv(self, request, queryset):
|
def email_travellers_to_school(self, request, queryset):
|
||||||
|
return email_school_roll(request, queryset)
|
||||||
|
|
||||||
|
def show_school_travellers(self, request, queryset):
|
||||||
|
return render_to_pdf('reports/school_roll.html', school_roll_context(queryset))
|
||||||
|
|
||||||
|
def export_travellers_to_csv(self, request, queryset):
|
||||||
traveller_list = []
|
traveller_list = []
|
||||||
for school in queryset:
|
for school in queryset:
|
||||||
for travellerRoute in TravellerRoute.objects.filter(traveller__school=school):
|
for travellerRoute in TravellerRoute.objects.filter(traveller__school=school):
|
||||||
@@ -270,7 +275,7 @@ class TravellerRouteAdmin(MyImportExportModelAdmin, admin.ModelAdmin):
|
|||||||
@admin.register(School)
|
@admin.register(School)
|
||||||
class SchoolAdmin(MyImportExportModelAdmin, admin.ModelAdmin, SchoolRollMixin):
|
class SchoolAdmin(MyImportExportModelAdmin, admin.ModelAdmin, SchoolRollMixin):
|
||||||
list_display = ["__str__", "address", "suburb", "school_email", "phone"]
|
list_display = ["__str__", "address", "suburb", "school_email", "phone"]
|
||||||
actions = ["export_travellers_to_csv"]
|
actions = ["email_travellers_to_school", "show_school_travellers", "export_travellers_to_csv"]
|
||||||
|
|
||||||
def school_email(self, obj):
|
def school_email(self, obj):
|
||||||
return format_html('<a href="mailto:{}">{}</a>', obj.email, obj.email)
|
return format_html('<a href="mailto:{}">{}</a>', obj.email, obj.email)
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
from django.db.models import Q
|
from django.db.models import Q
|
||||||
|
|
||||||
from coord.models import Bus, BusStop, TravellerRoute, Driver, Traveller, Shuttle, School
|
from coord.models import Bus, BusStop, TravellerRoute, Driver, Traveller, Shuttle
|
||||||
|
|
||||||
|
|
||||||
def bus_roll_context(queryset=None):
|
def bus_roll_context(queryset=None):
|
||||||
@@ -40,14 +40,10 @@ def bus_roll_context(queryset=None):
|
|||||||
return {'routes': bus_routes}
|
return {'routes': bus_routes}
|
||||||
|
|
||||||
|
|
||||||
def school_roll_context(queryset=None):
|
def school_roll_context(queryset):
|
||||||
school_list = []
|
school_list = []
|
||||||
if queryset is None:
|
|
||||||
schools = School.objects.all()
|
|
||||||
else:
|
|
||||||
schools = queryset
|
|
||||||
|
|
||||||
for school in schools:
|
for school in queryset:
|
||||||
school_routes = []
|
school_routes = []
|
||||||
query = Q(traveller__school=school) | Q(traveller__shuttle__school=school)
|
query = Q(traveller__school=school) | Q(traveller__shuttle__school=school)
|
||||||
for bus in Bus.objects.all():
|
for bus in Bus.objects.all():
|
||||||
@@ -64,9 +60,13 @@ def school_roll_context(queryset=None):
|
|||||||
is_fared = "---"
|
is_fared = "---"
|
||||||
if traveller.eligibility_status == "2":
|
if traveller.eligibility_status == "2":
|
||||||
is_fared = "Y"
|
is_fared = "Y"
|
||||||
|
shuttle_name = " "
|
||||||
|
if traveller.shuttle:
|
||||||
|
shuttle_name = traveller.shuttle.bus
|
||||||
travellers.append({
|
travellers.append({
|
||||||
'display': display_name,
|
'display': display_name,
|
||||||
'isFared': is_fared,
|
'isFared': is_fared,
|
||||||
|
'shuttle': shuttle_name,
|
||||||
'stop': f"#{bus_stop.get_stop_number()} - {bus_stop.address}",
|
'stop': f"#{bus_stop.get_stop_number()} - {bus_stop.address}",
|
||||||
'am_time': bus_stop.am_time,
|
'am_time': bus_stop.am_time,
|
||||||
'pm_time': bus_stop.pm_time
|
'pm_time': bus_stop.pm_time
|
||||||
|
|||||||
@@ -1,14 +1,13 @@
|
|||||||
import csv
|
|
||||||
from datetime import date
|
from datetime import date
|
||||||
from io import BytesIO, StringIO
|
from io import BytesIO
|
||||||
|
|
||||||
from django.core.mail import EmailMessage
|
from django.core.mail import EmailMessage
|
||||||
from django.http import HttpResponse
|
from django.http import HttpResponse
|
||||||
from django.template.loader import get_template
|
from django.template.loader import get_template
|
||||||
from xhtml2pdf import pisa
|
from xhtml2pdf import pisa
|
||||||
|
|
||||||
from coord.context_helpers import bus_roll_context, school_travellerRoute_context
|
from coord.context_helpers import bus_roll_context, school_roll_context
|
||||||
from coord.models import Company
|
from coord.models import Company, School
|
||||||
|
|
||||||
|
|
||||||
def render_to_pdf(template, context):
|
def render_to_pdf(template, context):
|
||||||
@@ -47,20 +46,28 @@ def email_companies_bus_roll(request, query_set=None):
|
|||||||
return render_to_pdf(html_template, context)
|
return render_to_pdf(html_template, context)
|
||||||
|
|
||||||
|
|
||||||
def email_school_roll_csv(request, school):
|
def email_school_roll(request, query_set):
|
||||||
|
html_template = 'reports/school_roll.html'
|
||||||
|
context = school_roll_context(query_set)
|
||||||
|
|
||||||
travellers = school_travellerRoute_context(school)
|
for school in School.objects.all():
|
||||||
csvFile = StringIO()
|
if not school.email:
|
||||||
fieldnames = list(travellers[0].keys())
|
continue
|
||||||
|
school_route = []
|
||||||
|
for school_context in context.get("schools"):
|
||||||
|
if school_context.get("name") == school.name:
|
||||||
|
school_route.append(school_context)
|
||||||
|
if not school_route:
|
||||||
|
continue
|
||||||
|
school_context = {'schools': school_route}
|
||||||
|
pdf = render_to_pdf(html_template, school_context)
|
||||||
|
|
||||||
writer = csv.DictWriter(csvFile, fieldnames=fieldnames)
|
subject = "Echuca Schools Bus Roll"
|
||||||
writer.writeheader(),
|
message = f"A new bus roll for {school.name} has been generated"
|
||||||
writer.writerows(travellers)
|
email_from = "bus.manager@education.vic.gov.au"
|
||||||
|
recipient = [school.email]
|
||||||
|
email = EmailMessage(subject, message, email_from, recipient)
|
||||||
|
email.attach(f"school_bus_roll_{date.today()}.pdf", pdf.content)
|
||||||
|
email.send(fail_silently=True)
|
||||||
|
|
||||||
subject = "Bus Roll"
|
return render_to_pdf(html_template, context)
|
||||||
message = f"A new bus roll for {school.name} has been generated"
|
|
||||||
email_from = "bus.manager@education.vic.gov.au"
|
|
||||||
recipient = [school.email]
|
|
||||||
email = EmailMessage(subject, message, email_from, recipient)
|
|
||||||
email.attach(f"school_bus_roll_{date.today()}.csv", csvFile.getvalue(), 'text/csv')
|
|
||||||
email.send()
|
|
||||||
@@ -14,16 +14,18 @@
|
|||||||
<h2>{{ route.bus }}</h2>
|
<h2>{{ route.bus }}</h2>
|
||||||
<table>
|
<table>
|
||||||
<tr>
|
<tr>
|
||||||
<th style="width: 60%">Traveller</th>
|
<th style="width: 35%">Traveller</th>
|
||||||
<th style="width: 5%">Fare</th>
|
<th style="width: 5%">Fare</th>
|
||||||
<th style="width: 40%">Stop</th>
|
<th style="width: 30%">Shuttle</th>
|
||||||
<th style="width: 12%">AM Time</th>
|
<th style="width: 30%">Stop</th>
|
||||||
<th style="width: 12%">PM Time</th>
|
<th style="width: 8%">AM Time</th>
|
||||||
|
<th style="width: 8%">PM Time</th>
|
||||||
</tr>
|
</tr>
|
||||||
{% for traveller in route.travellers %}
|
{% for traveller in route.travellers %}
|
||||||
<tr>
|
<tr>
|
||||||
<td style="padding-left: 2px">{{ traveller.display }}</td>
|
<td style="padding-left: 2px">{{ traveller.display }}</td>
|
||||||
<td style="text-align: center">{{ traveller.isFared }}</td>
|
<td style="text-align: center">{{ traveller.isFared }}</td>
|
||||||
|
<td style="padding-left: 2px">{{ traveller.shuttle }}</td>
|
||||||
<td style="padding-left: 2px">{{ traveller.stop }}</td>
|
<td style="padding-left: 2px">{{ traveller.stop }}</td>
|
||||||
<td style="text-align: center">{{ traveller.am_time }}</td>
|
<td style="text-align: center">{{ traveller.am_time }}</td>
|
||||||
<td style="text-align: center">{{ traveller.pm_time }}</td>
|
<td style="text-align: center">{{ traveller.pm_time }}</td>
|
||||||
|
|||||||
@@ -7,5 +7,4 @@ urlpatterns = [
|
|||||||
path("contacts", views.emergency_contacts, name="emergency_contacts"),
|
path("contacts", views.emergency_contacts, name="emergency_contacts"),
|
||||||
path("roll", views.bus_roll, name="bus_roll"),
|
path("roll", views.bus_roll, name="bus_roll"),
|
||||||
path("summary", views.bus_summary, name="bus_summary"),
|
path("summary", views.bus_summary, name="bus_summary"),
|
||||||
path("school", views.school_roll, name="school_roll"),
|
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -18,8 +18,3 @@ def emergency_contacts(request):
|
|||||||
@staff_member_required
|
@staff_member_required
|
||||||
def bus_roll(request):
|
def bus_roll(request):
|
||||||
return render_to_pdf('reports/bus_roll.html', bus_roll_context())
|
return render_to_pdf('reports/bus_roll.html', bus_roll_context())
|
||||||
|
|
||||||
|
|
||||||
@staff_member_required
|
|
||||||
def school_roll(request):
|
|
||||||
return render_to_pdf('reports/school_roll.html', school_roll_context())
|
|
||||||
|
|||||||
Reference in New Issue
Block a user