From a7c4944e54e9da5289f9749f4eba1ed177b17355 Mon Sep 17 00:00:00 2001 From: Mitch Riedstra Date: Mon, 9 Oct 2017 23:16:05 -0400 Subject: Fix up the shitty amount of date related variables being set in the template context. User friendly dates on load view --- README.md | 1 + .../templates/dispatch/drivers/detail.html | 10 ++--- app/dispatch/templates/dispatch/loads/list.html | 16 +++---- app/dispatch/views.py | 52 +++++++++++----------- 4 files changed, 41 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index 77b45d6..89d1d0a 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,7 @@ Requirements going forward: * Show Recent Changes on Loads ( Django auditlog ) * Sane Date format on Loads listing for each week * /loads + * Add the description for each load to the individual load * Sane date format for each individual day block * Sane date format for the title * Sane title format diff --git a/app/dispatch/templates/dispatch/drivers/detail.html b/app/dispatch/templates/dispatch/drivers/detail.html index bc674b9..80779c4 100644 --- a/app/dispatch/templates/dispatch/drivers/detail.html +++ b/app/dispatch/templates/dispatch/drivers/detail.html @@ -6,16 +6,16 @@

Driver Summary for {{ object.first_name }} {{ object.last_name }}

-

Loads for {{start_date|date:"l, F, d"}} to {{end_date|date:"l, F, d"}}

+

Loads for {{week_dates.start_date|date:"l, F, d"}} to {{week_dates.end_date|date:"l, F, d"}}

@@ -54,10 +54,10 @@
diff --git a/app/dispatch/templates/dispatch/loads/list.html b/app/dispatch/templates/dispatch/loads/list.html index 6141e75..b6f7008 100644 --- a/app/dispatch/templates/dispatch/loads/list.html +++ b/app/dispatch/templates/dispatch/loads/list.html @@ -6,19 +6,19 @@ {% block content %}
-
-

Loads for {{start_date}} - {{end_date}}

+
+

Loads for {{week_dates.start_date|date:"l, F, d"}} to {{week_dates.end_date|date:"l, F, d"}}

- {% load dynamic_key %} @@ -26,7 +26,7 @@
- {{date}} + {{date|date:"l F d"}} @@ -59,10 +59,10 @@ {% endfor %} {% endblock %} diff --git a/app/dispatch/views.py b/app/dispatch/views.py index 9dd60af..6ce552b 100644 --- a/app/dispatch/views.py +++ b/app/dispatch/views.py @@ -21,40 +21,44 @@ def home(request): return redirect(reverse('load_list')) def get_week_dates(date=None): + week_dates = {} if date == None: date = formats.date_format(datetime.now(), "SHORT_DATE_FORMAT") dt = datetime.strptime(date, '%m/%d/%Y') weekday = dt.weekday() if weekday == 6: - start_date = dt + week_dates['start_date'] = dt else: weekday = weekday + 1 - start_date = dt - timedelta(days=weekday) - end_date = start_date + timedelta(days=6) - next_week = end_date + timedelta(days=1) - previous_week = start_date - timedelta(days=1) - return start_date, end_date, next_week, previous_week + week_dates['start_date'] = dt - timedelta(days=weekday) + week_dates['end_date'] = week_dates['start_date'] + timedelta(days=6) + week_dates['next_week'] = week_dates['end_date'] + timedelta(days=1) + week_dates['previous_week'] = week_dates['start_date'] - timedelta(days=1) + return week_dates def split_loads_by_day(loads,start_date,end_date): split_loads = {} + for date in rrule.rrule(rrule.DAILY,dtstart=start_date, until=end_date): str_date = formats.date_format(date, "SHORT_DATE_FORMAT") - if str_date not in split_loads: - split_loads[str_date] = loads.filter(date=date) + if date not in split_loads: + split_loads[date] = loads.filter(date=date) + return split_loads class LoadDateSort(DetailView): def get_context_data(self, **kwargs): context = super(DetailView, self).get_context_data(**kwargs) - start_date, end_date, next_week, previous_week = get_week_dates(self.request.GET.get('date',None)) - loads = self.get_object().load_set.filter(date__range=(start_date, end_date)).prefetch_related('company') - context['loads'] = split_loads_by_day(loads,start_date,end_date) - - # Holy fucking shit this wasn't really necessary - context['start_date'] = start_date - context['end_date'] = end_date - context['next_week'] = next_week - context['previous_week'] = previous_week + week_dates = get_week_dates(self.request.GET.get('date',None)) + + loads = self.get_object().load_set.filter(date__range=(week_dates['start_date'], + week_dates['end_date'])).prefetch_related('company') + + context['loads'] = split_loads_by_day(loads,week_dates['start_date'], + week_dates['end_date']) + + context['week_dates'] = week_dates + return context class FilteredListView(ListView): @@ -142,17 +146,15 @@ class LoadList(FilteredListView): def get_queryset(self): base_qs = super(LoadList, self).get_queryset() - start_date, end_date, next_week, previous_week = get_week_dates(self.request.GET.get('date',None)) - return base_qs.filter(date__range=(start_date, end_date)) + wd = get_week_dates(self.request.GET.get('date',None)) + return base_qs.filter(date__range=(wd['start_date'], wd['end_date'])) def get_context_data(self, **kwargs): context = super(LoadList, self).get_context_data(**kwargs) - start_date, end_date, next_week, previous_week = get_week_dates(self.request.GET.get('date',None)) - context['start_date'] = formats.date_format(start_date, "SHORT_DATE_FORMAT") - context['end_date'] = formats.date_format(end_date, "SHORT_DATE_FORMAT") - context['next_week'] = formats.date_format(next_week, "SHORT_DATE_FORMAT") - context['previous_week'] = formats.date_format(previous_week, "SHORT_DATE_FORMAT") - context['loads'] = split_loads_by_day(self.object_list,start_date,end_date) + wd = get_week_dates(self.request.GET.get('date',None)) + context['week_dates'] = wd + + context['loads'] = split_loads_by_day(self.object_list,wd['start_date'],wd['end_date']) return context class LoadCreate(CreateView): -- cgit v1.2.3