[PATCH v2 2/9] urls: Use new login/password change CBVs

Daniel Axtens dja at axtens.net
Thu Nov 16 20:59:49 AEDT 2017


Hi Stephen,

I have 2 changes:
 - fixed a > where >= was probably intended
 - use reverse_lazy in url patterns - fixes a bug in my testing, see:
     https://docs.djangoproject.com/en/1.11/ref/urlresolvers/#reverse-lazy
     http://www.boxtricks.com/keyerror-en-us-when-doing-a-reverse-lookup-in-urls-py/

The following diff on top of your patch should be sufficient, apart from
that it looks good to me. Feel free to add my Signed-off to this patch
if you apply it.

diff --git a/patchwork/compat.py b/patchwork/compat.py
index 38a844d93d03..b609aab487ca 100644
--- a/patchwork/compat.py
+++ b/patchwork/compat.py
@@ -86,9 +86,11 @@ if settings.ENABLE_REST_API:
 if django.VERSION >= (1, 10):
     from django.urls import NoReverseMatch  # noqa
     from django.urls import reverse  # noqa
+    from django.urls import reverse_lazy  # noqa
 else:
     from django.core.urlresolvers import NoReverseMatch  # noqa
     from django.core.urlresolvers import reverse  # noqa
+    from django.core.urlresolvers import reverse_lazy  # noqa
diff --git a/patchwork/urls.py b/patchwork/urls.py
index 6d49d648f635..719347220a86 100644
--- a/patchwork/urls.py
+++ b/patchwork/urls.py
@@ -23,7 +23,7 @@ from django.conf.urls import url, include
 from django.contrib import admin
 from django.contrib.auth import views as auth_views
 
-from patchwork.compat import reverse
+from patchwork.compat import reverse_lazy
 from patchwork.views import about as about_views
 from patchwork.views import api as api_views
 from patchwork.views import bundle as bundle_views
@@ -90,7 +90,7 @@ urlpatterns = [
 ]
 
 # password change
-if django.VERSION > (1, 11):
+if django.VERSION >= (1, 11):
     urlpatterns += [
         url(r'^user/password-change/$',
             auth_views.PasswordChangeView.as_view(),
@@ -142,7 +142,7 @@ if django.VERSION >= (1, 11):
             template_name='patchwork/login.html'),
             name='auth_login'),
         url(r'^user/logout/$', auth_views.LogoutView.as_view(
-            next_page=reverse('project-list')),
+            next_page=reverse_lazy('project-list')),
             name='auth_logout'),
     ]
 else:
@@ -151,7 +151,7 @@ else:
             {'template_name': 'patchwork/login.html'},
             name='auth_login'),
         url(r'^user/logout/$', auth_views.logout,
-            {'next_page': reverse('project-list')},
+            {'next_page': reverse_lazy('project-list')},
             name='auth_logout'),
     ]


