Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Add first version of calendar view showing all days with workout logs

Roland Geider26e41f180af1
diff --git a/wger/manager/static/css/workout-manager-common.css b/wger/manager/static/css/workout-manager-common.css
index 8a8f1f4dc..b4cf8c38f 100644
--- a/wger/manager/static/css/workout-manager-common.css
+++ b/wger/manager/static/css/workout-manager-common.css
@@ -303,3 +303,57 @@ body .boxInner:hover .titleBox, body.touch .boxInner.touchFocus .titleBox {
padding-bottom: 33.3%;
}
}
+
+/**************************************************************
+ * Weight log calendar
+ **************************************************************/
+ th.month, th.month:hover{
+ text-align: center;
+ background-color: #d3d7cf;
+ border-bottom: 2px solid black;
+}
+td.filled, td.filled:hover{
+ background-color: #ad7fa8;
+}
+
+td.filled a:hover{
+ /*background-color: inherit;*/
+}
+
+table.month tbody tr:hover td.filled{
+ background-color: #ad7fa8;
+}
+table.month td.filled a{
+ display: block;
+ height:100%;
+ width:100%;
+ color: black;
+}
+table.month td a{
+ color: black;
+ text-shadow: none;
+ text-decoration: none;
+}
+table.month td{
+ padding: 0px;
+ margin-top: 10px;
+ margin-bottom: 10px;
+ margin-left:7px;
+ margin-right:7px;
+}
+table.month th{
+ text-align: center;
+}
+table.month td {
+ height: 4em;
+ width: 14.2857%;
+ text-align: center;
+ vertical-align: middle;
+}
+
+td.today, table.month tbody tr:hover td.today{
+ font-weight: bold;
+}
+.calendar-log-data{
+ display:none;
+}
\ No newline at end of file
diff --git a/wger/manager/templates/mobile/navigation.html b/wger/manager/templates/mobile/navigation.html
index e45ca7694..173451985 100644
--- a/wger/manager/templates/mobile/navigation.html
+++ b/wger/manager/templates/mobile/navigation.html
@@ -43,6 +43,7 @@
<h3>{% trans "Workouts" %}</h3>
<ul data-role="listview">
<li><a href="{% url 'wger.manager.views.workout.overview' %}" rel="nofollow">{% trans "Workouts" %}</a></li>
+ <li><a href="{% url 'workout-calendar' %}" rel="nofollow">{% trans "Calendar" %}</a></li>
<li><a href="{% url 'schedule-overview' %}" rel="nofollow">{% trans "Workouts schedules" %}</a></li>
</ul>
</div>
diff --git a/wger/manager/templates/navigation.html b/wger/manager/templates/navigation.html
index 8eaa58b29..cc482b27b 100644
--- a/wger/manager/templates/navigation.html
+++ b/wger/manager/templates/navigation.html
@@ -27,6 +27,7 @@
data-toggle="dropdown">{% trans "Workouts" %} <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="{% url 'wger.manager.views.workout.overview' %}" rel="nofollow">{% trans "Workouts" %}</a></li>
+ <li><a href="{% url 'workout-calendar' %}" rel="nofollow">{% trans "Calendar" %}</a></li>
<li><a href="{% url 'schedule-overview' %}" rel="nofollow">{% trans "Workouts schedules" %}</a></li>
</ul>
</li>
diff --git a/wger/manager/templates/workout/calendar.html b/wger/manager/templates/workout/calendar.html
new file mode 100644
index 000000000..b75f993c7
--- /dev/null
+++ b/wger/manager/templates/workout/calendar.html
@@ -0,0 +1,79 @@
+{% extends "base.html" %}
+{% load url from future %}
+{% load i18n %}
+{% load staticfiles %}
+{% load wger_extras %}
+
+{% block title %}{% trans "Workout log calendar" %}{% endblock %}
+
+{% block header %}
+<script>
+$(document).ready(function() {
+ $('.filled a').click(function(e){
+ e.preventDefault();
+ $('.calendar-log-data').hide();
+ $('#' + $(this).data('log')).toggle();
+ });
+ });
+</script>
+{% endblock %}
+
+{% block content %}
+{{calendar|safe}}
+
+{% for date in logs %}
+<div id="log-{{date|date:'Y-m-d'}}" class="calendar-log-data">
+ <h3>{{date}}</h3>
+
+ {% regroup logs|get_item:date by exercise as grouped_logs %}
+
+ {# TODO: what if some of the entries don't belong to a workout? #}
+ {% with tmp=grouped_logs|first %}
+ {% with first_log=tmp.list|first %}
+ <p>
+ Go to:
+ <a href="{{first_log.workout.get_absolute_url}}">workout</a> |
+ <a href="{% url 'workout-log' first_log.workout.id %}">log overview</a>
+ </p>
+ {% endwith %}
+ {% endwith %}
+ <ul>
+ {% for i in grouped_logs %}
+ <li>{{i.grouper}}
+ <ul>
+ {% for log in i.list %}
+ <li>{{log.reps}} × {{log.weight}}</li>
+ {% endfor %}
+ </ul>
+ </li>
+ {% endfor %}
+ </ul>
+
+</div>
+{% endfor %}
+{% endblock %}
+
+
+
+{% block sidebar %}
+ <h3>{% trans 'Info' %}</h3>
+ <p>{% blocktrans %}The coloured days in the calendar have weight logs,
+you can click on them to see the details. Click on an empty day to
+add a log.{% endblocktrans %}</p>
+
+<h3>{% trans 'Log' %}</h3>
+<p><a href="{% url 'workout-calendar' %}">{% trans "Go to today" %}</a></p>
+<div>
+{% for date in month_list %}
+ {% ifchanged date.year %}
+ <p><strong>{{ date|date:"Y" }}</strong></p>
+ {% endifchanged %}
+ {% if current_month = date.month and current_year = date.year %}
+ {{date|date:"F"}}<br>
+ {% else %}
+ <a href="{% url 'workout-calendar' date.year date.month %}">{{date|date:"F"}}</a><br>
+ {% endif %}
+{% endfor %}
+</div>
+
+{% endblock %}
\ No newline at end of file
diff --git a/wger/manager/urls.py b/wger/manager/urls.py
index 6105f86b4..8dbefb7b3 100644
--- a/wger/manager/urls.py
+++ b/wger/manager/urls.py
@@ -69,6 +69,12 @@ urlpatterns = patterns('wger.manager.views',
url(r'^workout/log/edit-entry/(?P<pk>\d+)$',
WorkoutLogUpdateView.as_view(),
name='workout-log-edit'),
+ url(r'^workout/calendar$',
+ 'log.calendar',
+ name='workout-calendar'),
+ url(r'^workout/calendar/(?P<year>\d{4})-(?P<month>\d{1,2})$',
+ 'log.calendar',
+ name='workout-calendar'),
# Schedules
url(r'^workout/schedule/overview$',
diff --git a/wger/manager/views/log.py b/wger/manager/views/log.py
index e435b0d32..9c9164b21 100644
--- a/wger/manager/views/log.py
+++ b/wger/manager/views/log.py
@@ -17,6 +17,9 @@
import logging
import uuid
import datetime
+from calendar import HTMLCalendar
+#from datetime import date
+from itertools import groupby
from django.template import RequestContext
from django.shortcuts import render_to_response
@@ -229,3 +232,88 @@ class WorkoutLogDetailView(DetailView, WgerPermissionMixin):
# Dispatch normally
return super(WorkoutLogDetailView, self).dispatch(request, *args, **kwargs)
+
+
+class WorkoutCalendar(HTMLCalendar):
+ '''
+ A calendar renderer, see this blog entry for details:
+ * http://uggedal.com/journal/creating-a-flexible-monthly-calendar-in-django/
+ '''
+ def __init__(self, workout_logs):
+ super(WorkoutCalendar, self).__init__()
+ self.workout_logs = self.group_by_day(workout_logs)
+
+ def formatday(self, day, weekday):
+ if day != 0:
+ cssclass = self.cssclasses[weekday]
+ date_obj = datetime.date(self.year, self.month, day)
+ if datetime.date.today() == date_obj:
+ cssclass += ' today'
+ if day in self.workout_logs:
+ cssclass += ' filled'
+ body = []
+
+ for log in self.workout_logs[day]:
+ url = reverse('workout-log', kwargs={'pk': log.workout.pk})
+ formatted_date = date_obj.strftime('%Y-%m-%d')
+ body.append('<a href="{0}" data-log="log-{1}">'.format(url, formatted_date))
+ body.append(unicode(day))
+ body.append('</a>')
+ return self.day_cell(cssclass, '{0}'.format(''.join(body)))
+ return self.day_cell(cssclass, day)
+ return self.day_cell('noday', '&nbsp;')
+
+ def formatmonth(self, year, month):
+ self.year, self.month = year, month
+ return super(WorkoutCalendar, self).formatmonth(year, month)
+
+ def group_by_day(self, workout_logs):
+ '''
+ Helper function that
+ '''
+ field = lambda log: log.date.day
+ result = dict(
+ [(day, list(items)) for day, items in groupby(workout_logs, field)]
+ )
+
+ return result
+
+ def day_cell(self, cssclass, body):
+ '''
+ Renders a day cell
+ '''
+ return '<td class="{0}">{1}</td>'.format(cssclass, body)
+
+
+def calendar(request, year=None, month=None):
+ '''
+ Show a calendar with all the workout logs
+ '''
+ if not year:
+ year = datetime.date.today().year
+ else:
+ year = int(year)
+ if not month:
+ month = datetime.date.today().month
+ else:
+ month = int(month)
+ context = {}
+ logs = WorkoutLog.objects.filter(user=request.user,
+ date__year=year,
+ date__month=month).order_by('exercise')
+ logs_filtered = []
+ temp = []
+
+ for log in logs:
+ if not log.date in temp:
+ temp.append(log.date)
+ logs_filtered.append(log)
+
+ context['calendar'] = WorkoutCalendar(logs_filtered).formatmonth(year, month)
+ context['logs'] = WorkoutLog().process_log_entries(logs)[0]
+ context['current_year'] = year
+ context['current_month'] = month
+ context['month_list'] = WorkoutLog.objects.filter(user=request.user).dates('date', 'month')
+ return render_to_response('workout/calendar.html',
+ context,
+ context_instance=RequestContext(request))
diff --git a/wger/software/templates/changelog.html b/wger/software/templates/changelog.html
index cc8c7dae3..fa3c3807a 100644
--- a/wger/software/templates/changelog.html
+++ b/wger/software/templates/changelog.html
@@ -7,6 +7,14 @@
{% block title %}{% trans "Changelog" %}{% endblock %}
{% block content %}
+<h3>1.4</h3>
+<h4>2013-XX-XX</h4>
+
+<ul>
+ <li>Calendar view to more easily check workout logs</li>
+</ul>
+
+
<h3>1.3</h3>
<h4>2013-XX-XX</h4>