Self hosted FLOSS fitness/workout, nutrition and weight tracker
Move logic for determining the timer steps from template to view
Roland Geider975fb7a26cef
diff --git a/wger/manager/templates/workout/timer.html b/wger/manager/templates/workout/timer.htmlindex 7e3dfb19c..296111070 100644--- a/wger/manager/templates/workout/timer.html+++ b/wger/manager/templates/workout/timer.html@@ -14,7 +14,7 @@padding:0;margin:0;}-ul li {+ul.workout-timer li {float:left;list-style:none;position:relative;@@ -76,10 +76,10 @@ function draw_step(step) { // step between 0 and 1}}-function start_countdown(set_id, seconds)+function start_countdown(step_id, seconds){- canvas = document.getElementById('canvas-'+set_id);- num = $('#counter-'+set_id);+ canvas = document.getElementById('canvas-div-'+step_id);+ num = $('#counter-'+step_id);ctx = canvas.getContext('2d');canvas_size = [canvas.width, canvas.height];radius = Math.min(canvas_size[0], canvas_size[1]) / 2;@@ -134,7 +134,8 @@ $(document).ready(function() {});$('#container ul li').each(function(intIndex) {$(this).find('a').bind("click", function() {- var set_id = $(this).parents('li').data('set');+ var step_id = $(this).parents('li').data('step');+ var step_time = $(this).parents('li').data('time');if ($(this).is(".next")) {$(this).parents('ul').animate({"margin-left": (-(intIndex + 1) * div_width)@@ -145,7 +146,7 @@ $(document).ready(function() {"margin-left": (-(intIndex - 1) * div_width)}, 600)} else if ($(this).is(".start")) {- start_countdown(set_id, 90);+ start_countdown(step_id, step_time);} else if ($(this).is(".startover")) {$(this).parent('li').parent('ul').animate({"margin-left": (0)@@ -173,54 +174,56 @@ $(document).ready(function() {data-icon="arrow-r"data-iconpos="right">{% trans "Start workout" %}</a></li>-{% for set in day.set_set.select_related %}-- {% for exercise in set.exercises.select_related %}- {% for setting in exercise|get_current_settings:set.id %}- <li class="timer-step" id="div-exercise-{{set.pk}}" data-set="{{set.pk}}">- <h2>{{exercise}}</h2>- <div class="filler_div">- <div data-role="fieldcontain">- <label for="reps-field">{% trans "Repetitions" %}:</label>- <input name="reps-field" id="reps-field" placeholder="{% trans 'Repetitions' %}" value="" type="text">- </div>- <div data-role="fieldcontain">- <label for="weight-field">{% trans "Weight" %}:</label>- <input name="weight-field" id="weight-field" placeholder="{% trans 'Weight' %}" value="" type="text">- </div>+{% for step in step_list %}+ {% if step.type == 'exercise' %}+ <li class="timer-step" id="div-{{step.current_step}}" data-step="{{step.current_step}}">+ <h2>{{step.exercise}}</h2>+ <div class="filler_div">+ <div data-role="fieldcontain">+ <label for="reps-field-{{step.current_step}}">{% trans "Repetitions" %}:</label>+ <input name="reps-field" id="reps-field-{{step.current_step}}" placeholder="{% trans 'Repetitions' %}" value="{{step.reps}}" type="text"></div>-- <div class="sub_header">{{set.sets}} x {{setting.reps}}</div>- <a class="next bottom_button"- href="#"- data-role="button"- data-icon="arrow-r"- data-iconpos="right">{% trans "Save or skip" %}</a>- </li>- <li class="timer-step" id="div-pause-{{set.pk}}" data-set="{{set.pk}}">- <h2>{% trans "Pause!" %}</h2>-- <canvas id="canvas-{{set.pk}}"></canvas>-- <div class="ui-grid-a">- <div class="ui-block-a">- <div class="sub_header"><span id="counter-{{set.pk}}">90</span> {% trans "sec" %}</div>- </div>- <div class="ui-block-b">- <a class="start" href="#" data-role="button">{% trans "start" %}</a>- </div>+ <div data-role="fieldcontain">+ <label for="weight-field-{{step.current_step}}">{% trans "Weight" %}:</label>+ <input name="weight-field" id="weight-field-{{step.current_step}}" placeholder="{% trans 'Weight' %}" value="{{step.weight}}" type="text"></div>--- <a class="next bottom_button"- href="#"- data-role="button"- data-icon="arrow-r"- data-iconpos="right">{% trans "skip" %}</a>- </li>- {% endfor %}- {% endfor %}-+ </div>++ <div class="sub_header">+ {% if step.weight %}+ {{step.reps}} × {{step.weight}} kg.+ {% else %}+ {{step.reps}} {% trans "reps" %}+ {% endif %}+ </div>+ <a class="next bottom_button"+ href="#"+ data-role="button"+ data-icon="arrow-r"+ data-iconpos="right">{% trans "Save or skip" %}</a>+ </li>+ {% else %}+ <li class="timer-step" id="div-{{step.current_step}}" data-step="{{step.current_step}}" data-time="{{step.time}}">+ <h2>{% trans "Pause!" %}</h2>++ <canvas id="canvas-div-{{step.current_step}}"></canvas>++ <div class="ui-grid-a">+ <div class="ui-block-a">+ <div class="sub_header"><span id="counter-{{step.current_step}}">{{step.time}}</span> {% trans "sec" %}</div>+ </div>+ <div class="ui-block-b">+ <a class="start" href="#" data-role="button">{% trans "start" %}</a>+ </div>+ </div>++ <a class="next bottom_button"+ href="#"+ data-role="button"+ data-icon="arrow-r"+ data-iconpos="right">{% trans "skip" %}</a>+ </li>+ {% endif %}{% endfor %}<li class="timer-step" id="div-{{set.pk}}"><h2>{% trans "Workout complete!" %}</h2>diff --git a/wger/manager/views/workout.py b/wger/manager/views/workout.pyindex 471f64d21..bd6481610 100644--- a/wger/manager/views/workout.py+++ b/wger/manager/views/workout.py@@ -15,6 +15,7 @@# You should have received a copy of the GNU Affero General Public Licenseimport logging+import uuidfrom django.template import RequestContextfrom django.shortcuts import render_to_response@@ -33,8 +34,10 @@ from django.views.generic import DeleteViewfrom django.views.generic import UpdateViewfrom wger.manager.models import Workout+from wger.manager.models import WorkoutLogfrom wger.manager.models import Schedulefrom wger.manager.models import Day+from wger.manager.models import Settingfrom wger.manager.forms import WorkoutFormfrom wger.manager.forms import WorkoutCopyForm@@ -241,7 +244,37 @@ def timer(request, pk):day = Day.objects.get(pk=pk, training__user=request.user)context['day'] = day-+ step_list = []++ for set in day.set_set.select_related():++ for exercise in set.exercises.select_related():+ for setting in Setting.objects.filter(set=set, exercise=exercise).order_by('order'):++ # To find out the last weight, look if the user has already+ # a log with same exercise and repetitions and use the last one.+ last_log = WorkoutLog.objects.filter(user=request.user,+ exercise=exercise,+ reps=setting.reps).order_by('-date')+ if last_log.exists():+ weight = last_log[0].weight+ else:+ weight = None++ # Exercise+ step_list.append({'current_step': uuid.uuid4().hex,+ 'exercise': exercise,+ 'type': 'exercise',+ 'weight': weight,+ 'reps': setting.reps})+ # Pause+ step_list.append({'current_step': uuid.uuid4().hex,+ 'type': 'pause',+ 'time': 30})++ context['step_list'] = step_list+ import pprint+ pprint.pprint(step_list)return render_to_response('workout/timer.html',context,context_instance=RequestContext(request))