#32477: Wrapping of admin views does not preserve view attributes
-----------------------------------------+------------------------
Reporter: Matt Pryor | Owner: nobody
Type: Bug | Status: new
Component: contrib.admin | Version: 3.1
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+------------------------
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()
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/32477>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/056.5d31d6b79085e2c2899d3a0ea768bd3c%40djangoproject.com.