From 0be25b7fed14e59ebb7ac52930a8284535eb4276 Mon Sep 17 00:00:00 2001 From: Kyle Blanker Date: Wed, 13 Sep 2017 14:04:47 -0400 Subject: Setup driver, company details to show loads, loads list now shows per week as well --- app/dispatch/migrations/0008_load_load_number.py | 21 +++++ app/dispatch/models.py | 1 + app/dispatch/templates/dispatch/base.html | 19 ++++- .../templates/dispatch/companies/detail.html | 57 ++++++++++++- .../templates/dispatch/companies/list.html | 2 +- .../templates/dispatch/drivers/detail.html | 63 ++++++++++++++ app/dispatch/templates/dispatch/drivers/edit.html | 15 ++++ app/dispatch/templates/dispatch/drivers/list.html | 34 ++++++++ app/dispatch/templates/dispatch/loads/list.html | 85 ++++++++++++------- app/dispatch/templatetags/__init__.py | 0 app/dispatch/templatetags/dynamic_key.py | 7 ++ app/dispatch/urls.py | 5 ++ app/dispatch/views.py | 99 ++++++++++++++++++++-- 13 files changed, 368 insertions(+), 40 deletions(-) create mode 100644 app/dispatch/migrations/0008_load_load_number.py create mode 100644 app/dispatch/templates/dispatch/drivers/detail.html create mode 100644 app/dispatch/templates/dispatch/drivers/edit.html create mode 100644 app/dispatch/templates/dispatch/drivers/list.html create mode 100644 app/dispatch/templatetags/__init__.py create mode 100644 app/dispatch/templatetags/dynamic_key.py (limited to 'app/dispatch') diff --git a/app/dispatch/migrations/0008_load_load_number.py b/app/dispatch/migrations/0008_load_load_number.py new file mode 100644 index 0000000..b671b87 --- /dev/null +++ b/app/dispatch/migrations/0008_load_load_number.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.5 on 2017-09-13 17:05 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('dispatch', '0007_auto_20170715_2105'), + ] + + operations = [ + migrations.AddField( + model_name='load', + name='load_number', + field=models.CharField(default="", max_length=64), + preserve_default=False, + ), + ] diff --git a/app/dispatch/models.py b/app/dispatch/models.py index f97c42b..f024d46 100644 --- a/app/dispatch/models.py +++ b/app/dispatch/models.py @@ -24,6 +24,7 @@ class Contact(models.Model): return "{f} {l} ( {c} )".format(c=self.works_for, f=self.first_name, l=self.last_name) class Load(models.Model): + load_number = models.CharField(max_length=64,default="") date = models.DateField() user = models.ForeignKey(settings.AUTH_USER_MODEL) company = models.ForeignKey(Company) diff --git a/app/dispatch/templates/dispatch/base.html b/app/dispatch/templates/dispatch/base.html index 37c8830..de28d7a 100644 --- a/app/dispatch/templates/dispatch/base.html +++ b/app/dispatch/templates/dispatch/base.html @@ -17,9 +17,16 @@ font-size: 2rem; margin: 0; } + h2 { + font-size: 1.75rem; + + } main { padding: 16px; } + .helptext{ + display: none; + } @@ -28,7 +35,7 @@ + +
+ + +
+ +{% load dynamic_key %} +{% for date in loads %} +
+
+
+ {{date}} + + + + + + + + + {% for load in loads|keyvalue:date %} + + + + + + {% empty %} + + {% endfor %} + +
DriverAmount
{{ load.user.first_name }} {{ load.user.last_name }}{{ load.amount }} + Edit + View +
No load.
+
+
+
+{% endfor %} + +
+ + +
+ {% endblock %} diff --git a/app/dispatch/templates/dispatch/companies/list.html b/app/dispatch/templates/dispatch/companies/list.html index 48612d9..9399276 100644 --- a/app/dispatch/templates/dispatch/companies/list.html +++ b/app/dispatch/templates/dispatch/companies/list.html @@ -10,7 +10,7 @@ Add Company - +
diff --git a/app/dispatch/templates/dispatch/drivers/detail.html b/app/dispatch/templates/dispatch/drivers/detail.html new file mode 100644 index 0000000..598d01e --- /dev/null +++ b/app/dispatch/templates/dispatch/drivers/detail.html @@ -0,0 +1,63 @@ +{% extends 'dispatch/base.html' %} + + +{% block content %} +
+
+

