From d5c14b6f5f690037f103ecfe51489620d0393679 Mon Sep 17 00:00:00 2001 From: Patrick Freeman Date: Wed, 16 Nov 2016 21:12:07 -0600 Subject: [PATCH] Allow deleting drinks from the Kegbot Admin --- pykeg/web/kegadmin/forms.py | 39 +++++++++++++++++++ .../templates/kegadmin/drink_list.html | 27 +------------ pykeg/web/kegadmin/views.py | 20 ++++++++++ 3 files changed, 60 insertions(+), 26 deletions(-) diff --git a/pykeg/web/kegadmin/forms.py b/pykeg/web/kegadmin/forms.py index e9bd77e43..18406e371 100644 --- a/pykeg/web/kegadmin/forms.py +++ b/pykeg/web/kegadmin/forms.py @@ -559,6 +559,45 @@ class CancelDrinkForm(forms.Form): pass +class DeleteDrinksForm(forms.Form): + helper = FormHelper() + helper.form_class = 'form-horizontal user-select' + helper.layout = Layout( + HTML(""" + + + + + + + + + + + +{% load kegweblib %} +{% for drink in drinks %} + + + + + + + + +{% endfor %} + +
SelectDrinkDateVolumeUserKeg
+
+ Edit   + {{ drink.id }} +
{{ drink.time }}{% volume drink.volume_ml %}{{ drink.user }}{{ drink.keg }}
"""), + FormActions( + Submit('delete_drinks', 'Delete Drinks', css_class='btn-danger'), + ) + ) + + class ReassignDrinkForm(forms.Form): username = forms.CharField(required=True) diff --git a/pykeg/web/kegadmin/templates/kegadmin/drink_list.html b/pykeg/web/kegadmin/templates/kegadmin/drink_list.html index f9de0f6be..d8de9b84f 100644 --- a/pykeg/web/kegadmin/templates/kegadmin/drink_list.html +++ b/pykeg/web/kegadmin/templates/kegadmin/drink_list.html @@ -8,32 +8,7 @@ {% block kegadmin-main %} - - - - - - - - - - - -{% for drink in drinks %} - - - - - - - -{% endfor %} - -
DrinkDateVolumeUserKeg
-
- Edit   - {{ drink.id }} -
{{ drink.time }}{% volume drink.volume_ml %}{{ drink.user }}{{ drink.keg }}
+{% crispy delete_drinks_form %} {% bootstrap_paginate drinks %} {% endblock %} diff --git a/pykeg/web/kegadmin/views.py b/pykeg/web/kegadmin/views.py index 53e94f78e..85600a909 100644 --- a/pykeg/web/kegadmin/views.py +++ b/pykeg/web/kegadmin/views.py @@ -650,6 +650,25 @@ def user_detail(request, user_id): @staff_member_required def drink_list(request): + delete_drinks_form = forms.DeleteDrinksForm() + + if 'delete_drinks' in request.POST: + form = forms.DeleteDrinksForm(request.POST) + if form.is_valid(): + delete_ids = request.POST.getlist("delete_ids[]") + drinks = models.Drink.objects.filter(Q(id__in=delete_ids)) + for drink in drinks: + drink.delete() + delete_ids.reverse() + if len(delete_ids) == 1: + messages.success(request, 'Drink ' + delete_ids[0] + ' has been deleted.') + elif len(delete_ids) == 2: + messages.success(request, 'Drinks ' + ' and '.join(delete_ids) + + ' have been deleted.') + else: + messages.success(request, 'Drinks ' + ', '.join(delete_ids[:-1]) + ', and ' + + delete_ids[-1] + ' have been deleted.') + context = RequestContext(request) drinks = models.Drink.objects.all().order_by('-time') paginator = Paginator(drinks, 25) @@ -663,6 +682,7 @@ def drink_list(request): drinks = paginator.page(paginator.num_pages) context['drinks'] = drinks + context['delete_drinks_form'] = delete_drinks_form return render_to_response('kegadmin/drink_list.html', context_instance=context)