Code

Ticket #15299: 15299.diff

File 15299.diff, 3.6 KB (added by v1v3kn, 3 years ago)
Line 
1Index: docs/topics/auth.txt
2===================================================================
3--- docs/topics/auth.txt        (revision 15550)
4+++ docs/topics/auth.txt        (working copy)
5@@ -1334,7 +1334,7 @@
6 
7 The currently logged-in user's permissions are stored in the template variable
8 ``{{ perms }}``. This is an instance of
9-:class:`django.core.context_processors.PermWrapper`, which is a
10+:class:`django.contrib.auth.context_processors.PermWrapper`, which is a
11 template-friendly proxy of permissions.
12 
13 In the ``{{ perms }}`` object, single-attribute lookup is a proxy to
14Index: docs/ref/templates/api.txt
15===================================================================
16--- docs/ref/templates/api.txt  (revision 15550)
17+++ docs/ref/templates/api.txt  (working copy)
18@@ -423,9 +423,13 @@
19       via the :doc:`messages framework </ref/contrib/messages>`.
20 
21     * ``perms`` -- An instance of
22-      ``django.core.context_processors.PermWrapper``, representing the
23+      ``django.contrib.auth.context_processors.PermWrapper``, representing the
24       permissions that the currently logged-in user has.
25 
26+.. versionchanged:: 1.3
27+    ``perms`` was moved in this release from
28+    ``django.core.context_processors.auth`` to its current location.
29+
30 .. versionchanged:: 1.2
31     This context processor was moved in this release from
32     ``django.core.context_processors.auth`` to its current location.
33Index: django/core/context_processors.py
34===================================================================
35--- django/core/context_processors.py   (revision 15550)
36+++ django/core/context_processors.py   (working copy)
37@@ -86,6 +86,9 @@
38 # PermWrapper and PermLookupDict proxy the permissions system into objects that
39 # the template system can understand.
40 
41+# Deprecation Warning: PermWrapper would be accessible from django.contrib.auth.context_processors from version 1.3
42+# and it would be removed from django.core.context_processors in future versions.
43+
44 class PermLookupDict(object):
45     def __init__(self, user, module_name):
46         self.user, self.module_name = user, module_name
47@@ -99,6 +102,7 @@
48     def __nonzero__(self):
49         return self.user.has_module_perms(self.module_name)
50 
51+
52 class PermWrapper(object):
53     def __init__(self, user):
54         self.user = user
55Index: django/contrib/auth/context_processors.py
56===================================================================
57--- django/contrib/auth/context_processors.py   (revision 15550)
58+++ django/contrib/auth/context_processors.py   (working copy)
59@@ -1,7 +1,34 @@
60-from django.core.context_processors import PermWrapper
61 from django.utils.functional import lazy, memoize, SimpleLazyObject
62 from django.contrib import messages
63 
64+# PermWrapper and PermLookupDict proxy the permissions system into objects that
65+# the template system can understand.
66+
67+class PermLookupDict(object):
68+    def __init__(self, user, module_name):
69+        self.user, self.module_name = user, module_name
70+
71+    def __repr__(self):
72+        return str(self.user.get_all_permissions())
73+
74+    def __getitem__(self, perm_name):
75+        return self.user.has_perm("%s.%s" % (self.module_name, perm_name))
76+
77+    def __nonzero__(self):
78+        return self.user.has_module_perms(self.module_name)
79+
80+
81+class PermWrapper(object):
82+    def __init__(self, user):
83+        self.user = user
84+
85+    def __getitem__(self, module_name):
86+        return PermLookupDict(self.user, module_name)
87+
88+    def __iter__(self):
89+        # I am large, I contain multitudes.
90+        raise TypeError("PermWrapper is not iterable.")
91+
92 def auth(request):
93     """
94     Returns context variables required by apps that use Django's authentication