[mod_python] Getting request object

Graham Dumpleton grahamd at dscpl.com.au
Thu Dec 8 17:43:53 EST 2005


Radek =?utf-8?q?Barto=C5=88?= wrote ..
> > If using mod_python.publisher, as:
> >
> >    index = MyClass
> >
> > you should I believe get NotFound as it will not automatically create
> > instances
> > of classes for you if a class type is referenced. I know Nicolas hand
> > it in mind
> > to perhaps support this, but personally I don't think it is a good
> > idea as it could
> > introduce other problems where people think methods are not
> > accessible but
> > may actually. The authorisation schemes as written also would not
> > work for the
> > instance created. Ie., authorisation could only be done at file scope.
> 
> With file test.py:
> 
> class MyClass:
>   def __init__(self):
>     pass # some initialization
>   def __call__(self, req):
>     return "whole class"
>   def method1(self, req):
>     return "method1"
>   def method2(self, req):
>     return "method2"
> 
> index = MyClass()
> 
> and URLs http://locahost/test/ http://localhost/test/index/method1 and
> http://localhost/test/index/method2 I didn't get any Not Found error.

That is different to what you originally had and not what I was talking about.
It may have been a typo, but I believe you had at one point:

  index = MyClass

not:

  index = MyClass()

There is a big difference. In one case you are publishing the class itself and
in the other and instance of the class. My description above was talking about
publication of a class verses publication of an instance of the class.

> And by the way it does all I want. Here is some example of using this 
> approach:
> 
> from mod_python import Session,util
> 
> class MyClass:
>   def __init__(self):
>     pass # some initialization
>   def __call__(self, req):
>     session = Session.Session(req)
>     # lazy evaluation
>     if session.has_key('logged') and session['logged']:
>       return """
>         <HTML>
> 	<HEAD>
> 	</HEAD>
> 	<BODY> 
>           Logged
> 	</BODY>
> 	</HTML>"""
>     else: 	  
>       return """
>         <HTML>
> 	<HEAD>
> 	</HEAD>
> 	<BODY>
>         <FORM method="POST" action="/test/index/submit">
>           <INPUT test="text" name="user" size="10"><BR>
>           <INPUT TYPE="password" name="password" size="10"><BR>
>           <INPUT TYPE="submit" name="submint" value="Login">
>         </FORM>
> 	</BODY>
> 	</HTML>"""
>   def submit(self, req):
>     form_user = req.form['user'].value
>     form_password = req.form['password'].value
>     session = Session.Session(req)
>     if form_user == 'test' and form_password == 'test':
>       session['logged'] = True
>       session.save()
>     util.redirect(req, '/test') 
> 
> index = MyClass()

So in other words you didn't actually need access to the request object
in the constructor after all. What you were missing was that __call__()
could be used in place of __str__() with __call__() allowing you to still
access the request object and form parameters if need be.

Graham


More information about the Mod_python mailing list