Ticket #15552: t15552.patch

File t15552.patch, 11.7 KB (added by Ulrich Petri, 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  
    33from django.conf import settings
    44from django.contrib.auth import REDIRECT_FIELD_NAME
    55from django.utils.decorators import available_attrs
     6from django.utils.url import resolve_url
    67
    78
    89def 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  
    2021            path = request.build_absolute_uri()
    2122            # If the login url is the same scheme and net location then just
    2223            # use the path as the "next" url.
    23             login_scheme, login_netloc = urlparse.urlparse(login_url or
    24                                                         settings.LOGIN_URL)[:2]
     24            login_scheme, login_netloc = urlparse.urlparse(
     25                resolve_url(login_url or settings.LOGIN_URL))[:2]
    2526            current_scheme, current_netloc = urlparse.urlparse(path)[:2]
    2627            if ((not login_scheme or login_scheme == current_scheme) and
    2728                (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):  
    2525            pass
    2626        login_required(normal_view)
    2727
    28     def testLoginRequired(self, view_url='/login_required/', login_url=settings.LOGIN_URL):
     28    def testLoginRequired(self, view_url='/login_required/', login_url="/login/"):
    2929        """
    3030        Check that login_required works on a simple view wrapped in a
    3131        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  
    66from django.template.response import TemplateResponse
    77from django.utils.http import base36_to_int
    88from django.utils.translation import ugettext as _
     9from django.utils.url import resolve_url
    910from django.views.decorators.debug import sensitive_post_parameters
    1011from django.views.decorators.cache import never_cache
    1112from django.views.decorators.csrf import csrf_protect
    def login(request, template_name='registration/login.html',  
    3435    if request.method == "POST":
    3536        form = authentication_form(data=request.POST)
    3637        if form.is_valid():
    37             netloc = urlparse.urlparse(redirect_to)[1]
    38 
    3938            # Use default setting if redirect_to is empty
    4039            if not redirect_to:
    4140                redirect_to = settings.LOGIN_REDIRECT_URL
     41            redirect_to = resolve_url(redirect_to)
    4242
     43            netloc = urlparse.urlparse(redirect_to)[1]
    4344            # Heavier security check -- don't allow redirection to a different
    4445            # host.
    45             elif netloc and netloc != request.get_host():
    46                 redirect_to = settings.LOGIN_REDIRECT_URL
     46            if netloc and netloc != request.get_host():
     47                redirect_to = resolve_url(settings.LOGIN_REDIRECT_URL)
    4748
    4849            # Okay, security checks complete. Log the user in.
    4950            auth_login(request, form.get_user())
    def logout_then_login(request, login_url=None, current_app=None, extra_context=N  
    106107    """
    107108    if not login_url:
    108109        login_url = settings.LOGIN_URL
     110    login_url = resolve_url(login_url)
    109111    return logout(request, login_url, current_app=current_app, extra_context=extra_context)
    110112
    111113def redirect_to_login(next, login_url=None,
    def redirect_to_login(next, login_url=None,  
    115117    """
    116118    if not login_url:
    117119        login_url = settings.LOGIN_URL
     120    login_url = resolve_url(login_url)
    118121
    119122    login_url_parts = list(urlparse.urlparse(login_url))
    120123    if redirect_field_name:
    def password_reset_complete(request,  
    223226                            template_name='registration/password_reset_complete.html',
    224227                            current_app=None, extra_context=None):
    225228    context = {
    226         'login_url': settings.LOGIN_URL
     229        'login_url': resolve_url(settings.LOGIN_URL)
    227230    }
    228231    if extra_context is not None:
    229232        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  
    1010from django.db.models.manager import Manager
    1111from django.db.models.query import QuerySet
    1212from django.core import urlresolvers
     13from django.utils.url import resolve_url
    1314
    1415def render_to_response(*args, **kwargs):
    1516    """
    def redirect(to, *args, **kwargs):  
    6667    else:
    6768        redirect_class = HttpResponseRedirect
    6869
    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))
    8671
    8772def _get_queryset(klass):
    8873    """
  • 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
    - +  
     1from django.core import urlresolvers
     2
     3def 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  
    12521252This is used by the :func:`~django.contrib.auth.decorators.login_required`
    12531253decorator, for example.
    12541254
     1255.. versionchanged:: 1.4
     1256
     1257This setting now also accepts view function names and
     1258:ref:`named URL patterns <naming-url-patterns>` which can be used to reduce
     1259configuration duplication since you no longer have to define the URL in two
     1260places (``settings`` and URLconf).
     1261For backward compatibility reasons the default remains unchanged.
     1262
    12551263.. setting:: LOGIN_URL
    12561264
    12571265LOGIN_URL
    Default: ``'/accounts/login/'``  
    12621270The URL where requests are redirected for login, especially when using the
    12631271:func:`~django.contrib.auth.decorators.login_required` decorator.
    12641272
     1273.. versionchanged:: 1.4
     1274
     1275This setting now also accepts view function names and
     1276:ref:`named URL patterns <naming-url-patterns>` which can be used to reduce
     1277configuration duplication since you no longer have to define the URL in two
     1278places (``settings`` and URLconf).
     1279For backward compatibility reasons the default remains unchanged.
     1280
    12651281.. setting:: LOGOUT_URL
    12661282
    12671283LOGOUT_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  
    155155:ref:`custom filter<custom-error-reports>`. Learn more on
    156156:ref:`Filtering error reports<filtering-error-reports>`.
    157157
    158 
    159158Minor features
    160159~~~~~~~~~~~~~~
    161160
    Django 1.4 also includes several smaller improvements worth noting:  
    165164  trace which reference Django's code are dimmed out, while frames in user
    166165  code are slightly emphasized. This change makes it easier to scan a stacktrace
    167166  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
    168173
    169174
    170175.. _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  
    769769
    770770        (r'^accounts/login/$', 'django.contrib.auth.views.login'),
    771771
     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
    772779.. function:: views.login(request, [template_name, redirect_field_name, authentication_form])
    773780
    774781    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  
    1212# Helper base class for comment tests that need data.
    1313class CommentTestCase(TestCase):
    1414    fixtures = ["comment_tests"]
    15     urls = 'django.contrib.comments.urls'
     15    urls = 'regressiontests.comment_tests.urls_default'
    1616
    1717    def createSomeComments(self):
    1818        # 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
    - +  
     1from django.conf.urls.defaults import *
     2
     3urlpatterns = 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):  
    113113    settings.TEMPLATE_DIRS = (os.path.join(os.path.dirname(__file__), TEST_TEMPLATE_DIR),)
    114114    settings.USE_I18N = True
    115115    settings.LANGUAGE_CODE = 'en'
    116     settings.LOGIN_URL = '/accounts/login/'
     116    settings.LOGIN_URL = 'django.contrib.auth.views.login'
    117117    settings.MIDDLEWARE_CLASSES = (
    118118        'django.contrib.sessions.middleware.SessionMiddleware',
    119119        'django.contrib.auth.middleware.AuthenticationMiddleware',
Back to Top