Skip to content

Commit

Permalink
Merge pull request #348 from patfreeman/feature_delete_multiple_drinks
Browse files Browse the repository at this point in the history
Allow deleting drinks from Kegbot Admin drinks page
  • Loading branch information
mik3y authored Apr 24, 2017
2 parents 5135efd + d5c14b6 commit b1abf0d
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 26 deletions.
39 changes: 39 additions & 0 deletions pykeg/web/kegadmin/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("""<table class="table table-hover table-bordered">
<thead>
<tr>
<th>Select</th>
<th>Drink</th>
<th>Date</th>
<th>Volume</th>
<th>User</th>
<th>Keg</th>
</tr>
</thead>
<tbody>
{% load kegweblib %}
{% for drink in drinks %}
<tr>
<td><input type="checkbox" name="delete_ids[]" value="{{ drink.id }}"></td>
<td>
<div class="form-horizontal">
<a class="btn btn-small btn-primary" href="{% url "kb-drink" drink.id %}">Edit</a>&nbsp;&nbsp;
<span>{{ drink.id }}</span>
</td>
<td>{{ drink.time }}</td>
<td>{% volume drink.volume_ml %}</td>
<td>{{ drink.user }}</td>
<td>{{ drink.keg }}</td>
</tr>
{% endfor %}
</tbody>
</table>"""),
FormActions(
Submit('delete_drinks', 'Delete Drinks', css_class='btn-danger'),
)
)


class ReassignDrinkForm(forms.Form):
username = forms.CharField(required=True)

Expand Down
27 changes: 1 addition & 26 deletions pykeg/web/kegadmin/templates/kegadmin/drink_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,7 @@

{% block kegadmin-main %}

<table class="table table-hover table-bordered">
<thead>
<tr>
<th>Drink</th>
<th>Date</th>
<th>Volume</th>
<th>User</th>
<th>Keg</th>
</tr>
</thead>
<tbody>
{% for drink in drinks %}
<tr>
<td>
<div class="form-horizontal">
<a class="btn btn-small btn-primary" href="{% url "kb-drink" drink.id %}">Edit</a>&nbsp;&nbsp;
<span>{{ drink.id }}</span>
</td>
<td>{{ drink.time }}</td>
<td>{% volume drink.volume_ml %}</td>
<td>{{ drink.user }}</td>
<td>{{ drink.keg }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% crispy delete_drinks_form %}
{% bootstrap_paginate drinks %}

{% endblock %}
Expand Down
20 changes: 20 additions & 0 deletions pykeg/web/kegadmin/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)


Expand Down

0 comments on commit b1abf0d

Please sign in to comment.