Ticket #15552: t15552.patch
File t15552.patch, 11.7 KB (added by , 13 years ago) |
---|
-
django/contrib/auth/decorators.py
diff --git a/django/contrib/auth/decorators.py b/django/contrib/auth/decorators.py index 00a6bce..c0aa1c4 100644
a b from functools import wraps 3 3 from django.conf import settings 4 4 from django.contrib.auth import REDIRECT_FIELD_NAME 5 5 from django.utils.decorators import available_attrs 6 from django.utils.url import resolve_url 6 7 7 8 8 9 def user_passes_test(test_func, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME): … … def user_passes_test(test_func, login_url=None, redirect_field_name=REDIRECT_FIE 20 21 path = request.build_absolute_uri() 21 22 # If the login url is the same scheme and net location then just 22 23 # use the path as the "next" url. 23 login_scheme, login_netloc = urlparse.urlparse( login_url or24 settings.LOGIN_URL)[:2]24 login_scheme, login_netloc = urlparse.urlparse( 25 resolve_url(login_url or settings.LOGIN_URL))[:2] 25 26 current_scheme, current_netloc = urlparse.urlparse(path)[:2] 26 27 if ((not login_scheme or login_scheme == current_scheme) and 27 28 (not login_netloc or login_netloc == current_netloc)): -
django/contrib/auth/tests/decorators.py
diff --git a/django/contrib/auth/tests/decorators.py b/django/contrib/auth/tests/decorators.py index bd3f011..dd41a32 100644
a b class LoginRequiredTestCase(AuthViewsTestCase): 25 25 pass 26 26 login_required(normal_view) 27 27 28 def testLoginRequired(self, view_url='/login_required/', login_url= settings.LOGIN_URL):28 def testLoginRequired(self, view_url='/login_required/', login_url="/login/"): 29 29 """ 30 30 Check that login_required works on a simple view wrapped in a 31 31 login_required decorator. -
django/contrib/auth/views.py
diff --git a/django/contrib/auth/views.py b/django/contrib/auth/views.py index 4995f89..8d63177 100644
a b from django.http import HttpResponseRedirect, QueryDict 6 6 from django.template.response import TemplateResponse 7 7 from django.utils.http import base36_to_int 8 8 from django.utils.translation import ugettext as _ 9 from django.utils.url import resolve_url 9 10 from django.views.decorators.debug import sensitive_post_parameters 10 11 from django.views.decorators.cache import never_cache 11 12 from django.views.decorators.csrf import csrf_protect … … def login(request, template_name='registration/login.html', 34 35 if request.method == "POST": 35 36 form = authentication_form(data=request.POST) 36 37 if form.is_valid(): 37 netloc = urlparse.urlparse(redirect_to)[1]38 39 38 # Use default setting if redirect_to is empty 40 39 if not redirect_to: 41 40 redirect_to = settings.LOGIN_REDIRECT_URL 41 redirect_to = resolve_url(redirect_to) 42 42 43 netloc = urlparse.urlparse(redirect_to)[1] 43 44 # Heavier security check -- don't allow redirection to a different 44 45 # host. 45 elif netloc and netloc != request.get_host():46 redirect_to = settings.LOGIN_REDIRECT_URL46 if netloc and netloc != request.get_host(): 47 redirect_to = resolve_url(settings.LOGIN_REDIRECT_URL) 47 48 48 49 # Okay, security checks complete. Log the user in. 49 50 auth_login(request, form.get_user()) … … def logout_then_login(request, login_url=None, current_app=None, extra_context=N 106 107 """ 107 108 if not login_url: 108 109 login_url = settings.LOGIN_URL 110 login_url = resolve_url(login_url) 109 111 return logout(request, login_url, current_app=current_app, extra_context=extra_context) 110 112 111 113 def redirect_to_login(next, login_url=None, … … def redirect_to_login(next, login_url=None, 115 117 """ 116 118 if not login_url: 117 119 login_url = settings.LOGIN_URL 120 login_url = resolve_url(login_url) 118 121 119 122 login_url_parts = list(urlparse.urlparse(login_url)) 120 123 if redirect_field_name: … … def password_reset_complete(request, 223 226 template_name='registration/password_reset_complete.html', 224 227 current_app=None, extra_context=None): 225 228 context = { 226 'login_url': settings.LOGIN_URL229 'login_url': resolve_url(settings.LOGIN_URL) 227 230 } 228 231 if extra_context is not None: 229 232 context.update(extra_context) -
django/shortcuts/__init__.py
diff --git a/django/shortcuts/__init__.py b/django/shortcuts/__init__.py index 9f97cae..4dc74d3 100644
a b from django.http import HttpResponseRedirect, HttpResponsePermanentRedirect 10 10 from django.db.models.manager import Manager 11 11 from django.db.models.query import QuerySet 12 12 from django.core import urlresolvers 13 from django.utils.url import resolve_url 13 14 14 15 def render_to_response(*args, **kwargs): 15 16 """ … … def redirect(to, *args, **kwargs): 66 67 else: 67 68 redirect_class = HttpResponseRedirect 68 69 69 # If it's a model, use get_absolute_url() 70 if hasattr(to, 'get_absolute_url'): 71 return redirect_class(to.get_absolute_url()) 72 73 # Next try a reverse URL resolution. 74 try: 75 return redirect_class(urlresolvers.reverse(to, args=args, kwargs=kwargs)) 76 except urlresolvers.NoReverseMatch: 77 # If this is a callable, re-raise. 78 if callable(to): 79 raise 80 # If this doesn't "feel" like a URL, re-raise. 81 if '/' not in to and '.' not in to: 82 raise 83 84 # Finally, fall back and assume it's a URL 85 return redirect_class(to) 70 return redirect_class(resolve_url(to, *args, **kwargs)) 86 71 87 72 def _get_queryset(klass): 88 73 """ -
new file django/utils/url.py
diff --git a/django/utils/url.py b/django/utils/url.py new file mode 100644 index 0000000..f7d0e0b
- + 1 from django.core import urlresolvers 2 3 def resolve_url(to, *args, **kwargs): 4 """ 5 Returns an URL apropriate for the arguments 6 passed. 7 8 The arguments could be: 9 10 * A model: the model's `get_absolute_url()` function will be called. 11 12 * A view name, possibly with arguments: `urlresolvers.reverse()` will 13 be used to reverse-resolve the name. 14 15 * A URL, which will be returned as-is. 16 """ 17 # If it's a model, use get_absolute_url() 18 if hasattr(to, 'get_absolute_url'): 19 return to.get_absolute_url() 20 21 # Next try a reverse URL resolution. 22 try: 23 return urlresolvers.reverse(to, args=args, kwargs=kwargs) 24 except urlresolvers.NoReverseMatch: 25 # If this is a callable, re-raise. 26 if callable(to): 27 raise 28 # If this doesn't "feel" like a URL, re-raise. 29 if '/' not in to and '.' not in to: 30 raise 31 32 # Finally, fall back and assume it's a URL 33 return to -
docs/ref/settings.txt
diff --git a/docs/ref/settings.txt b/docs/ref/settings.txt index e95c165..bbff83f 100644
a b The URL where requests are redirected after login when the 1252 1252 This is used by the :func:`~django.contrib.auth.decorators.login_required` 1253 1253 decorator, for example. 1254 1254 1255 .. versionchanged:: 1.4 1256 1257 This setting now also accepts view function names and 1258 :ref:`named URL patterns <naming-url-patterns>` which can be used to reduce 1259 configuration duplication since you no longer have to define the URL in two 1260 places (``settings`` and URLconf). 1261 For backward compatibility reasons the default remains unchanged. 1262 1255 1263 .. setting:: LOGIN_URL 1256 1264 1257 1265 LOGIN_URL … … Default: ``'/accounts/login/'`` 1262 1270 The URL where requests are redirected for login, especially when using the 1263 1271 :func:`~django.contrib.auth.decorators.login_required` decorator. 1264 1272 1273 .. versionchanged:: 1.4 1274 1275 This setting now also accepts view function names and 1276 :ref:`named URL patterns <naming-url-patterns>` which can be used to reduce 1277 configuration duplication since you no longer have to define the URL in two 1278 places (``settings`` and URLconf). 1279 For backward compatibility reasons the default remains unchanged. 1280 1265 1281 .. setting:: LOGOUT_URL 1266 1282 1267 1283 LOGOUT_URL -
docs/releases/1.4.txt
diff --git a/docs/releases/1.4.txt b/docs/releases/1.4.txt index 90f925c..ca84705 100644
a b You may override or customize the default filtering by writing a 155 155 :ref:`custom filter<custom-error-reports>`. Learn more on 156 156 :ref:`Filtering error reports<filtering-error-reports>`. 157 157 158 159 158 Minor features 160 159 ~~~~~~~~~~~~~~ 161 160 … … Django 1.4 also includes several smaller improvements worth noting: 165 164 trace which reference Django's code are dimmed out, while frames in user 166 165 code are slightly emphasized. This change makes it easier to scan a stacktrace 167 166 for issues in user code. 167 * The :setting:`LOGIN_URL` and :setting:`LOGIN_REDIRECT_URL` settings now also 168 accept view function names and 169 :ref:`named URL patterns <naming-url-patterns>`. This allows you to reduce 170 configuration duplication. More information can be found in the 171 :func:`~django.contrib.auth.decorators.login_required` documentation. 172 168 173 169 174 170 175 .. _backwards-incompatible-changes-1.4: -
docs/topics/auth.txt
diff --git a/docs/topics/auth.txt b/docs/topics/auth.txt index 12d538f..33a369b 100644
a b The login_required decorator 769 769 770 770 (r'^accounts/login/$', 'django.contrib.auth.views.login'), 771 771 772 .. versionchanged:: 1.4 773 774 As of version 1.4 :setting:`settings.LOGIN_URL <LOGIN_URL>` now also accepts 775 view function names and :ref:`named URL patterns <naming-url-patterns>`. 776 This allows you to freely remap your login view within your URLconf 777 without having to update the setting. 778 772 779 .. function:: views.login(request, [template_name, redirect_field_name, authentication_form]) 773 780 774 781 Here's what ``django.contrib.auth.views.login`` does: -
tests/regressiontests/comment_tests/tests/__init__.py
diff --git a/tests/regressiontests/comment_tests/tests/__init__.py b/tests/regressiontests/comment_tests/tests/__init__.py index 88c6f33..7fc5e29 100644
a b CT = ContentType.objects.get_for_model 12 12 # Helper base class for comment tests that need data. 13 13 class CommentTestCase(TestCase): 14 14 fixtures = ["comment_tests"] 15 urls = ' django.contrib.comments.urls'15 urls = 'regressiontests.comment_tests.urls_default' 16 16 17 17 def createSomeComments(self): 18 18 # Two anonymous comments on two different objects -
new file tests/regressiontests/comment_tests/urls_default.py
diff --git a/tests/regressiontests/comment_tests/urls_default.py b/tests/regressiontests/comment_tests/urls_default.py new file mode 100644 index 0000000..bfce8ff
- + 1 from django.conf.urls.defaults import * 2 3 urlpatterns = patterns('', 4 (r'^', include('django.contrib.comments.urls')), 5 6 # Provide the auth system login and logout views 7 (r'^accounts/login/$', 'django.contrib.auth.views.login', {'template_name': 'login.html'}), 8 (r'^accounts/logout/$', 'django.contrib.auth.views.logout'), 9 ) -
tests/runtests.py
diff --git a/tests/runtests.py b/tests/runtests.py index 6da3299..77c9281 100755
a b def setup(verbosity, test_labels): 113 113 settings.TEMPLATE_DIRS = (os.path.join(os.path.dirname(__file__), TEST_TEMPLATE_DIR),) 114 114 settings.USE_I18N = True 115 115 settings.LANGUAGE_CODE = 'en' 116 settings.LOGIN_URL = ' /accounts/login/'116 settings.LOGIN_URL = 'django.contrib.auth.views.login' 117 117 settings.MIDDLEWARE_CLASSES = ( 118 118 'django.contrib.sessions.middleware.SessionMiddleware', 119 119 'django.contrib.auth.middleware.AuthenticationMiddleware',