From e5a5e330b9af8d14e4132b58f022fc4f2af86f05 Mon Sep 17 00:00:00 2001 From: "Christopher C. Wells" Date: Tue, 28 Jan 2020 22:15:55 -0800 Subject: [PATCH] Obscure timer-child relations for one child use case --- core/models.py | 11 ++++++++--- core/templates/core/timer_list.html | 8 +++++++- core/views.py | 3 +++ 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/core/models.py b/core/models.py index dfd2a750..4e59cb2a 100644 --- a/core/models.py +++ b/core/models.py @@ -251,7 +251,7 @@ class Feeding(models.Model): validate_unique_period(Feeding.objects.filter(child=self.child), self) # "Formula" Type may only be associated with "Bottle" Method. - if self.type == 'formula'and self.method != 'bottle': + if self.type == 'formula' and self.method != 'bottle': raise ValidationError( {'method': _('Only "Bottle" method is allowed with "Formula" type.')}, @@ -435,8 +435,13 @@ class Timer(models.Model): @property def title_with_child(self): - return format_lazy('{title} ({child})', title=str(self), - child=self.child) + """ Get Timer title with child name in parenthesis. """ + title = str(self) + # Only actually add the name if there is more than one Child instance. + if title and self.child and Child.count() > 1: + title = format_lazy('{title} ({child})', title=title, + child=self.child) + return title @classmethod def from_db(cls, db, field_names, values): diff --git a/core/templates/core/timer_list.html b/core/templates/core/timer_list.html index fa5f7c76..fd16b815 100644 --- a/core/templates/core/timer_list.html +++ b/core/templates/core/timer_list.html @@ -27,7 +27,13 @@ {% for timer in object_list %} {{ timer }} - {{ timer.child }} + + {% if timer.child %} + {{ timer.child }} + {% else %} +   + {% endif %} + {{ timer.start }} {{ timer.duration|duration_string }} {{ timer.end }} diff --git a/core/views.py b/core/views.py index 0066e59b..422d978e 100644 --- a/core/views.py +++ b/core/views.py @@ -297,6 +297,9 @@ class TimerAddQuick(PermissionRequired403Mixin, RedirectView): def get(self, request, *args, **kwargs): instance = models.Timer.objects.create(user=request.user) + # Add child relationship if there is only Child instance. + if models.Child.count() == 1: + instance.child = models.Child.objects.first() instance.save() self.url = request.GET.get( 'next', reverse('core:timer-detail', args={instance.id}))