=== django/contrib/auth/middleware.py
==================================================================
|
|
|
1 | | class UserWrapper(object): |
| 1 | class LazyUser(object): |
2 | 2 | """ |
3 | | Proxy to lazily load a user object. |
| 3 | Descriptor to lazily load a user object. |
4 | 4 | """ |
5 | | def __init__(self, login): |
6 | | self._login = login |
7 | | self._cached_user = None |
| 5 | def __init__(self, user_id): |
| 6 | self.user_id = user_id |
8 | 7 | |
9 | | def _get_user(self): |
| 8 | def __get__(self, request, obj_type=None): |
10 | 9 | from django.contrib.auth.models import User |
11 | | if not self._cached_user: |
12 | | self._cached_user = User.objects.get(pk=self._login) |
13 | | return self._cached_user |
| 10 | request.user = User.objects.get(pk=self.user_id) |
| 11 | return request.user |
14 | 12 | |
15 | | _user = property(_get_user) |
16 | | |
17 | | def __getattr__(self, name): |
18 | | return getattr(self._user, name) |
19 | | |
20 | 13 | class AuthenticationMiddleware: |
21 | 14 | def process_request(self, request): |
22 | 15 | from django.contrib.auth.models import SESSION_KEY |
23 | 16 | assert hasattr(request, 'session'), "The Django authentication middleware requires session middleware to be installed. Edit your MIDDLEWARE_CLASSES setting to insert 'django.contrib.sessions.middleware.SessionMiddleware'." |
24 | 17 | try: |
25 | 18 | user_id = request.session[SESSION_KEY] |
26 | | request.user = UserWrapper(user_id) |
| 19 | request.__class__.user = LazyUser(user_id) |
27 | 20 | except KeyError: |
28 | 21 | from django.contrib.auth.models import AnonymousUser |
29 | 22 | request.user = AnonymousUser() |