From e14bb93cf92a973a6254a6dbbf311c9b60d48ceb Mon Sep 17 00:00:00 2001 From: Piotr Podusowski Date: Wed, 14 Jun 2017 09:16:54 +0200 Subject: [PATCH] #20 replace time finished with duration to simplify stats calculation --- activities/strength/strength_workout.py | 2 +- tests/test_strength.py | 7 ++++-- .../migrations/0039_auto_20170614_0711.py | 24 +++++++++++++++++++ training/models.py | 2 +- 4 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 training/migrations/0039_auto_20170614_0711.py diff --git a/activities/strength/strength_workout.py b/activities/strength/strength_workout.py index 73540c5..05a9ae9 100644 --- a/activities/strength/strength_workout.py +++ b/activities/strength/strength_workout.py @@ -62,7 +62,7 @@ def start_timer(user, excercise_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.duration = django.utils.timezone.now() - timer.time_started timer.save() return excercise.workout.id diff --git a/tests/test_strength.py b/tests/test_strength.py index 9a43f07..9e92e0a 100644 --- a/tests/test_strength.py +++ b/tests/test_strength.py @@ -1,4 +1,5 @@ from unittest.mock import patch +from datetime import timedelta from tests.utils import time, ClientTestCase from tests import utils @@ -77,6 +78,8 @@ def test_add_some_excercises_and_reps(self): THREE_O_CLOCK = time(2016, 1, 1, 15, 0, 0) FOUR_O_CLOCK = time(2016, 1, 1, 16, 0, 0) + ONE_HOUR = timedelta(hours=1) + def _timer_rep(self, excercise_id, time_start, time_finished): with patch('django.utils.timezone.now', autospec=True) as now: now.return_value = time_start @@ -101,7 +104,7 @@ def test_timer_based_excercise(self): first_timer = excercise.timers_set.all()[0] self.assertEqual(self.ONE_O_CLOCK, first_timer.time_started) - self.assertEqual(self.TWO_O_CLOCK, first_timer.time_finished) + self.assertEqual(self.ONE_HOUR, first_timer.duration) statistics = self._get_statistics_from_dashboard() excercises = statistics.most_popular_workouts() @@ -128,7 +131,7 @@ def test_timer_based_excercise_with_two_reps(self): second_timer = excercise.timers_set.all()[1] self.assertEqual(self.THREE_O_CLOCK, second_timer.time_started) - self.assertEqual(self.FOUR_O_CLOCK, second_timer.time_finished) + self.assertEqual(self.ONE_HOUR, second_timer.duration) def test_undo_last_rep(self): workout = self._start_workout() diff --git a/training/migrations/0039_auto_20170614_0711.py b/training/migrations/0039_auto_20170614_0711.py new file mode 100644 index 0000000..af7efa6 --- /dev/null +++ b/training/migrations/0039_auto_20170614_0711.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10 on 2017-06-14 07:11 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('training', '0038_auto_20170528_1931'), + ] + + operations = [ + migrations.RemoveField( + model_name='timers', + name='time_finished', + ), + migrations.AddField( + model_name='timers', + name='duration', + field=models.DurationField(default=None, null=True), + ), + ] diff --git a/training/models.py b/training/models.py index 984836c..dfec6af 100644 --- a/training/models.py +++ b/training/models.py @@ -105,7 +105,7 @@ class Timers(models.Model): ''' excercise = models.ForeignKey(Excercise) time_started = models.DateTimeField(null=True, default=None) - time_finished = models.DateTimeField(null=True, default=None) + duration = models.DurationField(null=True, default=None) class Meta: ordering = ['pk']