diff --git a/activities/strength/strength_workout.py b/activities/strength/strength_workout.py index b0140eb..7c92c8e 100644 --- a/activities/strength/strength_workout.py +++ b/activities/strength/strength_workout.py @@ -52,6 +52,20 @@ def add_reps(user, excercise_id, reps): return s.workout.id +def start_timer(user, excercise_id): + excercise = models.Excercise.objects.get(pk=excercise_id, workout__user=user) + excercise.timers_set.create(time_started=django.utils.timezone.now()) + return excercise.workout.id + + +def stop_timer(user, excercise_id): + excercise = models.Excercise.objects.get(pk=excercise_id, workout__user=user) + timer = excercise.timers_set.latest('pk') + timer.time_finished = django.utils.timezone.now() + timer.save() + return excercise.workout.id + + def undo(user, workout_id): workout = models.Workout.objects.get(pk=workout_id, user=user) diff --git a/activities/strength/urls.py b/activities/strength/urls.py index 5628183..16f3428 100644 --- a/activities/strength/urls.py +++ b/activities/strength/urls.py @@ -8,5 +8,7 @@ url('^finish_workout/(?P[0-9]+)$', views.finish_workout, name='finish_workout'), url('^add_excercise/(?P[0-9]+)/$', views.add_excercise, name='add_excercise'), url('^add_reps/(?P[0-9]+)/$', views.add_reps, name='add_reps'), + url('^start_timer/(?P[0-9]+)$', views.start_timer, name='start_timer'), + url('^stop_timer/(?P[0-9]+)$', views.stop_timer, name='stop_timer'), url('^undo/(?P[0-9]+)$', views.undo, name='undo'), ] diff --git a/activities/strength/views.py b/activities/strength/views.py index e62a06d..a9c554f 100644 --- a/activities/strength/views.py +++ b/activities/strength/views.py @@ -22,6 +22,18 @@ def add_reps(request, excercise_id): return redirect('workout', id) +@login_required +def start_timer(request, excercise_id): + workout_id = strength_workout.start_timer(request.user, excercise_id) + return redirect('workout', workout_id) + + +@login_required +def stop_timer(request, excercise_id): + workout_id = strength_workout.stop_timer(request.user, excercise_id) + return redirect('workout', workout_id) + + @login_required def undo(request, workout_id): strength_workout.undo(request.user, workout_id) diff --git a/tests/test_client.py b/tests/test_client.py index 49e72ce..576b9a8 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -351,6 +351,40 @@ def test_add_some_excercises_and_reps(self): self.assertIsNotNone(workout.started) self.assertIsNotNone(workout.finished) + def test_timer_based_excercises(self): + self._start_workout() + + statistics = self._get_statistics_from_dashboard() + workout = statistics.previous_workouts()[0] + + self.post('/strength/add_excercise/{}/'.format(workout.id), {'name': 'plank front'}) + excercise = workout.excercise_set.latest('pk') + + first_timer_start = time(2016, 7, 30, 6, 22, 5) + + with patch('django.utils.timezone.now', autospec=True) as now: + now.return_value = first_timer_start + self.post('/strength/start_timer/{}'.format(excercise.id)) + + statistics = self._get_statistics_from_dashboard() + workout = statistics.previous_workouts()[0] + excercise = workout.excercise_set.get() + + self.assertEqual(1, len(excercise.timers_set.all())) + + first_timer = excercise.timers_set.all()[0] + self.assertEqual(first_timer_start, first_timer.time_started) + + first_timer_stop = time(2016, 7, 30, 6, 22, 6) + + with patch('django.utils.timezone.now', autospec=True) as now: + now.return_value = first_timer_stop + self.post('/strength/stop_timer/{}'.format(excercise.id)) + + first_timer = excercise.timers_set.all()[0] + + self.assertEqual(first_timer_stop, first_timer.time_finished) + def test_undo_last_rep(self): workout = self._start_workout()