﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
32477	Wrapping of admin views does not preserve view attributes	Matt Pryor	nobody	"Because of how the `wrap` function is defined in `get_urls` for both `AdminSite` and `ModelAdmin`, any properties added to the views by the `admin_view` decorator are not propagated to the actual view that is used.

This can be fixed by tweaking the implementation of the `wrap` function (note that a similar tweak would need to be made to the `wrap` function in `ModelAdmin`):

{{{
#!python

def custom_decorator(view):
    def wrapper(*args, **kwargs):
        return view(*args, **kwargs)
    wrapper.custom_attribute = 'SOMEVALUE'
    return update_wrapper(wrapper, view)


class CustomAdminSite(AdminSite):
    def admin_view(self, view, cacheable=False):
        # Apply custom decorator to all admin views
        return custom_decorator(super().admin_view(view, cacheable))

    def get_urls(self):
        # Current implementation of wrap
        def wrap(view, cacheable=False):
            def wrapper(*args, **kwargs):
                return self.admin_view(view, cacheable)(*args, **kwargs)
            wrapper.admin_site = self
            return update_wrapper(wrapper, view)

        # This will print False
        print(hasattr(wrap(self.index), 'custom_attribute')

        # Suggested implementation of wrap
        def wrap(view, cacheable=False):
            wrapped = self.admin_view(view, cacheable)
            wrapped.admin_site = self
            return wrapped

        # This will print True
        print(hasattr(wrap(self.index), 'custom_attribute')
        # This will print SOMEVALUE
        print(wrap(self.index).custom_attribute)

        return super().get_urls()
}}}"	Bug	closed	contrib.admin	3.1	Normal	needsinfo			Unreviewed	0	0	0	0	0	0
