Code

Ticket #903: patch_login_required.diff

File patch_login_required.diff, 3.6 KB (added by vbmendes, 5 years ago)

Adds login_url parameter for login_required decorator and redirect_field_name parameter for permission_required decorator.

Line 
1Index: django/contrib/auth/decorators.py
2===================================================================
3--- django/contrib/auth/decorators.py   (revisão 9845)
4+++ django/contrib/auth/decorators.py   (cópia de trabalho)
5@@ -17,25 +17,26 @@
6         return _CheckLogin(view_func, test_func, login_url, redirect_field_name)
7     return decorate
8 
9-def login_required(function=None, redirect_field_name=REDIRECT_FIELD_NAME):
10+def login_required(function=None, redirect_field_name=REDIRECT_FIELD_NAME, login_url=None):
11     """
12     Decorator for views that checks that the user is logged in, redirecting
13     to the log-in page if necessary.
14     """
15     actual_decorator = user_passes_test(
16         lambda u: u.is_authenticated(),
17-        redirect_field_name=redirect_field_name
18+        login_url,
19+        redirect_field_name
20     )
21     if function:
22         return actual_decorator(function)
23     return actual_decorator
24 
25-def permission_required(perm, login_url=None):
26+def permission_required(perm, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME):
27     """
28     Decorator for views that checks whether a user has a particular permission
29     enabled, redirecting to the log-in page if necessary.
30     """
31-    return user_passes_test(lambda u: u.has_perm(perm), login_url=login_url)
32+    return user_passes_test(lambda u: u.has_perm(perm), login_url, redirect_field_name)
33 
34 class _CheckLogin(object):
35     """
36Index: docs/topics/auth.txt
37===================================================================
38--- docs/topics/auth.txt        (revisão 9845)
39+++ docs/topics/auth.txt        (cópia de trabalho)
40@@ -673,13 +673,34 @@
41         @login_required(redirect_field_name='redirect_to')
42         def my_view(request):
43             # ...
44+   
45+    :func:`~django.contrib.auth.decorators.login_required` also takes an
46+    optional ``login_url`` argument, which lets you specify the URL for
47+    your login page (settings.LOGIN_URL by default).
48+   
49+    Example in Python 2.3 syntax::
50+   
51+        from django.contrib.auth.decorators import login_required
52+       
53+        def my_view(request):
54+            # ...
55+        my_view = login_required(login_url='/login/')(my_view)
56+   
57+    Example in Python 2.4 syntax::
58+   
59+        from django.contrib.auth.decorators import login_required
60+       
61+        @login_required(login_url='/login/')
62+        def my_view(request):
63+            # ...
64 
65     :func:`~django.contrib.auth.decorators.login_required` does the following:
66 
67-        * If the user isn't logged in, redirect to
68-          :setting:`settings.LOGIN_URL <LOGIN_URL>` (``/accounts/login/`` by
69-          default), passing the current absolute URL in the query string as
70-          ``next`` or the value of ``redirect_field_name``. For example:
71+        * If the user isn't logged in, redirect to ``login_url`` value, wich
72+          defaults to :setting:`settings.LOGIN_URL <LOGIN_URL>`
73+          (``/accounts/login/`` by default), passing the current absolute URL
74+          in the query string as ``next`` or the value of
75+          ``redirect_field_name``. For example:
76           ``/accounts/login/?next=/polls/3/``.
77 
78         * If the user is logged in, execute the view normally. The view code is
79@@ -994,6 +1015,10 @@
80 
81     As in the :func:`~decorators.login_required` decorator, ``login_url``
82     defaults to :setting:`settings.LOGIN_URL <LOGIN_URL>`.
83+   
84+    :func:`~decorators.permission_required` decorator, also as in the
85+    :func:`~decorators.login_required` decorator, takes an optional parameter
86+    ``redirect_field_name``.
87 
88 Limiting access to generic views
89 --------------------------------