Update decorator patterns util to preserve original view method name when introspecting.

When decorate_urlpatterns apply a decorator to a view function it lost spec names, docs, and the module where it was imported, making it uneasy to inspect when debugging (Like getting the name view and docs when using django-debug-toolbar)

Using functools.update_wrapper it updates the resulting function to look like the original wrapped view function.

Without the use of this decorator factory, the name of the wrapped view function would have been 'decorated_view'.
pull/2575/merge
Mario César 2016-05-06 17:37:32 -04:00 zatwierdzone przez Tim Heap
rodzic 6b6866345e
commit b95cc1e687
4 zmienionych plików z 6 dodań i 1 usunięć

Wyświetl plik

@ -36,6 +36,7 @@ Changelog
* Fix: REM units in Wagtailuserbar caused incorrect spacing (Vincent Audebert)
* Fix: Explorer menu no longer scrolls with page content (Vincent Audebert)
* Fix: Border added around explorer menu to stop it blending in with StreamField block listing (Alex Gleason)
* Fix: `decorate_urlpatterns` now uses `functools.update_wrapper` to keep view names and docstrings (Mario César)
1.4.4 (xx.xx.2016)

Wyświetl plik

@ -130,6 +130,7 @@ Contributors
* Nick Smith
* João Luiz Lorencetti
* Jason Morrison
* Mario César
Translators
===========

Wyświetl plik

@ -65,6 +65,7 @@ Bug fixes
* REM units in Wagtailuserbar caused incorrect spacing (Vincent Audebert)
* Explorer menu no longer scrolls with page content (Vincent Audebert)
* Border added around explorer menu to stop it blending in with StreamField block listing (Alex Gleason)
* ``decorate_urlpatterns`` now uses ``functools.update_wrapper`` to keep view names and docstrings (Mario César)
Upgrade considerations

Wyświetl plik

@ -1,5 +1,7 @@
from __future__ import absolute_import, unicode_literals
from functools import update_wrapper
def decorate_urlpatterns(urlpatterns, decorator):
for pattern in urlpatterns:
@ -7,6 +9,6 @@ def decorate_urlpatterns(urlpatterns, decorator):
decorate_urlpatterns(pattern.url_patterns, decorator)
if hasattr(pattern, '_callback'):
pattern._callback = decorator(pattern.callback)
pattern._callback = update_wrapper(decorator(pattern.callback), pattern.callback)
return urlpatterns