> The function based views are deprecated in Django 1.11 [1], so support
> the newer class based views.
>
> [1] https://docs.djangoproject.com/en/dev/releases/1.11/#id2
>
> Signed-off-by: Stephen Finucane <stephen at that.guru>
> ---
>  patchwork/urls.py | 94 +++++++++++++++++++++++++++++++++++++++++--------------
>  1 file changed, 70 insertions(+), 24 deletions(-)
>
> diff --git a/patchwork/urls.py b/patchwork/urls.py
> index 285d5659..b33b01c4 100644
> --- a/patchwork/urls.py
> +++ b/patchwork/urls.py
> @@ -17,11 +17,13 @@
>  # along with Patchwork; if not, write to the Free Software
>  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
>  
> +import django
>  from django.conf import settings
>  from django.conf.urls import url, include
>  from django.contrib import admin
>  from django.contrib.auth import views as auth_views
>  
> +from patchwork.compat import reverse
>  from patchwork.views import about as about_views
>  from patchwork.views import api as api_views
>  from patchwork.views import bundle as bundle_views
> @@ -85,32 +87,75 @@ urlpatterns = [
>          name='user-link'),
>      url(r'^user/unlink/(?P<person_id>[^/]+)/$', user_views.unlink,
>          name='user-unlink'),
> +]
>  
> -    # password change
> -    url(r'^user/password-change/$', auth_views.password_change,
> -        name='password_change'),
> -    url(r'^user/password-change/done/$', auth_views.password_change_done,
> -        name='password_change_done'),
> -    url(r'^user/password-reset/$', auth_views.password_reset,
> -        name='password_reset'),
> -    url(r'^user/password-reset/mail-sent/$', auth_views.password_reset_done,
> -        name='password_reset_done'),
> -    url(r'^user/password-reset/(?P<uidb64>[0-9A-Za-z_\-]+)/'
> -        r'(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',
> -        auth_views.password_reset_confirm,
> -        name='password_reset_confirm'),
> -    url(r'^user/password-reset/complete/$',
> -        auth_views.password_reset_complete,
> -        name='password_reset_complete'),
> -
> -    # login/logout
> -    url(r'^user/login/$', auth_views.login,
> -        {'template_name': 'patchwork/login.html'},
> -        name='auth_login'),
> -    url(r'^user/logout/$', auth_views.logout,
> -        {'next_page': '/'},
> -        name='auth_logout'),
> +# password change
> +if django.VERSION > (1, 11):
> +    urlpatterns += [
> +        url(r'^user/password-change/$',
> +            auth_views.PasswordChangeView.as_view(),
> +            name='password_change'),
> +        url(r'^user/password-change/done/$',
> +            auth_views.PasswordChangeDoneView.as_view(),
> +            name='password_change_done'),
> +        url(r'^user/password-reset/$',
> +            auth_views.PasswordResetView.as_view(),
> +            name='password_reset'),
> +        url(r'^user/password-reset/mail-sent/$',
> +            auth_views.PasswordResetDoneView.as_view(),
> +            name='password_reset_done'),
> +        url(r'^user/password-reset/(?P<uidb64>[0-9A-Za-z_\-]+)/'
> +            r'(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',
> +            auth_views.PasswordResetConfirmView.as_view(),
> +            name='password_reset_confirm'),
> +        url(r'^user/password-reset/complete/$',
> +            auth_views.PasswordResetCompleteView.as_view(),
> +            name='password_reset_complete'),
> +    ]
> +else:
> +    urlpatterns += [
> +        url(r'^user/password-change/$',
> +            auth_views.password_change,
> +            name='password_change'),
> +        url(r'^user/password-change/done/$',
> +            auth_views.password_change_done,
> +            name='password_change_done'),
> +        url(r'^user/password-reset/$',
> +            auth_views.password_reset,
> +            name='password_reset'),
> +        url(r'^user/password-reset/mail-sent/$',
> +            auth_views.password_reset_done,
> +            name='password_reset_done'),
> +        url(r'^user/password-reset/(?P<uidb64>[0-9A-Za-z_\-]+)/'
> +            r'(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',
> +            auth_views.password_reset_confirm,
> +            name='password_reset_confirm'),
> +        url(r'^user/password-reset/complete/$',
> +            auth_views.password_reset_complete,
> +            name='password_reset_complete'),
> +    ]
>  
> +# login/logout
> +if django.VERSION >= (1, 11):
> +    urlpatterns += [
> +        url(r'^user/login/$', auth_views.LoginView.as_view(
> +            template_name='patchwork/login.html'),
> +            name='auth_login'),
> +        url(r'^user/logout/$', auth_views.LogoutView.as_view(
> +            next_page=reverse('project-list')),
> +            name='auth_logout'),
> +    ]
> +else:
> +    urlpatterns += [
> +        url(r'^user/login/$', auth_views.login,
> +            {'template_name': 'patchwork/login.html'},
> +            name='auth_login'),
> +        url(r'^user/logout/$', auth_views.logout,
> +            {'next_page': reverse('project-list')},
> +            name='auth_logout'),
> +    ]
> +
> +urlpatterns += [
>      # registration
>      url(r'^register/', user_views.register, name='user-register'),
>  
> @@ -144,6 +189,7 @@ urlpatterns = [
>  
>  if 'debug_toolbar' in settings.INSTALLED_APPS:
>      import debug_toolbar
> +
>      urlpatterns += [
>          url(r'^__debug__/', include(debug_toolbar.urls)),
>      ]
> -- 
> 2.13.6
>
> _______________________________________________
> Patchwork mailing list
> Patchwork at lists.ozlabs.org
> https://lists.ozlabs.org/listinfo/patchwork


More information about the Patchwork mailing list