Self hosted FLOSS fitness/workout, nutrition and weight tracker
Start working on a calendar day view
Roland Geider26a1532f6a7e
diff --git a/wger/core/api/serializers.py b/wger/core/api/serializers.pyindex 0d679b73e..b2a330ac2 100644--- a/wger/core/api/serializers.py+++ b/wger/core/api/serializers.py@@ -26,4 +26,10 @@ class UserprofileSerializer(serializers.ModelSerializer):'''class Meta:model = UserProfile- exclude = ('user',)+++class UsernameSerializer(serializers.Serializer):+ '''+ Serializer to extract the username+ '''+ username = serializers.CharField()diff --git a/wger/core/api/views.py b/wger/core/api/views.pyindex 85af2bcf0..0c7bac669 100644--- a/wger/core/api/views.py+++ b/wger/core/api/views.py@@ -17,11 +17,14 @@from django.contrib.auth.models import Userfrom rest_framework import viewsets+from rest_framework.response import Response+from rest_framework.decorators import linkfrom wger.core.models import UserProfilefrom wger.core.models import Languagefrom wger.core.models import DaysOfWeekfrom wger.core.models import License+from wger.core.api.serializers import UsernameSerializerfrom wger.core.api.serializers import UserprofileSerializerfrom wger.utils.permissions import UpdateOnlyPermissionfrom wger.utils.permissions import WgerPermission@@ -49,6 +52,15 @@ class UserProfileViewSet(viewsets.ModelViewSet):'''return [(User, 'user')]+ @link()+ def username(self, request, pk):+ '''+ Return the username+ '''++ user = self.get_object().user+ return Response(UsernameSerializer(user).data)+class LanguageViewSet(viewsets.ReadOnlyModelViewSet):'''diff --git a/wger/core/static/js/workout-manager.js b/wger/core/static/js/workout-manager.jsindex 1d0e3c728..a105bff85 100644--- a/wger/core/static/js/workout-manager.js+++ b/wger/core/static/js/workout-manager.js@@ -165,6 +165,30 @@ function set_profile_field(field, newValue) {});}++function get_username() {+ /*+ * Get the current user's username+ *+ * Do not use with anonymous users!+ *+ * Syncronous request, use sparingly!+ */++ var user_id = get_profile_field('user');+ var result;+ $.ajax({+ url:'/api/v2/userprofile/' + user_id + '/username/',+ type: 'GET',+ async: false,+ success: function(user) {+ result = user.username;+ }+ });+ return result;+}++function get_profile_field(field) {/** Get a single field from the user's profile@@ -174,13 +198,13 @@ function get_profile_field(field) {var result;$.ajax({- url:'/api/v2/userprofile/',- type: 'GET',- async: false,- success: function(userprofile) {- result = userprofile.results[0][field];- }- });+ url:'/api/v2/userprofile/',+ type: 'GET',+ async: false,+ success: function(userprofile) {+ result = userprofile.results[0][field];+ }+ });return result;}diff --git a/wger/manager/templates/workout/calendar.html b/wger/manager/templates/workout/calendar.htmlindex 619b1e0a6..f40faf220 100644--- a/wger/manager/templates/workout/calendar.html+++ b/wger/manager/templates/workout/calendar.html@@ -50,7 +50,10 @@ $(document).ready(function() {{% for date in logs %}<div id="log-{{date|date:'Y-m-d'}}" class="calendar-log-data">- <h3>{{date}}</h3>+ <h3>+ {{date}}+ <small><a href="{% url 'manager:workout:calendar-day' owner_user date.year date.month date.day %}">{% trans 'Detail page' %}</a></small>+ </h3>{% regroup logs|get_item:date by exercise as grouped_logs %}diff --git a/wger/manager/templates/workout/calendar_day.html b/wger/manager/templates/workout/calendar_day.htmlnew file mode 100644index 000000000..5e120959d--- /dev/null+++ b/wger/manager/templates/workout/calendar_day.html@@ -0,0 +1,120 @@+{% extends "base.html" %}+{% load i18n staticfiles wger_extras %}++{# #}+{# Opengraph #}+{# #}+{% block opengraph %}+ {{ block.super }}+ <meta property="og:title" content="{% trans 'Workout log' %}">++ {% with username=owner_user.username %}+ <meta property="og:description" content="{% blocktrans %}Workout log for {{username}} for {{date}}{% endblocktrans %}">+ {% endwith %}+{% endblock %}++{# #}+{# Title #}+{# #}+{% block title %}{% trans "Workout log" %} – {{date}}{% endblock %}+++{# #}+{# Content #}+{# #}+{% block content %}+{% if logs %}+ {% with first_log=logs|first %}+ {% with session=first_log.get_workout_session %}+ <div class="row">+ <div class="col-sm-4">+ <a href="{{first_log.workout.get_absolute_url}}" class="btn btn-default btn-block">{{ first_log.workout }}</a>+ </div>++ <div class="col-sm-4">+ <a href="{% url 'manager:log:log' first_log.workout.id %}" class="btn btn-default btn-block">{% trans "Log overview" %}</a>+ </div>++ {% if is_owner %}+ <div class="col-sm-4">+ {% if session %}+ <a href="{% url 'manager:session:edit' session.pk %}" class="btn btn-default btn-block wger-modal-dialog">{% trans "Edit workout session" %}</a>+ {% else %}+ <a href="{% url 'manager:session:add' first_log.workout_id date|date:'Y' date|date:'m' date|date:'d' %}" class="btn btn-default btn-block wger-modal-dialog">{% trans "Add workout impression" %}</a>+ {% endif %}+ </div>+ {% endif %}+ </div>+ {% if session %}+ <table class="table" style="margin-top: 2em;">+ <tr>+ <th style="width: 30%">{% trans "Time" %}</th>+ <td>+ {% if session.time_start %}+ {{session.time_start|time:"H:i"}} - {{session.time_end|time:"H:i"}}+ {% else %}+ -/-+ {% endif %}+ </td>+ </tr>+ <tr>+ <th>{% trans "General impression" %}</th>+ <td>{% trans session.get_impression_display %}</td>+ </tr>+ <tr>+ <th>{% trans "Notes" %}</th>+ <td>{{session.notes|default:'-/-'}}</td>+ </tr>+ </table>+ {% endif %}+ {% endwith %}+ {% endwith %}++++ {% regroup logs by exercise as grouped_logs %}+ {% for i in grouped_logs %}++ <table class="table table-hover table-bordered" style="margin-top: 2em;">+ <tr class="active">+ <th>{{ i.grouper }}</th>+ </tr>+ {% for log in i.list %}+ <tr>+ <td>+ {{log.reps}} × {{log.weight}} {% trans_weight_unit 'kg' owner_user %}++ {% if is_owner %}+ <span class="editoptions">+ <a href="{% url 'manager:log:delete' log.pk %}"+ title="{% trans 'Delete' %}"+ class="wger-modal-dialog">+ <img src="{% static 'images/icons/trash.svg' %}"+ width="16"+ height="16"+ alt="{% trans 'Delete' %}"></a>+ <a href="{% url 'manager:log:edit' log.pk %}"+ title="{% trans 'Edit' %}"+ class="wger-modal-dialog">+ <img src="{% static 'images/icons/edit.svg' %}"+ width="16"+ height="16"+ alt="{% trans 'Edit' %}"></a>+ </span>+ {% endif %}+ </td>+ </tr>+ {% endfor %}+ </table>+ {% endfor %}+{% else %}+ {% trans "Nothing found" %}+{% endif %}+{% endblock %}+++{# #}+{# Sidebar #}+{# #}+{% block sidebar %}+{% endblock %}diff --git a/wger/manager/templates/workout/timer.html b/wger/manager/templates/workout/timer.htmlindex 5275bfbc5..1802c463c 100644--- a/wger/manager/templates/workout/timer.html+++ b/wger/manager/templates/workout/timer.html@@ -239,6 +239,9 @@ $(document).ready(function() {// After clicking on the save button once, disable itfunction( data ) {+ var today = new Date();+ var date = '/' + today.getFullYear() + '/' + today.getMonth() + '/' + today.getDay();+ window.location = '/' + get_current_language() + '/workout/calendar/' + get_username() + date;$('#save_button_form').unbind("click");$('#save_button_form').addClass('disabled');$('#save_button_form').bind("click", function(e) {diff --git a/wger/manager/urls.py b/wger/manager/urls.pyindex 5298343c1..d57413446 100644--- a/wger/manager/urls.py+++ b/wger/manager/urls.py@@ -77,6 +77,9 @@ patterns_workout = patterns('',url(r'^calendar/(?P<year>\d{4})/(?P<month>\d{1,2})$',log.calendar,name='calendar'),+ url(r'^calendar/(?P<username>[\w.@+-]+)/(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<day>\d{1,2})$',+ log.day,+ name='calendar-day'),url(r'^(?P<pk>\d+)/ical/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})$',ical.export,name='ical'),@@ -208,4 +211,4 @@ urlpatterns = patterns('',url(r'^session/', include(patterns_session, namespace="session")),url(r'^schedule/', include(patterns_schedule, namespace="schedule")),url(r'^schedule/step/', include(patterns_step, namespace="step")),-)\ No newline at end of file+)diff --git a/wger/manager/views/log.py b/wger/manager/views/log.pyindex 90a628ae4..29d57335e 100644--- a/wger/manager/views/log.py+++ b/wger/manager/views/log.py@@ -385,7 +385,6 @@ def calendar(request, username=None, year=None, month=None):'''is_owner, user = check_access(request.user, username)- logger.info('aa bb cc')uid, token = make_token(user)year = int(year) if year else datetime.date.today().yearmonth = int(month) if month else datetime.date.today().month@@ -425,3 +424,26 @@ def calendar(request, username=None, year=None, month=None):context['month_list'] = WorkoutLog.objects.filter(user=user).dates('date', 'month')context['show_shariff'] = is_owner and user.userprofile.ro_accessreturn render(request, 'workout/calendar.html', context)+++def day(request, username, year, month, day):+ '''+ Show the logs for a single day+ '''+ is_owner, user = check_access(request.user, username)++ try:+ date = datetime.date(int(year), int(month), int(day))+ except ValueError as e:+ logger.error("Error on date: {0}".format(e))+ return HttpResponseForbidden()+ logs = WorkoutLog.objects.filter(user=user,+ date=date).order_by('id', 'exercise')+ context = {}+ context['logs'] = logs+ context['date'] = date+ context['owner_user'] = user+ context['is_owner'] = is_owner+ context['show_shariff'] = is_owner and user.userprofile.ro_access++ return render(request, 'workout/calendar_day.html', context)