Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

magic-removal: Fixed #1488 -- request.user is no longer read-only. Al…

…so improved it to only hit the session database table if a user is requested.

git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@2590 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 02b3f4489dbbbffc16cd43efaf917d688326f8d2 1 parent e7a4763
Adrian Holovaty authored March 28, 2006

Showing 1 changed file with 15 additions and 25 deletions. Show diff stats Hide diff stats

  1. 40  django/contrib/auth/middleware.py
40  django/contrib/auth/middleware.py
... ...
@@ -1,32 +1,22 @@
1  
-class UserWrapper(object):
2  
-    """
3  
-    Proxy to lazily load a user object.
4  
-    """
5  
-    def __init__(self, login):
6  
-        self._login = login
7  
-        self._cached_user = None
  1
+class LazyUser(object):
  2
+    def __init__(self, session):
  3
+        self.session = session
  4
+        self._user = None
8 5
 
9  
-    def _get_user(self):
10  
-        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
14  
-
15  
-    _user = property(_get_user)
16  
-
17  
-    def __getattr__(self, name):
18  
-        if name == '__setstate__': # slight hack to allow object to be unpickled
19  
-            return None 
20  
-        return getattr(self._user, name)
  6
+    def __get__(self, request, obj_type=None):
  7
+        if self._user is None:
  8
+            from django.contrib.auth.models import User, AnonymousUser, SESSION_KEY
  9
+            try:
  10
+                user_id = self.session[SESSION_KEY]
  11
+                self._user = User.objects.get(pk=user_id)
  12
+            except (KeyError, User.DoesNotExist):
  13
+                self._user = AnonymousUser()
  14
+            del self.session # We don't need to keep this around anymore.
  15
+        return self._user
21 16
 
22 17
 class AuthenticationMiddleware:
23 18
     def process_request(self, request):
24 19
         from django.contrib.auth.models import SESSION_KEY
25 20
         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'."
26  
-        try:
27  
-            user_id = request.session[SESSION_KEY]
28  
-            request.user = UserWrapper(user_id)
29  
-        except KeyError:
30  
-            from django.contrib.auth.models import AnonymousUser
31  
-            request.user = AnonymousUser()
  21
+        request.__class__.user = LazyUser(request.session)
32 22
         return None

0 notes on commit 02b3f44

Please sign in to comment.
Something went wrong with that request. Please try again.