Hello site
[auf_framonde.git] / eggs / Django-1.4.5-py2.7.egg / django / contrib / auth / context_processors.py
1 # PermWrapper and PermLookupDict proxy the permissions system into objects that
2 # the template system can understand.
3
4 class PermLookupDict(object):
5 def __init__(self, user, module_name):
6 self.user, self.module_name = user, module_name
7
8 def __repr__(self):
9 return str(self.user.get_all_permissions())
10
11 def __getitem__(self, perm_name):
12 return self.user.has_perm("%s.%s" % (self.module_name, perm_name))
13
14 def __iter__(self):
15 # To fix 'item in perms.someapp' and __getitem__ iteraction we need to
16 # define __iter__. See #18979 for details.
17 raise TypeError("PermLookupDict is not iterable.")
18
19 def __nonzero__(self):
20 return self.user.has_module_perms(self.module_name)
21
22
23 class PermWrapper(object):
24 def __init__(self, user):
25 self.user = user
26
27 def __getitem__(self, module_name):
28 return PermLookupDict(self.user, module_name)
29
30 def __iter__(self):
31 # I am large, I contain multitudes.
32 raise TypeError("PermWrapper is not iterable.")
33
34 def auth(request):
35 """
36 Returns context variables required by apps that use Django's authentication
37 system.
38
39 If there is no 'user' attribute in the request, uses AnonymousUser (from
40 django.contrib.auth).
41 """
42 if hasattr(request, 'user'):
43 user = request.user
44 else:
45 from django.contrib.auth.models import AnonymousUser
46 user = AnonymousUser()
47
48 return {
49 'user': user,
50 'perms': PermWrapper(user),
51 }