Changed bus roll to be a pdf view. Small changes needed to be able to email to companies.
Dropdown selection in admin > buses > Email Bus Roll To Company Will email bus roll only to the company the route is a member of. Will show all to the admin in a pdf file. Recipients are currently hardcoded for testing purposes.
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
from datetime import date
|
||||
from io import BytesIO
|
||||
|
||||
from django.contrib.admin.views.decorators import staff_member_required
|
||||
from django.core.mail import send_mail, EmailMessage
|
||||
from django.http import HttpResponse
|
||||
from django.template.loader import get_template
|
||||
from xhtml2pdf import pisa
|
||||
|
||||
from coord.context_helpers import bus_roll_context
|
||||
from coord.models import Company
|
||||
|
||||
|
||||
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')
|
||||
|
||||
|
||||
@staff_member_required
|
||||
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():
|
||||
company_route = []
|
||||
for route in context.get("routes"):
|
||||
bus = route.get("bus")
|
||||
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 = ["john.mullins@education.vic.gov.au", "nicole.edwards@education.vic.gov.au"]
|
||||
email = EmailMessage(subject, message, email_from, recipient)
|
||||
email.attach(f"school_bus_roll_{date.today()}.pdf", pdf.content)
|
||||
email.send()
|
||||
|
||||
return render_to_pdf(html_template, context)
|
||||
Reference in New Issue
Block a user