Add ability to stop a timer (WIP).

This commit handles everything except including the timer in the timers list in the UI. This may require changing the model declaration to make the "active" field more complex or add another field.
This commit is contained in:
Christopher Charbonneau Wells 2017-09-08 14:36:22 -04:00
parent c3e5a0349e
commit 7f5a40ae41
5 changed files with 37 additions and 10 deletions

View File

@ -25,11 +25,11 @@ def set_default_duration(kwargs):
if not kwargs.get('initial'): if not kwargs.get('initial'):
kwargs.update(initial={}) kwargs.update(initial={})
if not instance and timer_id: if not instance and timer_id:
timer_instance = Timer.objects.get(id=timer_id) instance = Timer.objects.get(id=timer_id)
kwargs['initial'].update({ kwargs['initial'].update({
'timer': timer_instance, 'timer': instance,
'start': timer_instance.start, 'start': instance.start,
'end': timezone.now() 'end': instance.end or timezone.now()
}) })
try: try:
kwargs.pop('timer') kwargs.pop('timer')

View File

@ -11,7 +11,12 @@
<span class="timer-minutes">{{ object.current_duration|minutes }}</span>m <span class="timer-minutes">{{ object.current_duration|minutes }}</span>m
<span class="timer-seconds">{{ object.current_duration|seconds }}</span>s <span class="timer-seconds">{{ object.current_duration|seconds }}</span>s
</h1> </h1>
<p class="lead text-muted">Started {{ object.start }} by {{ object.user }}</p> <p class="lead text-muted">
Started {{ object.start }}
{% if not timer.active %}
/ Stopped {{ object.end }}
{% endif %}
</p>
{% if perms.core.add_feeding %} {% if perms.core.add_feeding %}
<a class="btn btn-success btn-lg btn-block p-3 mb-3" <a class="btn btn-success btn-lg btn-block p-3 mb-3"
@ -34,13 +39,21 @@
{% if perms.core.delete_timer %} {% if perms.core.delete_timer %}
<a class="btn btn-danger btn-lg btn-block p-3 mb-3" <a class="btn btn-danger btn-lg btn-block p-3 mb-3"
href="{% url 'timer-delete' timer.id %}" href="{% url 'timer-delete' timer.id %}"
role="button"><i class="fa fa-times-circle" aria-hidden="true"></i> Delete Timer</a> role="button"><i class="fa fa-trash" aria-hidden="true"></i> Delete Timer</a>
{% endif %}
{% if object.active and perms.core.change_timer %}
<a class="btn btn-warning btn-lg btn-block p-3 mb-3"
href="{% url 'timer-stop' timer.id %}"
role="button"><i class="fa fa-stop" aria-hidden="true"></i> Stop Timer</a>
{% endif %} {% endif %}
</div> </div>
{% endblock %} {% endblock %}
{% block javascript %} {% block javascript %}
{% if object.active %}
<script type="application/javascript"> <script type="application/javascript">
BabyBlotter.Timer.run('timer-status'); BabyBlotter.Timer.run('timer-status');
</script> </script>
{% endif %}
{% endblock %} {% endblock %}

View File

@ -54,6 +54,8 @@ urlpatterns = [
name='timer-detail'), name='timer-detail'),
url(r'^timer/(?P<pk>[0-9]+)/delete/$', views.TimerDelete.as_view(), url(r'^timer/(?P<pk>[0-9]+)/delete/$', views.TimerDelete.as_view(),
name='timer-delete'), name='timer-delete'),
url(r'^timer/(?P<pk>[0-9]+)/stop/$', views.TimerStop.as_view(),
name='timer-stop'),
url(r'^tummy-time/$', views.TummyTimeList.as_view(), url(r'^tummy-time/$', views.TummyTimeList.as_view(),
name='tummytime-list'), name='tummytime-list'),

View File

@ -16,7 +16,7 @@ def filter_by_params(request, model, available_params):
def timer_stop(timer_id, end=None): def timer_stop(timer_id, end=None):
"""Stop a timer instance by setting it's end field.""" """Stop a timer instance by setting the end field value."""
if not end: if not end:
end = timezone.now() end = timezone.now()
from .models import Timer from .models import Timer

View File

@ -12,6 +12,7 @@ from django.views.generic.list import ListView
from .models import Child, DiaperChange, Feeding, Note, Sleep, Timer, TummyTime from .models import Child, DiaperChange, Feeding, Note, Sleep, Timer, TummyTime
from .forms import (ChildForm, DiaperChangeForm, FeedingForm, SleepForm, from .forms import (ChildForm, DiaperChangeForm, FeedingForm, SleepForm,
TimerForm, TummyTimeForm) TimerForm, TummyTimeForm)
from .utils import timer_stop
class ChildList(PermissionRequiredMixin, ListView): class ChildList(PermissionRequiredMixin, ListView):
@ -185,6 +186,17 @@ class TimerAddQuick(PermissionRequiredMixin, RedirectView):
return super(TimerAddQuick, self).get(request, *args, **kwargs) return super(TimerAddQuick, self).get(request, *args, **kwargs)
class TimerStop(PermissionRequiredMixin, RedirectView):
permission_required = ('core.change_timer',)
def get(self, request, *args, **kwargs):
timer_stop(kwargs['pk'])
return super(TimerStop, self).get(request, *args, **kwargs)
def get_redirect_url(self, *args, **kwargs):
return '/timer/{}'.format(kwargs['pk'])
class TimerDelete(PermissionRequiredMixin, DeleteView): class TimerDelete(PermissionRequiredMixin, DeleteView):
model = Timer model = Timer
permission_required = ('core.delete_timer',) permission_required = ('core.delete_timer',)