[mod_python] Getting request object

Radek Bartoň blackhex at post.cz
Thu Dec 8 16:18:02 EST 2005


> You might need to be running mod_python 3.2.5b for it to work, but I
> would not
> have expected that to be the case. This code was actual code I tried
> and tested
> before I sent it, albeit on mod_python 3.2.5b.

I have installed 3.1.4 from gentoo's portage.

> 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.

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()

Thank you all.


More information about the Mod_python mailing list