Pylons

Feb 24, 2009 3:32:12 PM

Pylons 0.9.7 + Elixir 0.6.1

Language: Python
Tags: elixir
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# Pylons 0.9.7 + Elixir 0.6.1
# This is just what works for me. YMMV.

# I have all my Entities in model/entities.py (nothing special there)

# model/__init__.py
"""The application's model objects"""
from sqlalchemy import orm
import elixir

# you may want different args to sessionmaker here:
Session = orm.scoped_session(orm.sessionmaker(autoflush=False, expire_on_commit=False))
elixir.session = Session
# next line is optional, and you may want different options:
elixir.options_defaults.update(dict(shortnames=True, inheritance='multi', polymorphic=True))
metadata = elixir.metadata
from entities import *
Session = elixir.session

def init_model(engine):
    """Call me before using any of the tables or classes in the model"""
    metadata.bind = engine

# environment.py

# in the imports section:
import yourapp.model as model
from sqlalchemy import engine_from_config

# then replace the SQLAlchemy setup section with:
    
    # Setup Elixir
    # note: in this example, I'm leaving connect_args empty, fill in what is right
    # for your app.
    config['pylons.app_globals'].sa_engine = \
        engine_from_config(config, 'sqlalchemy.',
                           connect_args={}, pool_recycle=90)
    elixir.bind = config['pylons.app_globals'].sa_engine
    model.metadata.bind = config['pylons.app_globals'].sa_engine
    elixir.setup_all()

# lib/base.py --------------------------------------------------------

# imports section:
import yourapp.model as model

# change BaseController like so:

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:
            model.Session.remove()

Download

Comments (0)

You must login before you can comment.

Powered by Pylons - Contact Administrators