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 TimerForm(forms.ModelForm):
class Meta: class Meta:
model = models.Timer model = models.Timer
fields = ['name', 'start'] fields = ['child', 'name', 'start']
widgets = { widgets = {
'start': forms.DateTimeInput(attrs={ 'start': forms.DateTimeInput(attrs={
'readonly': 'readonly', 'readonly': 'readonly',
@ -203,6 +203,7 @@ class TimerForm(forms.ModelForm):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user') self.user = kwargs.pop('user')
kwargs = set_default_child(kwargs)
super(TimerForm, self).__init__(*args, **kwargs) super(TimerForm, self).__init__(*args, **kwargs)
def save(self, commit=True): 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): class Timer(models.Model):
model_name = 'timer' model_name = 'timer'
child = models.ForeignKey(
'Child',
blank=True,
null=True,
on_delete=models.CASCADE,
related_name='timers',
verbose_name=_('Child')
)
name = models.CharField( name = models.CharField(
blank=True, blank=True,
max_length=255, max_length=255,
@ -412,6 +420,11 @@ class Timer(models.Model):
def __str__(self): def __str__(self):
return self.name or str(format_lazy(_('Timer #{id}'), id=self.id)) 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 @classmethod
def from_db(cls, db, field_names, values): def from_db(cls, db, field_names, values):
instance = super(Timer, cls).from_db(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-minutes">{{ object.duration|minutes }}</span>m
<span class="timer-seconds">{{ object.duration|seconds }}</span>s <span class="timer-seconds">{{ object.duration|seconds }}</span>s
</div> </div>
{% if timer.child %}
<div class="h2">
{{ timer.child }}
</div>
{% endif %}
<p class="lead text-secondary"> <p class="lead text-secondary">
{% trans "Started" %} {{ object.start }} {% trans "Started" %} {{ object.start }}
{% if not object.active %} {% if not object.active %}

View File

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

View File

@ -26,7 +26,9 @@
{% if timers %} {% if timers %}
<h6 class="dropdown-header">{% trans "Active Timers" %}</h6> <h6 class="dropdown-header">{% trans "Active Timers" %}</h6>
{% for timer in timers %} {% 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 %} {% empty %}
<a class="dropdown-item disabled" href="#">{% trans "None" %}</a> <a class="dropdown-item disabled" href="#">{% trans "None" %}</a>
{% endfor %} {% endfor %}

View File

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