Browse Source

[py3] Avoided passing a lazy string to urlparse.

This causes an exception under Python 3.

Fixed #18776.
Aymeric Augustin 12 years ago
parent
commit
de3ad8bb2d
2 changed files with 8 additions and 4 deletions
  1. 4 1
      django/contrib/auth/decorators.py
  2. 4 3
      django/contrib/auth/views.py

+ 4 - 1
django/contrib/auth/decorators.py

@@ -7,6 +7,7 @@ from django.conf import settings
 from django.contrib.auth import REDIRECT_FIELD_NAME
 from django.core.exceptions import PermissionDenied
 from django.utils.decorators import available_attrs
+from django.utils.encoding import force_str
 
 
 def user_passes_test(test_func, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME):
@@ -22,9 +23,11 @@ def user_passes_test(test_func, login_url=None, redirect_field_name=REDIRECT_FIE
             if test_func(request.user):
                 return view_func(request, *args, **kwargs)
             path = request.build_absolute_uri()
+            # urlparse chokes on lazy objects in Python 3
+            login_url_as_str = force_str(login_url or settings.LOGIN_URL)
             # If the login url is the same scheme and net location then just
             # use the path as the "next" url.
-            login_scheme, login_netloc = urlparse(login_url or settings.LOGIN_URL)[:2]
+            login_scheme, login_netloc = urlparse(login_url_as_str)[:2]
             current_scheme, current_netloc = urlparse(path)[:2]
             if ((not login_scheme or login_scheme == current_scheme) and
                 (not login_netloc or login_netloc == current_netloc)):

+ 4 - 3
django/contrib/auth/views.py

@@ -7,6 +7,7 @@ from django.conf import settings
 from django.core.urlresolvers import reverse
 from django.http import HttpResponseRedirect, QueryDict
 from django.template.response import TemplateResponse
+from django.utils.encoding import force_str
 from django.utils.http import base36_to_int
 from django.utils.translation import ugettext as _
 from django.views.decorators.debug import sensitive_post_parameters
@@ -116,10 +117,10 @@ def redirect_to_login(next, login_url=None,
     """
     Redirects the user to the login page, passing the given 'next' page
     """
-    if not login_url:
-        login_url = settings.LOGIN_URL
+    # urlparse chokes on lazy objects in Python 3
+    login_url_as_str = force_str(login_url or settings.LOGIN_URL)
 
-    login_url_parts = list(urlparse(login_url))
+    login_url_parts = list(urlparse(login_url_as_str))
     if redirect_field_name:
         querystring = QueryDict(login_url_parts[4], mutable=True)
         querystring[redirect_field_name] = next