Added email emergency contacts to bus companies function
This commit is contained in:
@@ -12,7 +12,8 @@ from rangefilter.filters import DateRangeFilterBuilder
|
||||
|
||||
from .adminClone import CloneModelAdmin
|
||||
from .context_helpers import *
|
||||
from .email_helpers import email_companies_bus_roll, render_to_pdf, email_school_roll
|
||||
from .email_helpers import email_companies_bus_roll, render_to_pdf, email_school_roll, \
|
||||
email_companies_emergency_contacts
|
||||
from .models import *
|
||||
from .utils.send_sms import send_sms
|
||||
|
||||
@@ -25,10 +26,14 @@ class BusRollMixin:
|
||||
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_bus_roll(self, request, queryset):
|
||||
return email_companies_bus_roll(request, queryset)
|
||||
|
||||
email_company.short_description = "Email Bus Roll to Company"
|
||||
def email_emergency_contacts(self, request, queryset):
|
||||
return email_companies_emergency_contacts(request, queryset)
|
||||
|
||||
email_bus_roll.short_description = "Email Bus Roll to Company"
|
||||
email_emergency_contacts.short_description = "Email Emergency Contacts to Company"
|
||||
|
||||
|
||||
class ShuttleRollMixin:
|
||||
@@ -132,7 +137,7 @@ class BusesAdmin(MyImportExportModelAdmin, admin.ModelAdmin, BusRollMixin):
|
||||
list_filter = ["company"]
|
||||
list_display = ["route_name", "company", "contract_number", "seating_capacity", "route_travellers"]
|
||||
readonly_fields = ["traveller_count"]
|
||||
actions = ["email_company", "show_bus_roll", "show_emergency_contacts"]
|
||||
actions = ["show_bus_roll", "show_emergency_contacts", "email_bus_roll", "email_emergency_contacts"]
|
||||
inlines = [DriverInline, BusStopInline]
|
||||
fieldsets = [
|
||||
(None, {'fields': [
|
||||
|
||||
@@ -6,8 +6,8 @@ from django.http import HttpResponse
|
||||
from django.template.loader import get_template
|
||||
from xhtml2pdf import pisa
|
||||
|
||||
from coord.context_helpers import bus_roll_context, school_roll_context
|
||||
from coord.models import Company, School
|
||||
from .models import Company, School
|
||||
from .context_helpers import bus_roll_context, emergency_contacts_context, school_roll_context
|
||||
|
||||
|
||||
def render_to_pdf(template, context):
|
||||
@@ -47,6 +47,34 @@ def email_companies_bus_roll(request, query_set=None):
|
||||
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)
|
||||
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)
|
||||
@@ -71,4 +99,4 @@ def email_school_roll(request, query_set):
|
||||
email.attach(f"school_bus_roll_{date.today()}.pdf", pdf.content)
|
||||
email.send(fail_silently=True)
|
||||
|
||||
return render_to_pdf(html_template, context)
|
||||
return render_to_pdf(html_template, context)
|
||||
|
||||
Reference in New Issue
Block a user