Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow deleting drinks from Kegbot Admin drinks page #348

Merged
merged 1 commit into from
Apr 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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