Skip to content

Commit

Permalink
Löschen mehrerer Objekte via Aktion in Listenansicht
Browse files Browse the repository at this point in the history
  • Loading branch information
gdmhrogut committed Jan 8, 2019
1 parent b5d5137 commit 02c416a
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 53 deletions.
2 changes: 1 addition & 1 deletion datenerfassung/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

# Application definition

DATENMANAGEMENT_VERSION = '1.4.2'
DATENMANAGEMENT_VERSION = '1.4.3'

DJANGO_APPS = [
'django.contrib.admin',
Expand Down
4 changes: 4 additions & 0 deletions datenmanagement/static/datenmanagement/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ html {
width:100%;
}

.action_counter {
margin-left:10px;
}

.container {
width:80%;
}
Expand Down
127 changes: 80 additions & 47 deletions datenmanagement/templates/datenmanagement/datalist.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% extends "_base/navbar.html" %}
{% extends '_base/navbar.html' %}
{% load datenmanagement_tags %}
{% load guardian_tags %}
{% load staticfiles %}
Expand All @@ -22,61 +22,94 @@ <h4>{{ model_description }}</h4>
<h3><i>Liste aller Datensätze</i></h3>
<div class='row'>
{% if user|user_has_model_add_permission:model_name_lower %}
<a href="{% url 'datenmanagement:'|add:model_name|add:'add' %}" class='btn btn-success'>neuen Datensatz anlegen</a>
<a href='{% url 'datenmanagement:'|add:model_name|add:'add' %}' class='btn btn-success'>neuen Datensatz anlegen</a>
{% endif %}
{% if geometry_type %}
<a href="{% url 'datenmanagement:'|add:model_name|add:'map' %}" class='btn btn-default'>Datensätze auf Karte anzeigen</a>
<a href='{% url 'datenmanagement:'|add:model_name|add:'map' %}' class='btn btn-default'>Datensätze auf Karte anzeigen</a>
{% endif %}
<a href="{% url 'datenmanagement:'|add:model_name|add:'start' %}" class='btn btn-default'>zurück</a>
<a href='{% url 'datenmanagement:'|add:model_name|add:'start' %}' class='btn btn-default'>zurück</a>
</div>
<div class='col-md-6 full-width'>
<table id='datasets' class='table table-bordered table-striped'>
<thead>
<tr>
{% for label in list_fields_labels %}
{% with schritt=forloop.counter|stringformat:"s" %}
{% with spalte="spalte_"|add:schritt %}
<th>{{label}}</th>
{% endwith %}
{% endwith %}
{% endfor %}
<th><i>editieren</i></th>
<th><i>löschen</i></th>
</tr>
</thead>
</table>
<script type='text/javascript'>
$(document).ready(function(){
$('#datasets').dataTable({
ajax: "{% url 'datenmanagement:'|add:model_name|add:'data' %}",
columnDefs: [{
'orderable': false,
'targets': -2
}, {
'orderable': false,
'targets': -1
}],
language: {
url: '{% static 'datatables-1.10.18/datatables.german.lang' %}'
},
orderClasses: false,
pageLength: 25,
processing: true,
searchDelay: 500,
searching: true,
serverSide: true,
stateSave: true
});
});
</script>
</div>
<div class='col-md-6 full-width'>
<table id='datasets' class='table table-bordered table-striped'>
<thead>
<tr>
<th></th>
{% for label in list_fields_labels %}
{% with schritt=forloop.counter|stringformat:'s' %}
{% with spalte='spalte_'|add:schritt %}
<th>{{label}}</th>
{% endwith %}
{% endwith %}
{% endfor %}
<th><i>editieren</i></th>
<th><i>löschen</i></th>
</tr>
</thead>
</table>
<script type='text/javascript'>
$(document).ready(function(){
$('#datasets').dataTable({
ajax: '{% url 'datenmanagement:'|add:model_name|add:'data' %}',
columnDefs: [{
'orderable': false,
'targets': 0
}, {
'orderable': false,
'targets': -2
}, {
'orderable': false,
'targets': -1
}],
language: {
url: '{% static 'datatables-1.10.18/datatables.german.lang' %}'
},
order: [[ 1, 'asc' ]],
orderClasses: false,
pageLength: 25,
processing: true,
searchDelay: 500,
searching: true,
serverSide: true,
stateSave: true
});

$('#action_button').click(function() {
if ($('#action_select').val() === 'delete_selected') {
var actionCheckboxes = $('.action_checkbox').filter(':checked');
if (actionCheckboxes.length > 0) {
actionCheckboxes.each(function() {
jQuery.get(window.document.location + '../deleteimmediately/' + $(this).val() + '/');
});
window.location.href = '{% url 'datenmanagement:'|add:model_name|add:'list' %}';
}
}
});
});

