Pylons + Elixir

I use Pylons and Elixir from trunk (both). Paster create -t pylons … generate most of code for SQLAlchemy so in order to use Elixir, I just made small modification following http://cleverdevil.org/computing/68/ and http://elixir.ematia.de/trac/wiki/TutorialDivingIn. In the example, meta.py has not been modified but I try to modify meta.py too. Also I put a lot of thing in function “init_model”. I don’t know whether these will casue problems … but as least, it works with a small example😛.

In my example, the project name is “mydb4”. Firstly, I modified mydb4/model/meta.py:

 engine = None 

Secondly, mydb4/model/__init__.py was modfied. In this file, I put also the class Person, which declares the table in the database.

import sqlalchemy as sa
from sqlalchemy import orm
from sqlalchemy.orm import scoped_session, sessionmaker
import meta
import elixir

from unlalign.model import meta

sm = orm.sessionmaker(autoflush=True, autocommit=True)
meta.Session = orm.scoped_session(sm)
elixir.session = meta.Session
elixir.options_defaults.update({ 'shortnames': True })

def init_model(engine):
    meta.engine = engine
    elixir.metadata.bind = engine

from elixir import *

class Person(Entity):
    name = Field(String(100))

elixir.setup_all()

mydb4/websetup.py has been edited also:

"""Setup the mydb4 application"""
import logging

from mydb4.config.environment import load_environment

log = logging.getLogger(__name__)

def setup_app(command, conf, vars):
    """Place any commands to setup mydb4 here"""
    load_environment(conf.global_conf, conf.local_conf)

    # Load the models
    from elixir import metadata
    from mydb4.model import meta

    metadata.bind = meta.engine

    # Create the tables if they aren't there already
    metadata.create_all(checkfirst=True)

The controller (mydb4/controller/person.py) looks like below:

import logging

from pylons import request, response, session
from pylons import tmpl_context as c
from pylons.controllers.util import abort, redirect_to

from mydb4.lib.base import BaseController
import mydb4.model as model

log = logging.getLogger(__name__)

class PersonController(BaseController):

    def index(self):
        # Return a rendered template
        #   return render('/template.mako')
        # or, Return a response
        return ",".join([i.name for i in model.Person.query.all()])

We have to modify also mydb4/lib/base.py

"""The base Controller API

Provides the BaseController class for subclassing.
"""
from pylons.controllers import WSGIController
from pylons.templating import render_mako as render

from mydb4.model import meta
import elixir

class BaseController(WSGIController):

    def __call__(self, environ, start_response):
        """Invoke the Controller"""
        # WSGIController.__call__ dispatches to the Controller method
        # the request is routed to. This routing information is
        # available in environ['pylons.routes_dict']
        try:
            return WSGIController.__call__(self, environ, start_response)
        finally:
            elixir.session.remove()

At the shell, command “paster setup-app development.ini” was called, then it created database correctly. After database was created, data are insert into table manually by “sqlite3 development.db” … then

insert into mydb4_model_person(id,name) values (1, "titi");
insert into mydb4_model_person(id,name) values (2, "tata");

After running “paster serve –reload development.ini”, then visit the url “http://localhost:5000/person/index”, it looks like this screen capture below:

One thought on “Pylons + Elixir

ใส่ความเห็น

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / เปลี่ยนแปลง )

Twitter picture

You are commenting using your Twitter account. Log Out / เปลี่ยนแปลง )

Facebook photo

You are commenting using your Facebook account. Log Out / เปลี่ยนแปลง )

Google+ photo

You are commenting using your Google+ account. Log Out / เปลี่ยนแปลง )

Connecting to %s