Version 2 (modified by guest, 5 years ago)

added some pigments

Here's a short recipe in order to get a Pylons-like controller class working with Elixir.

I'll try adding details here as time goes by, updating the page, etc.

Ah yeah, optionally, I've decided using webob for this example. I figure you can use whatever WSGI adapter that fits for your environment.

In particular, notice the init_session and __call__ function. All the rest is kinda optional or can be modified to best suit your base.

- erob (18/12/2008)

#!/usr/bin/env python
# Copyright (c) Etienne Robillard 2008 <robillard.etienne@gmail.com>
# This file is part of the notmm project:
# http://gthc.org/projects/notmm/

import elixir
#import webob

from notmm.controllers.base import BaseController
from notmm.wsgi.utils import render_response
from notmm.utils.sql.session import ScopedSession as scoped_session

from tm.config.database import engine

class ElixirController(BaseController):
    """ 
    A `BaseController` subclass which supports contextual sessions
    on top of Elixir.
    """
    
    def __init__(self, request_class, response_class):
        BaseController.__init__(self, request_class, response_class)
        self.engine = engine
 
    def handle404(self, request):
        """ handle 404 errors here """
        template_name = '404.mako'
        return render_response(request, template_name, status_code=404)
    
    def init_session(self, engine, shortnames=True):
        """ Prepare the session stuff with Elixir """
        scoped_session.set_session(engine, autoflush=True, autocommit=False)
    
        ## replace the elixir session with our own
        ## http://cleverdevil.org/computing/68/
        elixir.session = scoped_session.get_session()
        elixir.options_defaults.update(dict(shortnames=bool(shortnames)))
    
        # setup_all 
        if not elixir.metadata.is_bound(): 
            elixir.metadata.bind = engine
    
        # create tables if they're missing...
        # meta.create_all()

    def __call__(self, environ, start_response):
        
        self.init_session(self.engine)
        try:
            return self.application(environ, start_response)
        finally:
            elixir.session.close()