From 32fac6e442571606bcf55dd6391dd0f25e4b54cb Mon Sep 17 00:00:00 2001 From: James Turk Date: Fri, 3 Apr 2015 16:46:59 -0400 Subject: [PATCH] initial month view --- lifting/views.py | 32 +++++++++++++++++++++++++++++++- templates/base.html | 1 + templates/month.html | 42 ++++++++++++++++++++++++++++++++++++++++++ web/urls.py | 2 ++ 4 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 templates/month.html diff --git a/lifting/views.py b/lifting/views.py index 5ad630a..b36a9fb 100644 --- a/lifting/views.py +++ b/lifting/views.py @@ -1,11 +1,41 @@ +import datetime +import os import tempfile +import calendar +from collections import defaultdict from django.shortcuts import render from django import forms from django.contrib.auth.decorators import login_required -import os +from django.views.generic import dates from . import importers +from .models import Set + + +@login_required +def month(request, year, month): + year, month = int(year), int(month) + + sets_by_day = defaultdict(list) + for workset in Set.objects.filter(user=request.user, date__year=year, date__month=month): + sets_by_day[workset.date.day].append(workset) + date = datetime.date(year, month, 1) + first_day, max_days = calendar.monthrange(year, month) + # make first_day use 0 for sunday + first_day = (first_day + 1) % 7 + + # start calendar with a few blank days + days = [None]*first_day + + for day in range(max_days+1): + days.append({'number': day, 'sets': sets_by_day[day]}) + + days_by_week = [days[0:7], days[7:14], days[14:21], days[21:28], days[28:35] + + return render(request, 'month.html', { 'date': date, + 'days': days_by_week + }) class FitnotesUploadForm(forms.Form): diff --git a/templates/base.html b/templates/base.html index 27e43b3..4945ad5 100644 --- a/templates/base.html +++ b/templates/base.html @@ -11,6 +11,7 @@ +
diff --git a/templates/month.html b/templates/month.html new file mode 100644 index 0000000..c7dfbbf --- /dev/null +++ b/templates/month.html @@ -0,0 +1,42 @@ +{% extends "base.html" %} +{% block content %} + +
+
+

{{date|date:"F Y"}}

+
+
+ +
+
+ + + + + + + + + + + + + + {% for week in days %} + + {% for day in week %} + + {% endfor %} + + {% endfor %} + +
SundayMondayTuesdayWednesdayThursdayFridaySaturday
+ {% if day %} + {{day.number}} + {{day.sets|length}} + {% endif %} +
+
+
+ +{% endblock %} diff --git a/web/urls.py b/web/urls.py index 9d92288..fafe7ee 100644 --- a/web/urls.py +++ b/web/urls.py @@ -7,4 +7,6 @@ urlpatterns = [ url(r'^admin/', include(admin.site.urls)), url(r'^fitnotes-upload/$', 'lifting.views.fitnotes_upload'), + url(r'^lifting/(?P\d{4})/(?P\d{1,2})/$', 'lifting.views.month'), + ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)