Add optional Child to Timer model

This commit is contained in:
Christopher C. Wells 2020-01-28 13:56:28 -08:00 committed by Christopher Charbonneau Wells
parent e168babd86
commit c80ec252bc
7 changed files with 47 additions and 3 deletions

View File

@ -193,7 +193,7 @@ class TemperatureForm(forms.ModelForm):
class TimerForm(forms.ModelForm):
class Meta:
model = models.Timer
fields = ['name', 'start']
fields = ['child', 'name', 'start']
widgets = {
'start': forms.DateTimeInput(attrs={
'readonly': 'readonly',
@ -203,6 +203,7 @@ class TimerForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user')
kwargs = set_default_child(kwargs)
super(TimerForm, self).__init__(*args, **kwargs)
def save(self, commit=True):

View File

@ -0,0 +1,19 @@
# Generated by Django 3.0.2 on 2020-01-28 21:29
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('core', '0009_diaperchange_amount'),
]
operations = [
migrations.AddField(
model_name='timer',
name='child',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='timers', to='core.Child', verbose_name='Child'),
),
]

View File

@ -367,6 +367,14 @@ class Temperature(models.Model):
class Timer(models.Model):
model_name = 'timer'
child = models.ForeignKey(
'Child',
blank=True,
null=True,
on_delete=models.CASCADE,
related_name='timers',
verbose_name=_('Child')
)
name = models.CharField(
blank=True,
max_length=255,
@ -412,6 +420,11 @@ class Timer(models.Model):
def __str__(self):
return self.name or str(format_lazy(_('Timer #{id}'), id=self.id))
@property
def title_with_child(self):
return format_lazy('{title} ({child})', title=str(self),
child=self.child)
@classmethod
def from_db(cls, db, field_names, values):
instance = super(Timer, cls).from_db(db, field_names, values)

View File

@ -16,6 +16,13 @@
<span class="timer-minutes">{{ object.duration|minutes }}</span>m
<span class="timer-seconds">{{ object.duration|seconds }}</span>s
</div>
{% if timer.child %}
<div class="h2">
{{ timer.child }}
</div>
{% endif %}
<p class="lead text-secondary">
{% trans "Started" %} {{ object.start }}
{% if not object.active %}

View File

@ -15,6 +15,7 @@
<thead class="thead-inverse">
<tr>
<th>{% trans "Name" %}</th>
<th>{% trans "Child" %}</th>
<th>{% trans "Start" %}</th>
<th>{% trans "Duration" %}</th>
<th>{% trans "End" %}</th>
@ -26,6 +27,7 @@
{% for timer in object_list %}
<tr>
<th scope="row"><a href="{% url 'core:timer-detail' timer.id %}">{{ timer }}</a></th>
<td><a href="{% url 'core:child' timer.child.slug %}">{{ timer.child }}</a></td>
<td>{{ timer.start }}</td>
<td>{{ timer.duration|duration_string }}</td>
<td>{{ timer.end }}</td>

View File

@ -26,7 +26,9 @@
{% if timers %}
<h6 class="dropdown-header">{% trans "Active Timers" %}</h6>
{% for timer in timers %}
<a class="dropdown-item" href="{% url 'core:timer-detail' timer.id %}">{{ timer }} ({{ timer.user }})</a>
<a class="dropdown-item" href="{% url 'core:timer-detail' timer.id %}">
{{ timer.title_with_child }}
</a>
{% empty %}
<a class="dropdown-item disabled" href="#">{% trans "None" %}</a>
{% endfor %}

View File

@ -16,7 +16,7 @@
{% for instance in instances %}
<a href="{% url 'core:timer-detail' instance.id %}"
class="list-group-item list-group-item-action">
<strong>{{ instance }}</strong>
<strong>{{ instance.title_with_child }}</strong>
<p class="text-muted small m-0">
{% blocktrans trimmed with start=instance.start|time user=instance.user %}
Started by {{ user }} at {{ start }}