mirror of https://github.com/snachodog/mybuddy.git
Set Child from an argument instead of the timer
This commit is contained in:
parent
5e9946ecd6
commit
4e01365e85
|
@ -7,31 +7,51 @@ from django.utils.translation import gettext as _
|
|||
from core import models
|
||||
|
||||
|
||||
# Sets the default Child instance if only one exists in the database.
|
||||
def set_default_child(kwargs):
|
||||
"""
|
||||
Sets the default Child for an instance based on the `child` parameter or
|
||||
if only one Child instance exists.
|
||||
|
||||
:param kwargs: Form arguments.
|
||||
:return: Form arguments with updated initial values.
|
||||
"""
|
||||
instance = kwargs.get('instance', None)
|
||||
child_slug = kwargs.get('child', None)
|
||||
if not kwargs.get('initial'):
|
||||
kwargs.update(initial={})
|
||||
if instance is None and models.Child.objects.count() == 1:
|
||||
kwargs['initial'].update({'child': models.Child.objects.first()})
|
||||
|
||||
# Do not update initial values for an existing instance (edit operation).
|
||||
if instance is None:
|
||||
if child_slug:
|
||||
kwargs['initial'].update({
|
||||
'child': models.Child.objects.filter(slug=child_slug).first(),
|
||||
})
|
||||
elif models.Child.count() == 1:
|
||||
kwargs['initial'].update({'child': models.Child.objects.first()})
|
||||
|
||||
try:
|
||||
kwargs.pop('child')
|
||||
except KeyError:
|
||||
pass
|
||||
return kwargs
|
||||
|
||||
|
||||
# Sets default values (start/end date, child) from a timer.
|
||||
def set_defaults_from_timer(kwargs):
|
||||
timer = kwargs.get('instance', None)
|
||||
instance = kwargs.get('instance', None)
|
||||
timer_id = kwargs.get('timer', None)
|
||||
if not kwargs.get('initial'):
|
||||
kwargs.update(initial={})
|
||||
if not timer and timer_id:
|
||||
|
||||
# Do not update initial values for an existing instance (edit operation).
|
||||
if not instance and timer_id:
|
||||
timer = models.Timer.objects.get(id=timer_id)
|
||||
kwargs['initial'].update({
|
||||
'timer': timer,
|
||||
'start': timer.start,
|
||||
'end': timer.end or timezone.now()
|
||||
})
|
||||
if timer.child:
|
||||
kwargs['initial'].update({'child': timer.child})
|
||||
|
||||
try:
|
||||
kwargs.pop('timer')
|
||||
except KeyError:
|
||||
|
|
|
@ -37,7 +37,7 @@
|
|||
|
||||
{% if perms.core.add_feeding %}
|
||||
<a class="btn btn-success btn-lg btn-block mb-3"
|
||||
href="{% url 'core:feeding-add' %}?timer={{ timer.id }}"
|
||||
href="{% instance_add_url 'core:feeding-add' %}"
|
||||
role="button"><i class="icon icon-feeding" aria-hidden="true"></i>
|
||||
{% trans "Feeding" %}
|
||||
</a>
|
||||
|
@ -45,7 +45,7 @@
|
|||
|
||||
{% if perms.core.add_sleep %}
|
||||
<a class="btn btn-success btn-lg btn-block mb-3"
|
||||
href="{% url 'core:sleep-add' %}?timer={{ timer.id }}"
|
||||
href="{% instance_add_url 'core:sleep-add' %}"
|
||||
role="button"><i class="icon icon-sleep" aria-hidden="true"></i>
|
||||
{% trans "Sleep" %}
|
||||
</a>
|
||||
|
@ -53,7 +53,7 @@
|
|||
|
||||
{% if perms.core.add_tummytime %}
|
||||
<a class="btn btn-success btn-lg btn-block mb-3"
|
||||
href="{% url 'core:tummytime-add' %}?timer={{ timer.id }}"
|
||||
href="{% instance_add_url 'core:tummytime-add' %}"
|
||||
role="button"><i class="icon icon-tummytime" aria-hidden="true"></i>
|
||||
{% trans "Tummy Time" %}
|
||||
</a>
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from django import template
|
||||
from django.urls import reverse
|
||||
|
||||
from core.models import Timer
|
||||
|
||||
|
@ -20,3 +21,12 @@ def timer_nav(context, active=True):
|
|||
perms = context['perms'] or None
|
||||
# The 'next' parameter is currently not used.
|
||||
return {'timers': timers, 'perms': perms, 'next': request.path}
|
||||
|
||||
|
||||
@register.simple_tag(takes_context=True)
|
||||
def instance_add_url(context, url_name):
|
||||
timer = context['timer']
|
||||
url = '{}?timer={}'.format(reverse(url_name), timer.id)
|
||||
if timer.child:
|
||||
url += '&child={}'.format(timer.child.slug)
|
||||
return url
|
||||
|
|
|
@ -22,6 +22,22 @@ class CoreAddView(PermissionRequired403Mixin, SuccessMessageMixin, CreateView):
|
|||
self.success_message = _('%(model)s entry added!')
|
||||
return self.success_message % cleaned_data
|
||||
|
||||
def get_form_kwargs(self):
|
||||
"""
|
||||
Check for and add "child" and "timer" from request query parameters.
|
||||
- "child" may provide a slug for a Child instance.
|
||||
- "timer" may provided an ID for a Timer instance.
|
||||
|
||||
These arguments are used in some add views to pre-fill initial data in
|
||||
the form fields.
|
||||
|
||||
:return: Updated keyword arguments.
|
||||
"""
|
||||
kwargs = super(CoreAddView, self).get_form_kwargs()
|
||||
kwargs.update({'child': self.request.GET.get('child', None)})
|
||||
kwargs.update({'timer': self.request.GET.get('timer', None)})
|
||||
return kwargs
|
||||
|
||||
|
||||
class CoreUpdateView(PermissionRequired403Mixin, SuccessMessageMixin,
|
||||
UpdateView):
|
||||
|
@ -137,12 +153,6 @@ class FeedingAdd(CoreAddView):
|
|||
form_class = forms.FeedingForm
|
||||
success_url = reverse_lazy('core:feeding-list')
|
||||
|
||||
def get_form_kwargs(self):
|
||||
kwargs = super(FeedingAdd, self).get_form_kwargs()
|
||||
# Add timer to be used by FeedingForm.__init__
|
||||
kwargs.update({'timer': self.request.GET.get('timer', None)})
|
||||
return kwargs
|
||||
|
||||
|
||||
class FeedingUpdate(CoreUpdateView):
|
||||
model = models.Feeding
|
||||
|
@ -199,12 +209,6 @@ class SleepAdd(CoreAddView):
|
|||
form_class = forms.SleepForm
|
||||
success_url = reverse_lazy('core:sleep-list')
|
||||
|
||||
def get_form_kwargs(self):
|
||||
kwargs = super(SleepAdd, self).get_form_kwargs()
|
||||
# Add timer to be used by SleepForm.__init__
|
||||
kwargs.update({'timer': self.request.GET.get('timer', None)})
|
||||
return kwargs
|
||||
|
||||
|
||||
class SleepUpdate(CoreUpdateView):
|
||||
model = models.Sleep
|
||||
|
@ -353,12 +357,6 @@ class TummyTimeAdd(CoreAddView):
|
|||
form_class = forms.TummyTimeForm
|
||||
success_url = reverse_lazy('core:tummytime-list')
|
||||
|
||||
def get_form_kwargs(self):
|
||||
kwargs = super(TummyTimeAdd, self).get_form_kwargs()
|
||||
# Add timer to be used by TummyTimeForm.__init__
|
||||
kwargs.update({'timer': self.request.GET.get('timer', None)})
|
||||
return kwargs
|
||||
|
||||
|
||||
class TummyTimeUpdate(CoreUpdateView):
|
||||
model = models.TummyTime
|
||||
|
|
Loading…
Reference in New Issue