$('body').on('change', '.action_checkbox', function(e) {
var actionCheckboxes = $('.action_checkbox').filter(':checked');
$('#action_count').text(actionCheckboxes.length);
});
</script>
</div>
{% if user|user_has_model_add_permission:model_name_lower %}
<div class='row'>
Aktion
<select id='action_select' name='action'>
<option value='' selected='selected'>---------</option>
<option value='delete_selected'>ausgewählte Datensätze löschen</option>
</select>
<button id='action_button' type='button' class='btn btn-default'>ausführen</button>
<span class='action_counter'>(<span id='action_count'>0</span> Datensätze ausgewählt)</span>
</div>
{% endif %}
{% else %}
<h2>Keine Datensätze vorhanden.</h2>
<div class='row'>
{% if user|user_has_model_add_permission:model_name_lower %}
<a href="{% url 'datenmanagement:'|add:model_name|add:'add' %}" class='btn btn-success'>neuen Datensatz anlegen</a>
<a href='{% url 'datenmanagement:'|add:model_name|add:'add' %}' class='btn btn-success'>neuen Datensatz anlegen</a>
{% endif %}
<a href="{% url 'datenmanagement:'|add:model_name|add:'start' %}" class='btn btn-default'>zurück</a>
<a href='{% url 'datenmanagement:'|add:model_name|add:'start' %}' class='btn btn-default'>zurück</a>
</div>
{% endif %}
{% endblock %}
2 changes: 1 addition & 1 deletion datenmanagement/templates/datenmanagement/datamap.html
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ <h3><i>Karte aller Datensätze</i></h3>
if (feature.properties.user_may_change == 'ok') {
layer.on('click', function (e) {
// jedem Feature einen Link auf dessen Bearbeitungsseite mitgeben
window.document.location = window.document.location + '../change/' + feature.properties.id + '/';
window.document.location = window.document.location + '../change/' + feature.properties.id;
});
}
// falls Geometrie nicht punkthaft ist...
Expand Down
1 change: 1 addition & 0 deletions datenmanagement/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ def permission_required(*perms):
urlpatterns.append(url(regex + r'add/$', permission_required('datenmanagement.add_' + model_name_lower)(views.DataAddView.as_view(model = model, template_name = 'datenmanagement/dataform.html', success_url = reverse_lazy('datenmanagement:' + model_name + 'start'))), name = model_name + 'add'))
urlpatterns.append(url(regex + r'change/(?P<pk>\d+)/$', permission_required('datenmanagement.change_' + model_name_lower, 'datenmanagement.delete_' + model_name_lower)(views.DataChangeView.as_view(model = model, template_name = 'datenmanagement/dataform.html', success_url = reverse_lazy('datenmanagement:' + model_name + 'start'))), name = model_name + 'change'))
urlpatterns.append(url(regex + r'delete/(?P<pk>\d+)/$', permission_required('datenmanagement.delete_' + model_name_lower)(views.DataDeleteView.as_view(model = model, template_name = 'datenmanagement/datadelete.html', success_url = reverse_lazy('datenmanagement:' + model_name + 'start'))), name = model_name + 'delete'))
urlpatterns.append(url(regex + r'deleteimmediately/(?P<pk>\d+)/$', permission_required('datenmanagement.delete_' + model_name_lower)(views.delete_object_immediately), name = model_name + 'deleteimmediately'))
22 changes: 18 additions & 4 deletions datenmanagement/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from django.forms import ModelForm, ValidationError
from django.forms.models import modelform_factory
from django.http import HttpResponse
from django.shortcuts import get_object_or_404, render
from django.template import loader
from django.utils.html import escape
from django.views import generic
Expand All @@ -34,6 +35,15 @@ def assign_widgets(field, widget = None):
return field.formfield()


def delete_object_immediately(request, pk):
model_name = re.sub('^.*\/', '', re.sub('\/deleteimmediately.*$', '', request.path_info))
model = apps.get_app_config('datenmanagement').get_model(model_name)
obj = get_object_or_404(model, pk = pk)
if ObjectPermissionChecker(request.user).has_perm('delete_' + model_name.lower(), obj):
obj.delete()
return HttpResponse(status = 204)


def get_thumb_url(url):
head, tail = os.path.split(url)
return head + '/thumbs/' + tail
Expand Down Expand Up @@ -162,6 +172,13 @@ def prepare_results(self, qs):
json_data = []
for item in qs:
item_data = []
item_id = getattr(item, self.model._meta.pk.name)
checker = ObjectPermissionChecker(self.request.user)
obj = self.model.objects.get(pk=item_id)
if checker.has_perm('delete_' + self.model_name_lower, obj):
item_data.append('<input class="action_checkbox" type="checkbox" value="' + str(item_id) + '">')
else:
item_data.append('')
for column in self.columns:
data = None
value = getattr(item, column)
Expand All @@ -183,15 +200,12 @@ def prepare_results(self, qs):
elif value is not None:
data = escape(value)
item_data.append(data)
item_id = getattr(item, self.model._meta.pk.name)
checker = ObjectPermissionChecker(self.request.user)
obj = self.model.objects.get(pk=item_id)
if checker.has_perm('change_' + self.model_name_lower, obj):
item_data.append('<a href="' + reverse('datenmanagement:' + self.model_name + 'change', args=[item_id]) + '"><span class="glyphicon glyphicon-pencil"/></a>')
else:
item_data.append('')
if checker.has_perm('delete_' + self.model_name_lower, obj):
item_data.append('<a href="' + reverse('datenmanagement:' + self.model_name + 'delete', args=[item_id]) + '"><span class="glyphicon glyphicon-trash"/></a>')
item_data.append('<a href="' + reverse('datenmanagement:' + self.model_name + 'delete', args=[item_id]) + '"><span class="glyphicon glyphicon-trash"/></a>')
else:
item_data.append('')
json_data.append(item_data)
Expand Down

0 comments on commit 02c416a

Please sign in to comment.