mirror of https://github.com/snachodog/mybuddy.git
Use a from_db class method instead of a separate method to always provider a timer's current duration.
This commit is contained in:
parent
c305d8038b
commit
6e936df1bc
|
@ -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."""
|
||||
|
|
|
@ -12,9 +12,9 @@
|
|||
<div class="jumbotron text-center{% if not object.active %} text-danger{% endif %}">
|
||||
|
||||
<div class="h1" id="timer-status">
|
||||
<span class="timer-hours">{{ object.current_duration|hours }}</span>h
|
||||
<span class="timer-minutes">{{ object.current_duration|minutes }}</span>m
|
||||
<span class="timer-seconds">{{ object.current_duration|seconds }}</span>s
|
||||
<span class="timer-hours">{{ object.duration|hours }}</span>h
|
||||
<span class="timer-minutes">{{ object.duration|minutes }}</span>m
|
||||
<span class="timer-seconds">{{ object.duration|seconds }}</span>s
|
||||
</div>
|
||||
<p class="lead text-secondary">
|
||||
Started {{ object.start }}
|
||||
|
@ -75,7 +75,7 @@
|
|||
{% block javascript %}
|
||||
{% if object.active %}
|
||||
<script type="application/javascript">
|
||||
BabyBuddy.Timer.run('timer-status');
|
||||
BabyBuddy.Timer.run({{ timer.id }}, 'timer-status');
|
||||
</script>
|
||||
{% endif %}
|
||||
{% endblock %}
|
|
@ -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)
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue