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
|
from core import models
|
||||||
|
|
||||||
|
|
||||||
# Sets the default Child instance if only one exists in the database.
|
|
||||||
def set_default_child(kwargs):
|
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)
|
instance = kwargs.get('instance', None)
|
||||||
|
child_slug = kwargs.get('child', None)
|
||||||
if not kwargs.get('initial'):
|
if not kwargs.get('initial'):
|
||||||
kwargs.update(initial={})
|
kwargs.update(initial={})
|
||||||
if instance is None and models.Child.objects.count() == 1:
|
|
||||||
|
# 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()})
|
kwargs['initial'].update({'child': models.Child.objects.first()})
|
||||||
|
|
||||||
|
try:
|
||||||
|
kwargs.pop('child')
|
||||||
|
except KeyError:
|
||||||
|
pass
|
||||||
return kwargs
|
return kwargs
|
||||||
|
|
||||||
|
|
||||||
# Sets default values (start/end date, child) from a timer.
|
# Sets default values (start/end date, child) from a timer.
|
||||||
def set_defaults_from_timer(kwargs):
|
def set_defaults_from_timer(kwargs):
|
||||||
timer = kwargs.get('instance', None)
|
instance = kwargs.get('instance', None)
|
||||||
timer_id = kwargs.get('timer', None)
|
timer_id = kwargs.get('timer', None)
|
||||||
if not kwargs.get('initial'):
|
if not kwargs.get('initial'):
|
||||||
kwargs.update(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)
|
timer = models.Timer.objects.get(id=timer_id)
|
||||||
kwargs['initial'].update({
|
kwargs['initial'].update({
|
||||||
'timer': timer,
|
'timer': timer,
|
||||||
'start': timer.start,
|
'start': timer.start,
|
||||||
'end': timer.end or timezone.now()
|
'end': timer.end or timezone.now()
|
||||||
})
|
})
|
||||||
if timer.child:
|
|
||||||
kwargs['initial'].update({'child': timer.child})
|
|
||||||
try:
|
try:
|
||||||
kwargs.pop('timer')
|
kwargs.pop('timer')
|
||||||
except KeyError:
|
except KeyError:
|
||||||
|
|
|
@ -37,7 +37,7 @@
|
||||||
|
|
||||||
{% if perms.core.add_feeding %}
|
{% if perms.core.add_feeding %}
|
||||||
<a class="btn btn-success btn-lg btn-block mb-3"
|
<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>
|
role="button"><i class="icon icon-feeding" aria-hidden="true"></i>
|
||||||
{% trans "Feeding" %}
|
{% trans "Feeding" %}
|
||||||
</a>
|
</a>
|
||||||
|
@ -45,7 +45,7 @@
|
||||||
|
|
||||||
{% if perms.core.add_sleep %}
|
{% if perms.core.add_sleep %}
|
||||||
<a class="btn btn-success btn-lg btn-block mb-3"
|
<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>
|
role="button"><i class="icon icon-sleep" aria-hidden="true"></i>
|
||||||
{% trans "Sleep" %}
|
{% trans "Sleep" %}
|
||||||
</a>
|
</a>
|
||||||
|
@ -53,7 +53,7 @@
|
||||||
|
|
||||||
{% if perms.core.add_tummytime %}
|
{% if perms.core.add_tummytime %}
|
||||||
<a class="btn btn-success btn-lg btn-block mb-3"
|
<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>
|
role="button"><i class="icon icon-tummytime" aria-hidden="true"></i>
|
||||||
{% trans "Tummy Time" %}
|
{% trans "Tummy Time" %}
|
||||||
</a>
|
</a>
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
from django import template
|
from django import template
|
||||||
|
from django.urls import reverse
|
||||||
|
|
||||||
from core.models import Timer
|
from core.models import Timer
|
||||||
|
|
||||||
|
@ -20,3 +21,12 @@ def timer_nav(context, active=True):
|
||||||
perms = context['perms'] or None
|
perms = context['perms'] or None
|
||||||
# The 'next' parameter is currently not used.
|
# The 'next' parameter is currently not used.
|
||||||
return {'timers': timers, 'perms': perms, 'next': request.path}
|
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!')
|
self.success_message = _('%(model)s entry added!')
|
||||||
return self.success_message % cleaned_data
|
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,
|
class CoreUpdateView(PermissionRequired403Mixin, SuccessMessageMixin,
|
||||||
UpdateView):
|
UpdateView):
|
||||||
|
@ -137,12 +153,6 @@ class FeedingAdd(CoreAddView):
|
||||||
form_class = forms.FeedingForm
|
form_class = forms.FeedingForm
|
||||||
success_url = reverse_lazy('core:feeding-list')
|
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):
|
class FeedingUpdate(CoreUpdateView):
|
||||||
model = models.Feeding
|
model = models.Feeding
|
||||||
|
@ -199,12 +209,6 @@ class SleepAdd(CoreAddView):
|
||||||
form_class = forms.SleepForm
|
form_class = forms.SleepForm
|
||||||
success_url = reverse_lazy('core:sleep-list')
|
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):
|
class SleepUpdate(CoreUpdateView):
|
||||||
model = models.Sleep
|
model = models.Sleep
|
||||||
|
@ -353,12 +357,6 @@ class TummyTimeAdd(CoreAddView):
|
||||||
form_class = forms.TummyTimeForm
|
form_class = forms.TummyTimeForm
|
||||||
success_url = reverse_lazy('core:tummytime-list')
|
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):
|
class TummyTimeUpdate(CoreUpdateView):
|
||||||
model = models.TummyTime
|
model = models.TummyTime
|
||||||
|
|
Loading…
Reference in New Issue