Adds toggle to select all for deletion

Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Prachi Srivastava 2018-11-26 12:51:20 +05:30 committed by James Valleroy
parent 5ec14f7c97
commit 1ab4815e8a
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
5 changed files with 30 additions and 86 deletions

View File

@ -1,57 +0,0 @@
{% extends "base.html" %}
{% comment %}
#
# This file is part of FreedomBox.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
{% endcomment %}
{% load bootstrap %}
{% load i18n %}
{% block content %}
<h2>{{ title }}</h2>
<p>{% trans "Delete the following snapshots permanently?" %}</p>
<table class="table table-bordered table-condensed table-striped">
<thead>
<th>{% trans "Number" %}</th>
<th>{% trans "Date" %}</th>
<th>{% trans "Description" %}</th>
</thead>
<tbody>
{% for snapshot in snapshots %}
{% if not snapshot.is_default %}
<tr>
<td>{{ snapshot.number }}</td>
<td>{{ snapshot.date }}</td>
<td>{{ snapshot.description }}</td>
</tr>
{% endif %}
{% endfor %}
</tbody>
</table>
<p>
<form class="form" method="post">
{% csrf_token %}
<input type="submit" class="btn btn-danger"
value="{% trans 'Delete Snapshots' %}"/>
</form>
</p>
{% endblock %}

View File

@ -33,7 +33,7 @@
<th>{% trans "Description" %}</th>
</thead>
<tbody>
{% for snapshot in snapshot_list %}
{% for snapshot in snapshots %}
{% if not snapshot.is_default %}
<tr>
<td>{{ snapshot.number }}</td>

View File

@ -33,7 +33,7 @@
value="{% trans 'Create Snapshot' %}"/>
</div>
<div class="col-xs-6 text-right">
<input type="submit" class="btn btn-primary" name="delete_selected"
<input type="submit" class="btn btn-danger" name="delete_selected"
value="{% trans 'Delete Snapshots' %}"/>
</div>
</div>
@ -44,7 +44,7 @@
<th>{% trans "Date" %}</th>
<th>{% trans "Description" %}</th>
<th>{% trans "Rollback" %}</th>
<th>{% trans "Delete" %}</th>
<th><input type="checkbox" name="select_all" onclick="toggle(this)"></th>
</thead>
<tbody>
{% for snapshot in snapshots %}
@ -81,6 +81,19 @@
</tbody>
</table>
</div>
</form>
{% endblock %}
</form>
{% endblock %}
{% endblock %}
{% block page_js %}
<script>
function toggle(source) {
var checkedState = source.checked;
checkboxes = document.getElementsByName('snapshot_list');
jQuery.each(checkboxes, function(i, checkbox) {
checkbox.checked = checkedState;
})
}
</script>
{% endblock %}

View File

@ -25,7 +25,6 @@ from . import views
urlpatterns = [
url(r'^sys/snapshot/$', views.index, name='index'),
url(r'^sys/snapshot/manage/$', views.manage, name='manage'),
url(r'^sys/snapshot/all/delete$', views.delete_all, name='delete-all'),
url(r'^sys/snapshot/selected/delete$', views.delete_selected, name='delete-selected'),
url(r'^sys/snapshot/(?P<number>\d+)/rollback$', views.rollback,
name='rollback'),

View File

@ -71,9 +71,10 @@ def manage(request):
actions.superuser_run('snapshot', ['create'])
messages.success(request, _('Created snapshot.'))
if 'delete_selected' in request.POST:
snapshot_to_delete = request.POST.getlist('snapshot_list')
request.session['snapshots'] = snapshot_to_delete
return redirect(reverse('snapshot:delete-selected'))
if request.POST.getlist('snapshot_list'):
snapshot_to_delete = request.POST.getlist('snapshot_list')
request.session['snapshots'] = snapshot_to_delete
return redirect(reverse('snapshot:delete-selected'))
output = actions.superuser_run('snapshot', ['list'])
snapshots = json.loads(output)
@ -135,37 +136,25 @@ def delete_selected(request):
if request.method == 'POST':
if 'snapshots' in request.session:
to_delete = request.session['snapshots']
for snapshot in to_delete:
actions.superuser_run('snapshot', ['delete', snapshot])
messages.success(request, _('Deleted the selected snapshots'))
if to_delete == len(snapshots):
actions.superuser_run('snapshot', ['delete_all'])
messages.success(request, _('Deleted all snapshots'))
else:
for snapshot in to_delete:
actions.superuser_run('snapshot', ['delete', snapshot])
messages.success(request, _('Deleted selected snapshots'))
return redirect(reverse('snapshot:manage'))
if 'snapshots' in request.session:
data = request.session['snapshots']
to_delete = list(filter(lambda x: x['number'] in data, snapshots))
return TemplateResponse(request, 'snapshot_delete_all.html', {
return TemplateResponse(request, 'snapshot_delete_selected.html', {
'title': _('Delete Snapshots'),
'snapshots': to_delete
})
def delete_all(request):
"""Show confirmation to delete all snapshots."""
if request.method == 'POST':
actions.superuser_run('snapshot', ['delete-all'])
messages.success(request, _('Deleted all snapshots.'))
return redirect(reverse('snapshot:manage'))
output = actions.superuser_run('snapshot', ['list'])
snapshots = json.loads(output)
return TemplateResponse(request, 'snapshot_delete_all.html', {
'title': _('Delete Snapshots'),
'snapshots': snapshots[1:]
})
def rollback(request, number):
"""Show confirmation to rollback to a snapshot."""
if request.method == 'POST':