burke-software / django-mass-edit

Make bulk changes in the Django admin interface
152 stars 67 forks source link

Compatibility with djangoadmin_reorder #106

Open pakal opened 2 years ago

pakal commented 2 years ago

Just in case someone stumbles on this :

the mass-edit form page doesn't get updated by djangoadmin_reorder middleware, because it uses direct render() and not template reponse ;

so one should replace in massadmin/massadmin.py the calls to "render" with calls to "TemplateResponse".

This is only the first step, because djangoadmin_reorder doesn't have a way to detect that django-mass-edit urls belong to the admin category. I have no idea what's the best way to make apps recognize each other, for example django-adminsortable uses get_urls() method of Admin to register itself, and thus naturally gets recognzied as part of the admin.

For now here is a quick hack to force recognition:


def patched_ModelAdminReorder_process_template_response(self, request, response):
    try:
        url = resolve(request.path_info)
    except Resolver404:
        return response

    if not url.app_name == 'admin' and \
            url.url_name not in ['index', 'app_list', 'massadmin_change_view']:  # HACKED
        # current view is not a django admin index
        # or app_list view, bail out!
        return response

    if 'app_list' in response.context_data:
        app_list = response.context_data['app_list']
        context_key = 'app_list'
    # handle django 3.1 sidebar
    elif 'available_apps' in response.context_data:
        app_list = response.context_data['available_apps']
        context_key = 'available_apps'
    else:  # nothing to reorder, return response
        return response

    self.init_config(request, app_list)
    ordered_app_list = self.get_app_list()
    response.context_data[context_key] = ordered_app_list
    return response

import admin_reorder.middleware
admin_reorder.middleware.ModelAdminReorder.process_template_response = patched_ModelAdminReorder_process_template_response```