Driver: {{ object.first_name }} {{ object.last_name }}

+

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

+
+
+ +
+ + +
+ +{% load dynamic_key %} +{% for date in loads %} +
+
+
+ {{date}} +
Name
+ + + + + + + + {% for load in loads|keyvalue:date %} + + + + + + {% empty %} + + {% endfor %} + +
CompanyAmount
{{ load.company.name }}{{ load.amount }} + Edit + View +
No load.
+ + + +{% endfor %} + +
+ + +
+ +{% endblock %} diff --git a/app/dispatch/templates/dispatch/drivers/edit.html b/app/dispatch/templates/dispatch/drivers/edit.html new file mode 100644 index 0000000..fe8460d --- /dev/null +++ b/app/dispatch/templates/dispatch/drivers/edit.html @@ -0,0 +1,15 @@ +{% extends 'dispatch/base.html' %} + + +{% block content %} +
+
+

{{object.name}}

+
+
+ +
{% csrf_token %} + {{ form.as_p }} + +
+{% endblock %} diff --git a/app/dispatch/templates/dispatch/drivers/list.html b/app/dispatch/templates/dispatch/drivers/list.html new file mode 100644 index 0000000..efc6141 --- /dev/null +++ b/app/dispatch/templates/dispatch/drivers/list.html @@ -0,0 +1,34 @@ +{% extends 'dispatch/base.html' %} + + +{% block content %} +
+
+

Drivers

+
+
+ + + + + + + + + + {% for driver in object_list %} + + + + + + + {% empty %} + + {% endfor %} + +
First NameLast NameEmail
{{ driver.first_name }}{{ driver.last_name }}{{ driver.email }} + Edit + View +
No drivers yet.
+{% endblock %} diff --git a/app/dispatch/templates/dispatch/loads/list.html b/app/dispatch/templates/dispatch/loads/list.html index 8129907..d4caf71 100644 --- a/app/dispatch/templates/dispatch/loads/list.html +++ b/app/dispatch/templates/dispatch/loads/list.html @@ -2,42 +2,65 @@ {% block content %} +
-

Loads

+

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

