Django

Code

Changeset 5072

Show
Ignore:
Timestamp:
04/25/07 03:49:57 (1 year ago)
Author:
mtredinnick
Message:

Fixed #3185 -- Made values for login, logout and post-login redirect URLs
configurable. This is a combined patch from Vasily Sulatskov, Marc Fargas and
Collin Grady.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/conf/global_settings.py

    r4975 r5072  
    313313AUTHENTICATION_BACKENDS = ('django.contrib.auth.backends.ModelBackend',) 
    314314 
     315LOGIN_URL = '/accounts/login/' 
     316 
     317LOGOUT_URL = '/accounts/logout/' 
     318 
     319LOGIN_REDIRECT_URL = '/accounts/profile/' 
     320 
    315321########### 
    316322# TESTING # 
  • django/trunk/django/contrib/auth/decorators.py

    r4265 r5072  
    1 from django.contrib.auth import LOGIN_URL, REDIRECT_FIELD_NAME 
     1from django.contrib.auth import REDIRECT_FIELD_NAME 
    22from django.http import HttpResponseRedirect 
    33from urllib import quote 
    44 
    5 def user_passes_test(test_func, login_url=LOGIN_URL): 
     5def user_passes_test(test_func, login_url=None): 
    66    """ 
    77    Decorator for views that checks that the user passes the given test, 
     
    99    that takes the user object and returns True if the user passes. 
    1010    """ 
     11    if not login_url: 
     12        from django.conf import settings 
     13        login_url = settings.LOGIN_URL 
    1114    def _dec(view_func): 
    1215        def _checklogin(request, *args, **kwargs): 
     
    2831    ) 
    2932 
    30 def permission_required(perm, login_url=LOGIN_URL): 
     33def permission_required(perm, login_url=None): 
    3134    """ 
    3235    Decorator for views that checks whether a user has a particular permission 
  • django/trunk/django/contrib/auth/__init__.py

    r4265 r5072  
    33SESSION_KEY = '_auth_user_id' 
    44BACKEND_SESSION_KEY = '_auth_user_backend' 
    5 LOGIN_URL = '/accounts/login/' 
    65REDIRECT_FIELD_NAME = 'next' 
    76 
  • django/trunk/django/contrib/auth/views.py

    r4486 r5072  
    77from django.http import HttpResponseRedirect 
    88from django.contrib.auth.decorators import login_required 
    9 from django.contrib.auth import LOGIN_URL, REDIRECT_FIELD_NAME 
     9from django.contrib.auth import REDIRECT_FIELD_NAME 
    1010 
    1111def login(request, template_name='registration/login.html'): 
     
    1818            # Light security check -- make sure redirect_to isn't garbage. 
    1919            if not redirect_to or '://' in redirect_to or ' ' in redirect_to: 
    20                 redirect_to = '/accounts/profile/' 
     20                from django.conf import settings 
     21                redirect_to = settings.LOGIN_REDIRECT_URL 
    2122            from django.contrib.auth import login 
    2223            login(request, manipulator.get_user()) 
     
    4243        return HttpResponseRedirect(next_page or request.path) 
    4344 
    44 def logout_then_login(request, login_url=LOGIN_URL): 
     45def logout_then_login(request, login_url=None): 
    4546    "Logs out the user if he is logged in. Then redirects to the log-in page." 
     47    if not login_url: 
     48        from django.conf import settings 
     49        login_url = settings.LOGIN_URL 
    4650    return logout(request, login_url) 
    4751 
    48 def redirect_to_login(next, login_url=LOGIN_URL): 
     52def redirect_to_login(next, login_url=None): 
    4953    "Redirects the user to the login page, passing the given 'next' page" 
     54    if not login_url: 
     55        from django.conf import settings 
     56        login_url = settings.LOGIN_URL 
    5057    return HttpResponseRedirect('%s?%s=%s' % (login_url, REDIRECT_FIELD_NAME, next)) 
    5158 
  • django/trunk/django/contrib/comments/templates/comments/form.html

    r3360 r5072  
    44 
    55{% if user.is_authenticated %} 
    6 <p>{% trans "Username:" %} <strong>{{ user.username }}</strong> (<a href="/accounts/logout/">{% trans "Log out" %}</a>)</p> 
     6<p>{% trans "Username:" %} <strong>{{ user.username }}</strong> (<a href="{{ logout_url }}">{% trans "Log out" %}</a>)</p> 
    77{% else %} 
    88<p><label for="id_username">{% trans "Username:" %}</label> <input type="text" name="username" id="id_username" /><br />{% trans "Password:" %} <input type="password" name="password" id="id_password" /> (<a href="/accounts/password_reset/">{% trans "Forgotten your password?" %}</a>)</p> 
  • django/trunk/django/contrib/comments/templatetags/comments.py

    r3360 r5072  
    2626 
    2727    def render(self, context): 
     28        from django.conf import settings 
    2829        from django.utils.text import normalize_newlines 
    2930        import base64 
     
    6566                context['rating_range'], context['rating_choices'] = Comment.objects.get_rating_options(self.rating_options) 
    6667            context['hash'] = Comment.objects.get_security_hash(context['options'], context['photo_options'], context['rating_options'], context['target']) 
     68            context['logout_url'] = settings.LOGOUT_URL 
    6769            default_form = loader.get_template(COMMENT_FORM) 
    6870        output = default_form.render(context) 
  • django/trunk/docs/authentication.txt

    r5064 r5072  
    388388``login_required`` does the following: 
    389389 
    390     * If the user isn't logged in, redirect to ``/accounts/login/``, passing 
    391       the current absolute URL in the query string as ``next``. For example: 
     390    * If the user isn't logged in, redirect to ``settings.LOGIN_URL`` 
     391      (``/accounts/login/`` by default), passing the current absolute URL 
     392      in the query string as ``next``. For example: 
    392393      ``/accounts/login/?next=/polls/3/``. 
    393394    * If the user is logged in, execute the view normally. The view code is 
    394395      free to assume the user is logged in. 
    395396 
    396 Note that you'll need to map the appropriate Django view to ``/accounts/login/``. 
    397 To do this, add the following line to your URLconf:: 
     397Note that you'll need to map the appropriate Django view to ``settings.LOGIN_URL``. 
     398For example, using the defaults, add the following line to your URLconf:: 
    398399 
    399400    (r'^accounts/login/$', 'django.contrib.auth.views.login'), 
     
    406407    * If called via ``POST``, it tries to log the user in. If login is 
    407408      successful, the view redirects to the URL specified in ``next``. If 
    408       ``next`` isn't provided, it redirects to ``/accounts/profile/`` (which is 
    409       currently hard-coded). If login isn't successful, it redisplays the login 
    410       form. 
     409      ``next`` isn't provided, it redirects to ``settings.LOGIN_REDIRECT_URL`` 
     410      (which defaults to ``/accounts/profile/``). If login isn't successful, 
     411      it redisplays the login form. 
    411412 
    412413It's your responsibility to provide the login form in a template called 
     
    488489 
    489490    * ``login_url``: The URL of the login page to redirect to. This 
    490       will default to ``/accounts/login/`` if not supplied. 
     491      will default to ``settings.LOGIN_URL`` if not supplied. 
    491492 
    492493``django.contrib.auth.views.password_change`` 
     
    570571 
    571572    * ``login_url``: The URL of the login page to redirect to. This 
    572       will default to ``/accounts/login/`` if not supplied. 
     573      will default to ``settings.LOGIN_URL`` if not supplied. 
    573574 
    574575Built-in manipulators 
     
    637638 
    638639``user_passes_test()`` takes an optional ``login_url`` argument, which lets you 
    639 specify the URL for your login page (``/accounts/login/`` by default). 
     640specify the URL for your login page (``settings.LOGIN_URL`` by default). 
    640641 
    641642Example in Python 2.3 syntax:: 
     
    681682 
    682683As in the ``login_required`` decorator, ``login_url`` defaults to 
    683 ``'/accounts/login/'``. 
     684``settings.LOGIN_URL``. 
    684685 
    685686Limiting access to generic views 
  • django/trunk/docs/settings.txt

    r5064 r5072  
    563563any code that uses ``LANGUAGES`` at runtime. 
    564564 
     565LOGIN_URL 
     566--------- 
     567 
     568Default: ``'/accounts/login/'`` 
     569 
     570The URL where requests are redirected for login, specially when using the 
     571`@login_required`_ decorator. 
     572 
     573LOGOUT_URL 
     574---------- 
     575 
     576Default: ``'/accounts/logout/'`` 
     577 
     578LOGIN_URL counterpart. 
     579 
    565580MANAGERS 
    566581-------- 
     
    620635See `allowed date format strings`_. See also DATE_FORMAT, DATETIME_FORMAT, 
    621636TIME_FORMAT and YEAR_MONTH_FORMAT. 
     637 
     638LOGIN_REDIRECT_URL 
     639------------------ 
     640 
     641Default: ``'/accounts/profile/'`` 
     642 
     643The URL where requests are redirected after login when the 
     644``contrib.auth.login`` view gets no ``next`` parameter. 
     645 
     646This is used by the `@login_required`_ decorator, for example. 
    622647 
    623648PREPEND_WWW 
     
    10131038``DJANGO_SETTINGS_MODULE``. Not both, and not neither. 
    10141039 
     1040.. _@login_required: ../authentication/#the-login-required-decorator 
     1041 
    10151042Error reporting via e-mail 
    10161043==========================