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 | # myapp.models.__init__.py
from elixir import metadata, objectstore
from pylons.database import create_engine, make_session
session_context = objectstore.context
engine = None
# call this with every thread
def resync():
session_context.current = make_session()
# connects engine to metadata
def connect():
global engine
if not engine:
engine = create_engine()
metadata.connect(engine)
# writes all changes to db
def flush_all():
objectstore.flush()
# connect automatically if possible
# this won't work if config not set at import time,
# for example in websetup.py, so must be called
# manually
try:
connect()
except:
pass
# myapp.lib.base.py
class BaseController(WSGIController):
def __call__(self, environ, start_response):
model.resync()
return WSGIController.__call__(self, environ, start_response)
# websetup.py
def setup_config(command, filename, section, vars):
"""
Place any commands to setup testrig here.
"""
conf = paste.deploy.appconfig('config:' + filename)
conf.update(dict(app_conf=conf.local_conf, global_conf=conf.global_conf))
paste.deploy.CONFIG.push_process_config(conf)
from myapp import models as model
model.connect()
model.metadata.drop_all()
model.metadata.create_all()
|