Add ability to delete all inactive Timers (#108)

This commit is contained in:
Christopher C. Wells 2020-02-06 21:15:36 -08:00
parent 005a4f180e
commit 127c3d5cb0
5 changed files with 70 additions and 1 deletions

View File

@ -47,6 +47,7 @@ INSTALLED_APPS = [
'django.contrib.sessions', 'django.contrib.sessions',
'django.contrib.messages', 'django.contrib.messages',
'django.contrib.staticfiles', 'django.contrib.staticfiles',
'django.contrib.humanize',
] ]
# Middleware # Middleware

View File

@ -0,0 +1,24 @@
{% extends 'babybuddy/page.html' %}
{% load humanize i18n widget_tweaks %}
{% block title %}
{% blocktrans %}Delete All Inactive Timers{% endblocktrans %}
{% endblock %}
{% block breadcrumbs %}
<li class="breadcrumb-item"><a href="{% url 'core:timer-list' %}">{% trans "Timers" %}</a></li>
<li class="breadcrumb-item active" aria-current="page">{% trans "Delete Inactive" %}</li>
{% endblock %}
{% block content %}
<form role="form" method="post">
{% csrf_token %}
<h1>
{% blocktrans trimmed with number=timer_count|apnumber|intcomma plural=timer_count|pluralize %}
Are you sure you want to delete {{ number }} inactive timer{{ plural }}?
{% endblocktrans %}
</h1>
<input type="submit" value="{% trans "Delete" %}" class="btn btn-danger" />
<a href="/" class="btn btn-default">{% trans "Cancel" %}</a>
</form>
{% endblock %}

View File

@ -56,4 +56,11 @@
</table> </table>
</div> </div>
{% include 'babybuddy/paginator.html' %} {% include 'babybuddy/paginator.html' %}
{% if perms.core.delete_timer %}
<a href="{% url 'core:timer-delete-inactive' %}" class="btn btn-sm btn-danger">
<i class="icon icon-delete" aria-hidden="true"></i> {% trans "Delete Inactive Timers" %}
</a>
{% endif %}
{% endblock %} {% endblock %}

View File

@ -121,6 +121,11 @@ urlpatterns = [
views.TimerRestart.as_view(), views.TimerRestart.as_view(),
name='timer-restart' name='timer-restart'
), ),
path(
'timers/delete-inactive/',
views.TimerDeleteInactive.as_view(),
name='timer-delete-inactive'
),
path('tummy-time/', views.TummyTimeList.as_view(), name='tummytime-list'), path('tummy-time/', views.TummyTimeList.as_view(), name='tummytime-list'),
path( path(

View File

@ -1,12 +1,15 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from django.contrib import messages from django.contrib import messages
from django.contrib.messages.views import SuccessMessageMixin from django.contrib.messages.views import SuccessMessageMixin
from django.forms import Form
from django.http import HttpResponseRedirect
from django.urls import reverse, reverse_lazy from django.urls import reverse, reverse_lazy
from django.utils import timezone from django.utils import timezone
from django.utils.translation import gettext as _ from django.utils.translation import gettext as _
from django.views.generic.base import RedirectView from django.views.generic.base import RedirectView
from django.views.generic.detail import DetailView from django.views.generic.detail import DetailView
from django.views.generic.edit import CreateView, UpdateView, DeleteView from django.views.generic.edit import CreateView, UpdateView, DeleteView, \
FormView
from babybuddy.mixins import PermissionRequired403Mixin from babybuddy.mixins import PermissionRequired403Mixin
from babybuddy.views import BabyBuddyFilterView from babybuddy.views import BabyBuddyFilterView
@ -353,6 +356,35 @@ class TimerDelete(CoreDeleteView):
success_url = reverse_lazy('core:timer-list') success_url = reverse_lazy('core:timer-list')
class TimerDeleteInactive(PermissionRequired403Mixin, SuccessMessageMixin,
FormView):
permission_required = ('core.delete_timer',)
form_class = Form
template_name = 'core/timer_confirm_delete_inactive.html'
success_url = reverse_lazy('core:timer-list')
success_message = _('All inactive timers deleted.')
def get_context_data(self, **kwargs):
kwargs = super().get_context_data(**kwargs)
kwargs['timer_count'] = self.get_instances().count()
return kwargs
def get(self, request, *args, **kwargs):
# Redirect back to list if there are no inactive timers.
if self.get_instances().count() == 0:
messages.warning(request, _('No inactive timers exist.'))
return HttpResponseRedirect(self.success_url)
return super().get(request, *args, **kwargs)
def form_valid(self, form):
self.get_instances().delete()
return super().form_valid(form)
@staticmethod
def get_instances():
return models.Timer.objects.filter(active=False)
class TummyTimeList(PermissionRequired403Mixin, BabyBuddyFilterView): class TummyTimeList(PermissionRequired403Mixin, BabyBuddyFilterView):
model = models.TummyTime model = models.TummyTime
template_name = 'core/tummytime_list.html' template_name = 'core/tummytime_list.html'