137 lines
5.0 KiB
Python
137 lines
5.0 KiB
Python
from datetime import date
|
|
from io import BytesIO
|
|
|
|
from django.core.mail import EmailMessage
|
|
from django.http import HttpResponse
|
|
from django.template.loader import get_template
|
|
from xhtml2pdf import pisa
|
|
|
|
from .models import Company, School, Setting
|
|
from .context_helpers import bus_roll_context, emergency_contacts_context, school_roll_context
|
|
|
|
|
|
def _getBCC(request):
|
|
if request.user.email:
|
|
return [request.user.email]
|
|
return []
|
|
|
|
|
|
def render_to_pdf(template, context):
|
|
html = get_template(template).render(context)
|
|
result = BytesIO()
|
|
pdf = pisa.pisaDocument(BytesIO(html.encode("ISO-8859-1")), result)
|
|
if pdf.err:
|
|
return HttpResponse("Invalid PDF", status_code=400, content_type='text/plan')
|
|
return HttpResponse(result.getvalue(), content_type='application/pdf')
|
|
|
|
|
|
def email_companies_bus_roll(request, query_set=None):
|
|
html_template = 'reports/bus_roll.html'
|
|
context = bus_roll_context(query_set)
|
|
|
|
for company in Company.objects.all():
|
|
if not company.contact_email:
|
|
continue
|
|
company_route = []
|
|
|
|
for route in context.get("routes"):
|
|
if route.get("bus").company == company:
|
|
company_route.append(route)
|
|
if not company_route:
|
|
continue
|
|
company_context = {'routes': company_route}
|
|
pdf = render_to_pdf(html_template, company_context)
|
|
|
|
subject = "Echuca Schools Bus Roll"
|
|
message = f"A new bus roll for {company.name} has been generated"
|
|
email_from = "bus.manager@education.vic.gov.au"
|
|
recipient = [company.contact_email]
|
|
email = EmailMessage(subject, message, email_from, recipient, _getBCC(request))
|
|
email.attach(f"school_bus_roll_{date.today()}.pdf", pdf.content)
|
|
email.send(fail_silently=True)
|
|
|
|
return render_to_pdf(html_template, context)
|
|
|
|
|
|
def email_companies_emergency_contacts(request, query_set=None):
|
|
html_template = 'reports/emergency_contacts.html'
|
|
context = emergency_contacts_context(query_set)
|
|
|
|
for company in Company.objects.all():
|
|
if not company.contact_email:
|
|
continue
|
|
company_route = []
|
|
|
|
for route in context.get("routes"):
|
|
if route.get("bus").company == company:
|
|
company_route.append(route)
|
|
if not company_route:
|
|
continue
|
|
company_context = {'routes': company_route}
|
|
pdf = render_to_pdf(html_template, company_context)
|
|
|
|
subject = "Echuca School Buses Emergency Contacts"
|
|
message = f"A new emergency contact list for {company.name} has been generated"
|
|
email_from = "bus.manager@education.vic.gov.au"
|
|
recipient = [company.contact_email]
|
|
email = EmailMessage(subject, message, email_from, recipient, _getBCC(request))
|
|
email.attach(f"school_bus_roll_{date.today()}.pdf", pdf.content)
|
|
email.send(fail_silently=True)
|
|
|
|
return render_to_pdf(html_template, context)
|
|
|
|
|
|
def email_school_roll(request, query_set):
|
|
html_template = 'reports/school_roll.html'
|
|
context = school_roll_context(query_set)
|
|
|
|
for school in School.objects.all():
|
|
if not school.email:
|
|
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)
|
|
|
|
subject = "Echuca Schools Bus Roll"
|
|
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, _getBCC(request))
|
|
email.attach(f"school_bus_roll_{date.today()}.pdf", pdf.content)
|
|
email.send(fail_silently=True)
|
|
|
|
return render_to_pdf(html_template, context)
|
|
|
|
|
|
def email_school_shuttle_roll(request, queryset):
|
|
html_template = 'reports/bus_roll.html'
|
|
|
|
schools = []
|
|
for shuttle in queryset:
|
|
if shuttle.school not in schools:
|
|
schools.append(shuttle.school)
|
|
for school in schools:
|
|
buses = []
|
|
for shuttle in queryset:
|
|
if shuttle.school == school:
|
|
buses.append(shuttle.bus)
|
|
pdf = render_to_pdf(html_template, bus_roll_context(buses, include_bus_stops=False))
|
|
|
|
subject = "Echuca Schools Shuttle Roll"
|
|
message = f"A new shuttle 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, _getBCC(request))
|
|
email.attach(f"school_shuttle_roll_{date.today()}.pdf", pdf.content)
|
|
email.send(fail_silently=True)
|
|
buses = []
|
|
for shuttle in queryset:
|
|
if shuttle.bus not in buses:
|
|
buses.append(shuttle.bus)
|
|
return render_to_pdf(html_template, bus_roll_context(buses, include_bus_stops=False))
|