diff --git a/core/models.py b/core/models.py index 0c67ea07..408120ff 100644 --- a/core/models.py +++ b/core/models.py @@ -156,11 +156,12 @@ class Timer(models.Model): def __str__(self): return self.name or 'Timer #{}'.format(self.id) - def current_duration(self): - if self.duration: - return self.duration - else: - return timezone.now() - self.start + @classmethod + def from_db(cls, db, field_names, values): + instance = super(Timer, cls).from_db(db, field_names, values) + if not instance.duration: + instance.duration = timezone.now() - instance.start + return instance def restart(self): """Restart the timer.""" diff --git a/core/templates/core/timer_detail.html b/core/templates/core/timer_detail.html index 14e79a17..671a7b6f 100644 --- a/core/templates/core/timer_detail.html +++ b/core/templates/core/timer_detail.html @@ -12,9 +12,9 @@
Started {{ object.start }} @@ -75,7 +75,7 @@ {% block javascript %} {% if object.active %} {% endif %} {% endblock %} \ No newline at end of file diff --git a/core/tests/tests_models.py b/core/tests/tests_models.py index 710141a0..061df5c9 100644 --- a/core/tests/tests_models.py +++ b/core/tests/tests_models.py @@ -152,11 +152,11 @@ class TimerTestCase(TestCase): timer.save() self.assertEqual( - timer.current_duration().seconds, + timer.duration().seconds, timezone.timedelta(minutes=30).seconds) timer.stop() self.assertEqual( - timer.current_duration().seconds, + timer.duration().seconds, timezone.timedelta(minutes=30).seconds)