- - - - {% if user.is_superuser %} - - {% endif %} - - - - - - - {% for load in object_list %} - - {% if user.is_superuser %} - - {% endif %} - - - - - - {% empty %} - - {% endfor %} - -
DriverDateCompanyAmount
{{ load.user.first_name }} {{ load.user.last_name }}{{ load.date }}{{ load.company.name }}{{ load.amount }} - Edit - View -
No loads yet.
+
+ + +
+{% load dynamic_key %} +{% for date in loads %} +
+
+
+ {{date}} + + + + + + + + + + + {% for load in loads|keyvalue:date %} + + + + + + + + {% empty %} + + {% endfor %} + +
DriverCompanyLoad NumberAmount
{{ load.user.first_name }} {{ load.user.last_name }}{{ load.company.name }}{{ load.load_number }}{{ load.amount }} + Edit + View +
No load.
+
+
+
+{% endfor %} +
+ + +
{% endblock %} diff --git a/app/dispatch/templatetags/__init__.py b/app/dispatch/templatetags/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/dispatch/templatetags/dynamic_key.py b/app/dispatch/templatetags/dynamic_key.py new file mode 100644 index 0000000..6a79707 --- /dev/null +++ b/app/dispatch/templatetags/dynamic_key.py @@ -0,0 +1,7 @@ +from django import template + +register = template.Library() + +@register.filter +def keyvalue(dict, key): + return dict[key] \ No newline at end of file diff --git a/app/dispatch/urls.py b/app/dispatch/urls.py index 3d33940..c895674 100644 --- a/app/dispatch/urls.py +++ b/app/dispatch/urls.py @@ -10,6 +10,11 @@ urlpatterns = [ url(r'^%s$' % settings.LOGIN_URL, authviews.LoginView.as_view(template_name="dispatch/login.html"), name='login'), url(r'^logout/$', authviews.LogoutView.as_view(next_page='/'), name='logout'), + url(r'^drivers/$', views.DriverList.as_view(), name='driver_list'), + url(r'^drivers/view/(?P\d+)$', views.DriverDetail.as_view(), name='driver_detail'), + url(r'^drivers/edit/(?P\d+)$', views.DriverUpdate.as_view(), name='driver_edit'), + url(r'^drivers/delete/(?P\d+)$', views.DriverDelete.as_view(), name='driver_delete'), + url(r'^companies/$', views.CompanyList.as_view(), name='company_list'), url(r'^companies/new$', views.CompanyCreate.as_view(), name='company_new'), url(r'^companies/view/(?P\d+)$', views.CompanyDetail.as_view(), name='company_detail'), diff --git a/app/dispatch/views.py b/app/dispatch/views.py index 8101ac0..f487ef7 100644 --- a/app/dispatch/views.py +++ b/app/dispatch/views.py @@ -10,10 +10,39 @@ from django.views.generic.detail import DetailView from django.views.generic.edit import CreateView, UpdateView, DeleteView from django.core.urlresolvers import reverse_lazy from dispatch.models import Company, Load +from django.contrib.auth.models import User +from django.contrib.auth.mixins import UserPassesTestMixin +from django.http import HttpResponseRedirect +from datetime import datetime, timedelta +from django.utils import formats +from dateutil import rrule def home(request): return render(request,"dispatch/index.html") +def get_week_dates(date=None): + 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 + 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 + +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): + print(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) + return split_loads class FilteredListView(ListView): def get_queryset(self): @@ -36,6 +65,54 @@ class FilteredDeleteView(DeleteView): return base_qs.filter(user=self.request.user) return base_qs + +# Driver CRUD + +class DriverList(UserPassesTestMixin, ListView): + template_name = "dispatch/drivers/list.html" + model = User + + def test_func(self): + return self.request.user.is_superuser + +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) + 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") + return context + +class DriverDetail(LoadDateSort): + template_name = "dispatch/drivers/detail.html" + model = User + + def test_func(self): + return self.request.user.is_superuser + +class DriverUpdate(UpdateView): + template_name = "dispatch/drivers/edit.html" + model = User + success_url = reverse_lazy('driver_list') + fields = ['username', 'first_name','last_name','email','groups', 'is_active'] + + def test_func(self): + return self.request.user.is_superuser + +class DriverDelete(DeleteView): + template_name = "dispatch/loads/delete.html" + model = User + success_url = reverse_lazy('driver_list') + + def test_func(self): + return self.request.user.is_superuser + +# Company CRUD + class CompanyList(ListView): template_name = "dispatch/companies/list.html" model = Company @@ -46,14 +123,10 @@ class CompanyCreate(CreateView): success_url = reverse_lazy('company_list') fields = ['name', 'address', 'phone_number','email_address'] -class CompanyDetail(DetailView): +class CompanyDetail(LoadDateSort): template_name = "dispatch/companies/detail.html" model = Company - def get_context_data(self, **kwargs): - context = super(CompanyDetail, self).get_context_data(**kwargs) - return context - class CompanyUpdate(UpdateView): template_name = "dispatch/loads/edit.html" model = Company @@ -71,6 +144,22 @@ class LoadList(FilteredListView): template_name = "dispatch/loads/list.html" model = Load + + 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)) + + 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) + return context + class LoadCreate(CreateView): template_name = "dispatch/loads/create.html" model = Load -- cgit v1.2.3