[mod_python] Pre and postprocess blocks (Publisher mode)?

Graham Dumpleton graham.dumpleton at gmail.com
Fri May 4 05:50:59 EDT 2007


On 04/05/07, durumdara <durumdara at gmail.com> wrote:
> Hi!
>
> I tried Pylons to see and check it... how to working, what I need to
> learn about it... from it... :-)
>
> http://groups.google.com/group/pylons-discuss/browse_thread/thread/8870ed10d821c06f
>
> I probably ported my modpy site to pylons-type site.
>
> One thing I like in it: it have preprocess, and postprocess phase where
> I define the common operations.
>
> Example: session, database handling, right/access checking, etc.
>
> So pylons is starts with _before_ procedure, what defined in base
> controller and "overrideable" in any controllers; and next it is
> traverses the path to controller/action.
> Then it is calls the actual "controller object"'s actual function (named
> as action). This procedure is get back a Response object.
> Finally it is call the _after_ procedure, and finished with request.
>
> Because the controllers are python modules, it is avoid the "same module
> name" hell (in another modules I can use same name - example index).

What exactly are you talking about when you say 'same module name hell'?

Note that packages like Pylons, Django etc rely on standard module
importing mechanism so module names have to be unique or contained
with a package structure. The mod_python module importer supports
being able to load modules based on absolute path as well, thus
allowing same module names in multiple contexts and still support
module reloading. See the apache.import_module() function described
in:

  http://www.modpython.org/live/current/doc-html/pyapi-apmeth.html

> I know that modpy supports publisher mode, that same like pylons'
> working method.
>
> 1.)
> But I want to know: how the modpython helps me to I do pre- and
> postprocess operations?

Apache/mod_python actually has a much better developed concept of
request processing phases than Pylons and other monolithic solutions
such as Django. Have a look at the section '4.4.4 Process HTTP
Request' in:

  http://www.fmc-modeling.org/projects/apache/html/4_4Request_Response_Loop.html

In mod_python most of the phases can have code associated with them. See:

  http://www.modpython.org/live/current/doc-html/dir-handlers.html

A post handler or cleanup handler can be registered using the
req.register_cleanup() method. See documentation in:

  http://www.modpython.org/live/current/doc-html/pyapi-mprequest-meth.html

Note that you can also define stacked handlers for PythonHandler as
another means of implementing a request in multiple phases.

One can also use other Apache modules to implement some phases, thus
avoiding duplication of effort.

> 2.)
> Can I get paths/pages without extensions like in pylons?
> Example:
> www.somesite.com/main
> www.somesite.com/search
> And not:
> www.somesite.com/main.py
> www.somesite.com/search.py

Yes. See discussion of SetHandler/AddHandler differences and
MultiViews feature of Apache in:

  http://www.dscpl.com.au/wiki/ModPython/Articles/SetHandlerVersusAddHandler

> In publisher mode how the paths are working?
> When I have a module named main.py that have many subfunctions, what
> path I need to use?
> www.somesite.com/main.py/index
> www.somesite.com/main.py/search
> or
> www.somesite.com/main/index
> www.somesite.com/main/search
> ?

Either if you set up the Apache configuration correctly.

Also read documentation:

  http://www.modpython.org/live/current/doc-html/hand-pub-alg.html

Overall, Apache/mod_python is probably more flexible and through use
of other Apache modules can be controlled to a much higher degree
without having to write too much custom code for URL dispatching which
is just going to bloat your application.

So, read through all those references and try and take it in. Then
perhaps come back with more specific questions with examples of what
you have tried.

Graham


More information about the Mod_python mailing list