Added date selector to rolls
This commit is contained in:
@@ -14,6 +14,7 @@ from .adminClone import CloneModelAdmin
|
|||||||
from .context_helpers import *
|
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
|
email_companies_emergency_contacts
|
||||||
|
from .forms import roll_date_selector
|
||||||
from .models import *
|
from .models import *
|
||||||
from .utils.send_sms import send_sms
|
from .utils.send_sms import send_sms
|
||||||
|
|
||||||
@@ -23,6 +24,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_bus_roll_on_date(self, request, queryset):
|
||||||
|
return roll_date_selector(self, request, queryset)
|
||||||
|
|
||||||
def show_emergency_contacts(self, request, queryset):
|
def show_emergency_contacts(self, request, queryset):
|
||||||
return render_to_pdf('reports/emergency_contacts.html', emergency_contacts_context(queryset))
|
return render_to_pdf('reports/emergency_contacts.html', emergency_contacts_context(queryset))
|
||||||
|
|
||||||
@@ -67,6 +71,9 @@ class SchoolRollMixin:
|
|||||||
def show_school_travellers(self, request, queryset):
|
def show_school_travellers(self, request, queryset):
|
||||||
return render_to_pdf('reports/school_roll.html', school_roll_context(queryset))
|
return render_to_pdf('reports/school_roll.html', school_roll_context(queryset))
|
||||||
|
|
||||||
|
def show_school_travellers_on_date(self, request, queryset):
|
||||||
|
return render_to_pdf('reports/school_roll.html', school_roll_context(queryset))
|
||||||
|
|
||||||
def export_travellers_to_csv(self, request, queryset):
|
def export_travellers_to_csv(self, request, queryset):
|
||||||
traveller_list = []
|
traveller_list = []
|
||||||
for school in queryset:
|
for school in queryset:
|
||||||
@@ -147,7 +154,7 @@ class BusesAdmin(MyImportExportModelAdmin, admin.ModelAdmin, BusRollMixin):
|
|||||||
list_filter = ["company"]
|
list_filter = ["company"]
|
||||||
list_display = ["route_name", "company", "contract_number", "seating_capacity", "route_travellers"]
|
list_display = ["route_name", "company", "contract_number", "seating_capacity", "route_travellers"]
|
||||||
readonly_fields = ["traveller_count"]
|
readonly_fields = ["traveller_count"]
|
||||||
actions = ["show_bus_roll", "show_emergency_contacts", "sms_traveller_contacts", "email_bus_roll", "email_emergency_contacts"]
|
actions = ["show_bus_roll", "show_bus_roll_on_date", "show_emergency_contacts", "sms_traveller_contacts", "email_bus_roll", "email_emergency_contacts"]
|
||||||
inlines = [DriverInline, BusStopInline]
|
inlines = [DriverInline, BusStopInline]
|
||||||
fieldsets = [
|
fieldsets = [
|
||||||
(None, {'fields': [
|
(None, {'fields': [
|
||||||
|
|||||||
@@ -5,8 +5,7 @@ from django.db.models import Q
|
|||||||
from .models import Bus, BusStop, TravellerRoute, Driver, Traveller, Shuttle
|
from .models import Bus, BusStop, TravellerRoute, Driver, Traveller, Shuttle
|
||||||
|
|
||||||
|
|
||||||
def bus_roll_context(queryset=None, include_bus_stops=True):
|
def bus_roll_context(queryset=None, include_bus_stops=True, date=None):
|
||||||
date = datetime.date.today().strftime('%Y-%m-%d')
|
|
||||||
bus_routes = []
|
bus_routes = []
|
||||||
if queryset is None:
|
if queryset is None:
|
||||||
buses = Bus.objects.all()
|
buses = Bus.objects.all()
|
||||||
@@ -16,17 +15,19 @@ def bus_roll_context(queryset=None, include_bus_stops=True):
|
|||||||
for bus in buses:
|
for bus in buses:
|
||||||
route_stops = []
|
route_stops = []
|
||||||
if include_bus_stops:
|
if include_bus_stops:
|
||||||
route_stops = route_paged_context(bus)
|
route_stops = route_paged_context(bus=bus, date=date)
|
||||||
|
|
||||||
shuttle_routes = []
|
shuttle_routes = []
|
||||||
for shuttle in Shuttle.objects.filter(bus=bus):
|
for shuttle in Shuttle.objects.filter(bus=bus):
|
||||||
shuttle_routes.append(shuttle_route_context(shuttle))
|
shuttle_routes.append(shuttle_route_context(shuttle))
|
||||||
|
|
||||||
bus_routes.append({'bus': bus, 'route_stops': route_stops, 'shuttle_routes': shuttle_routes})
|
bus_routes.append({'bus': bus, 'route_stops': route_stops, 'shuttle_routes': shuttle_routes})
|
||||||
return {'routes': bus_routes, 'date': date}
|
if date is None:
|
||||||
|
date = datetime.date.today()
|
||||||
|
return {'routes': bus_routes, 'date': date.strftime('%Y-%m-%d')}
|
||||||
|
|
||||||
|
|
||||||
def school_roll_context(queryset):
|
def school_roll_context(queryset, date=None):
|
||||||
school_list = []
|
school_list = []
|
||||||
|
|
||||||
for school in queryset:
|
for school in queryset:
|
||||||
@@ -36,7 +37,7 @@ def school_roll_context(queryset):
|
|||||||
travellers = []
|
travellers = []
|
||||||
for trav_route in TravellerRoute.objects.filter(query).filter(busStop__bus=bus).order_by('busStop__am_time'):
|
for trav_route in TravellerRoute.objects.filter(query).filter(busStop__bus=bus).order_by('busStop__am_time'):
|
||||||
traveller = trav_route.traveller
|
traveller = trav_route.traveller
|
||||||
if not traveller._is_active():
|
if not traveller._is_active(date):
|
||||||
continue
|
continue
|
||||||
bus_stop = trav_route.busStop
|
bus_stop = trav_route.busStop
|
||||||
|
|
||||||
@@ -75,7 +76,7 @@ def traveller_roll_context(queryset):
|
|||||||
return travellers
|
return travellers
|
||||||
|
|
||||||
|
|
||||||
def route_paged_context(bus):
|
def route_paged_context(bus, date=None):
|
||||||
table_header_size = 5
|
table_header_size = 5
|
||||||
page_max_size = 45
|
page_max_size = 45
|
||||||
page_size = 3 # Account for traveller numbers at the top of the first page
|
page_size = 3 # Account for traveller numbers at the top of the first page
|
||||||
@@ -85,7 +86,7 @@ def route_paged_context(bus):
|
|||||||
traveller_list = []
|
traveller_list = []
|
||||||
for trav_route in traveller_routes:
|
for trav_route in traveller_routes:
|
||||||
traveller = trav_route.traveller
|
traveller = trav_route.traveller
|
||||||
if not traveller._is_active():
|
if not traveller._is_active(date):
|
||||||
continue
|
continue
|
||||||
is_fared = "---"
|
is_fared = "---"
|
||||||
if traveller.eligibility_status == "2":
|
if traveller.eligibility_status == "2":
|
||||||
@@ -124,11 +125,11 @@ def shuttle_route_context(shuttle):
|
|||||||
return {'shuttle': shuttle, 'shuttle_travellers': shuttle_travellers}
|
return {'shuttle': shuttle, 'shuttle_travellers': shuttle_travellers}
|
||||||
|
|
||||||
|
|
||||||
def school_travellerRoute_context(school):
|
def school_travellerRoute_context(school, date=None):
|
||||||
travellers = []
|
travellers = []
|
||||||
for travellerRoute in TravellerRoute.objects.filter(traveller__school=school):
|
for travellerRoute in TravellerRoute.objects.filter(traveller__school=school):
|
||||||
traveller = travellerRoute.traveller
|
traveller = travellerRoute.traveller
|
||||||
if not traveller._is_active():
|
if not traveller._is_active(date):
|
||||||
continue
|
continue
|
||||||
travellers.append(traveller_route_context(travellerRoute))
|
travellers.append(traveller_route_context(travellerRoute))
|
||||||
return travellers
|
return travellers
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
from django.shortcuts import render
|
||||||
|
from django import forms
|
||||||
|
|
||||||
|
from .context_helpers import bus_roll_context
|
||||||
|
from .email_helpers import render_to_pdf
|
||||||
|
|
||||||
|
|
||||||
|
class RollDateSelector(forms.Form):
|
||||||
|
_selected_action = forms.CharField(widget=forms.MultipleHiddenInput)
|
||||||
|
|
||||||
|
|
||||||
|
def roll_date_selector(mixin, request, queryset):
|
||||||
|
if 'generate' in request.POST:
|
||||||
|
date = request.POST.get("date")
|
||||||
|
if date:
|
||||||
|
date = datetime.strptime(date, '%Y-%m-%d')
|
||||||
|
else:
|
||||||
|
date = None
|
||||||
|
return render_to_pdf('reports/bus_roll.html', bus_roll_context(queryset, date=date))
|
||||||
|
form = RollDateSelector(initial={'_selected_action': queryset.values_list('id', flat=True)})
|
||||||
|
return render(request, 'admin/date_selector.html', context={'form': form})
|
||||||
@@ -216,16 +216,18 @@ class Traveller(models.Model):
|
|||||||
self._repopulate_address()
|
self._repopulate_address()
|
||||||
super(Traveller, self).save(*args, **kwargs)
|
super(Traveller, self).save(*args, **kwargs)
|
||||||
|
|
||||||
def _is_active(self):
|
def _is_active(self, date=None):
|
||||||
today = datetime.today()
|
if date is None:
|
||||||
today = datetime(today.year, today.month, today.day)
|
today = datetime.today()
|
||||||
if not self.travel_start_date or datetime(self.travel_start_date.year, self.travel_start_date.month, self.travel_start_date.day) > today:
|
date = datetime(today.year, today.month, today.day)
|
||||||
|
|
||||||
|
if not self.travel_start_date or datetime(self.travel_start_date.year, self.travel_start_date.month, self.travel_start_date.day) > date:
|
||||||
return False
|
return False
|
||||||
if not self.travel_end_date:
|
if not self.travel_end_date:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
end_date = datetime(self.travel_end_date.year, self.travel_end_date.month, self.travel_end_date.day)
|
end_date = datetime(self.travel_end_date.year, self.travel_end_date.month, self.travel_end_date.day)
|
||||||
return end_date >= today
|
return end_date >= date
|
||||||
|
|
||||||
def _update_active_status(self):
|
def _update_active_status(self):
|
||||||
new_start_date = None
|
new_start_date = None
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
{% extends "admin/base_site.html" %}
|
||||||
|
|
||||||
|
{% block content %}
|
||||||
|
<form action="" method="post">
|
||||||
|
{% csrf_token %}
|
||||||
|
{{ form }}
|
||||||
|
<input type="hidden" name="action" value="show_bus_roll_on_date">
|
||||||
|
<input type="date" name="date">
|
||||||
|
<input type="submit" name="generate" value="Generate">
|
||||||
|
</form>
|
||||||
|
{% endblock %}
|
||||||
Reference in New Issue
Block a user