Subclassing TwodWSGIRequest

6 views
Skip to first unread message

Hunter Morris

unread,
Aug 29, 2010, 7:23:35 PM8/29/10
to 2degrees FLOSS
Hi,

I've been trying to create a sub-class of TwodWSGIRequest for use in
my application like so:

class MyRequest(TwodWSGIRequest):
def __init__(self, environ):
environ['my.property'] = None
super(MyRequest, self).__init__(environ)

@property
def user(self):
return environ['my.property']

@user.setter
def user(self, value):
environ['my.property'] = value

I then create a make_application method to be used by Paste:

def make_application(global_conf, **local_conf):
app = wsgify_django(global_conf, **local_conf)
app.request_class = MyRequest
return app

And from within some middleware I do:

request.user = foo()

It never uses my 'user' property as defined in MyRequest. Why is this?

Hunter Morris

unread,
Aug 31, 2010, 9:05:22 AM8/31/10
to 2degrees FLOSS
On Aug 30, 12:23 am, Hunter Morris <huntermor...@gmail.com> wrote:
> It never uses my 'user' property as defined in MyRequest.  Why is this?

After some sleep and more investigation, I've found that it's an
interaction with the LazyUser class attached to the request by
Django's django.contrib.auth.AuthenticationMiddleware which does:

request.__class__.user = LazyUser()

Therefore for now, my workaround involves simply replacing Django's
AuthenticationMiddleware with my own.

Gustavo Narea

unread,
Aug 31, 2010, 9:04:30 AM8/31/10
to 2degree...@googlegroups.com
Hello, Hunter.

Are you using Django's authentication middleware? If so, you'd have to
disable it because this is what it does:

class AuthenticationMiddleware(object):
def process_request(self, request):
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'."
request.__class__.user = LazyUser()
return None


HTH.

--
Gustavo Narea.
Software Developer.
2degrees, Ltd. <http://dev.2degreesnetwork.com/>.

Reply all
Reply to author
Forward
0 new messages