From 177fe32c9a4fb140f99964cc3d103b1b2cced436 Mon Sep 17 00:00:00 2001 From: Cefin Date: Fri, 15 Oct 2021 16:16:59 +0200 Subject: draft version number one --- ishtar_common/admin.py | 58 +++++++++++++++++++++++- ishtar_common/templates/admin/change_parent.html | 17 +++++++ 2 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 ishtar_common/templates/admin/change_parent.html diff --git a/ishtar_common/admin.py b/ishtar_common/admin.py index 03e736f4e..68e15404b 100644 --- a/ishtar_common/admin.py +++ b/ishtar_common/admin.py @@ -368,6 +368,62 @@ 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): + if post: + super(ChangeParentForm, self).__init__(post, files) + else: + super(ChangeParentForm, self).__init__() + self.fields["change_parent"].choices = choices + +class ChangeParentAdmin: + def get_actions(self, request): + action_dct = super(ChangeParentAdmin, self).get_actions(request) + action_dct["change_parent_selected"] = ( + self.change_parent, + "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 = request.POST.getlist("_selected_action", []) + items = {} + choices = [] + for pk in selected: + obj = self.model._get_parent_types(pk) + choices.append(obj.pk, str(obj)) + items[str(obj.pk)] = obj + + form = None + if "apply" in request.POST: + form = ChangeParentForm(choices, request.POST, request.FILES) + if form.is_valid(): + + messages.add_message( + request, + messages.INFO, + ) + return HttpResponseRedirect(return_url) + if not form: + form = ChangeParentForm(choices) + return render( + request, + "admin/change_parent.html", + { + "selected_item": selected, + }, + ) + class HistorizedObjectAdmin(admin.ModelAdmin): readonly_fields = [ "history_creator", @@ -968,7 +1024,7 @@ class TownAdmin(ImportGEOJSONActionAdmin, ImportActionAdmin): admin_site.register(models_common.Town, TownAdmin) -class GeneralTypeAdmin(ImportActionAdmin, ImportJSONActionAdmin): +class GeneralTypeAdmin(ImportActionAdmin, ImportJSONActionAdmin, ChangeParentAdmin): 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..5ab6d5b6b --- /dev/null +++ b/ishtar_common/templates/admin/change_parent.html @@ -0,0 +1,17 @@ +{% extends "admin/base_site.html" %}{% load i18n %} + +{% block content %} + +
+ {% crsf_token %} + + {{ change_parent_form }} +
+ + {% for selected in selected_items %} + + {% endfor %} + +
+ +{% endblock %} -- cgit v1.2.3