summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2021-10-27 14:20:12 +0000
committerÉtienne Loks <etienne.loks@iggdrasil.net>2021-11-16 17:04:41 +0100
commita72adcf584bd6aa1508df6dbcbdffddd6ab6c930 (patch)
treefd2d0029442ff83a701bd06eab482baba5161738
parentaaebbb9fc57eecea352973c59945ae877642a46b (diff)
downloadIshtar-a72adcf584bd6aa1508df6dbcbdffddd6ab6c930.tar.bz2
Ishtar-a72adcf584bd6aa1508df6dbcbdffddd6ab6c930.zip
Admin - list action: change parent (refs #4698)
-rw-r--r--ishtar_common/admin.py66
-rw-r--r--ishtar_common/templates/admin/change_parent.html17
2 files changed, 82 insertions, 1 deletions
diff --git a/ishtar_common/admin.py b/ishtar_common/admin.py
index 03e736f4e..58096b0cd 100644
--- a/ishtar_common/admin.py
+++ b/ishtar_common/admin.py
@@ -368,6 +368,70 @@ TokenAdmin.raw_id_fields = ("user",)
admin_site.register(Token, TokenAdmin)
+class ChangeParentForm(forms.Form):
+ change_parent = forms.ChoiceField(label=_("Change parent"), choices=[])
+
+ def __init__(self, choices, post=None, files=None):
+ super(ChangeParentForm, self).__init__(post, files)
+ self.fields["change_parent"].choices = choices
+
+
+class ChangeParentAdmin:
+ def get_actions(self, request):
+ action_dct = super(ChangeParentAdmin, self).get_actions(request)
+ if hasattr(self.model, "parent"):
+ action_dct["change_parent_selected"] = (
+ self.change_parent_admin,
+ "change_parent_selected",
+ _("Change parent of selected item(s)"),
+ )
+ return action_dct
+
+ def change_parent_admin(self, modeladmin, request, queryset):
+ return_url = reverse(
+ "admin:%s_%s_changelist"
+ % (self.model._meta.app_label, self.model._meta.model_name)
+ )
+ if not request.POST:
+ return HttpResponseRedirect(return_url)
+
+ selected_children_pk = request.POST.getlist("_selected_action", [])
+ choices = []
+ parents = self.model.objects.all()
+ for obj in parents:
+ choices.append((obj.pk, str(obj)))
+
+ form = None
+ if "apply" in request.POST:
+ form = ChangeParentForm(choices, request.POST, request.FILES)
+ if form.is_valid():
+ change_parent = form.cleaned_data["change_parent"]
+ change = []
+ for child_id in selected_children_pk:
+ child = self.model.objects.get(pk=child_id)
+ child.parent_id = change_parent
+ child.save()
+ change.append(str(child))
+ messages.add_message(
+ request,
+ messages.INFO,
+ str(_("{} parent(s) was change to {}.")).format(
+ " ; ".join(change), str(self.model.objects.get(pk=change_parent))
+ ),
+ )
+ return HttpResponseRedirect(return_url)
+ if not form:
+ form = ChangeParentForm(choices)
+ return render(
+ request,
+ "admin/change_parent.html",
+ {
+ "change_parent_form": form,
+ "current_action": "change_parent_selected",
+ "selected_items": selected_children_pk,
+ },
+ )
+
class HistorizedObjectAdmin(admin.ModelAdmin):
readonly_fields = [
"history_creator",
@@ -968,7 +1032,7 @@ class TownAdmin(ImportGEOJSONActionAdmin, ImportActionAdmin):
admin_site.register(models_common.Town, TownAdmin)
-class GeneralTypeAdmin(ImportActionAdmin, ImportJSONActionAdmin):
+class GeneralTypeAdmin(ChangeParentAdmin, ImportActionAdmin, ImportJSONActionAdmin):
search_fields = (
"label",
"txt_idx",
diff --git a/ishtar_common/templates/admin/change_parent.html b/ishtar_common/templates/admin/change_parent.html
new file mode 100644
index 000000000..8d6744cff
--- /dev/null
+++ b/ishtar_common/templates/admin/change_parent.html
@@ -0,0 +1,17 @@
+{% extends "admin/base_site.html" %}{% load i18n %}
+
+{% block content %}
+
+ <form action="." method="post" enctype="multipart/form-data">
+ {% csrf_token %}
+ <table>
+ {{ change_parent_form }}
+ </table>
+ <input type="hidden" name="action" value="{{ current_action }}"/>
+ {% for selected in selected_items %}
+ <input type="hidden" name="_selected_action" value="{{ selected }}"/>
+ {% endfor %}
+ <input type="submit" name="apply" value="{% trans 'Change parent' %}"/>
+ </form>
+
+{% endblock %}