def run(run=True): global app SqlaNamespaceManager._init_dependencies() setattr(SqlaNamespaceManager, 'lock_dir', None) session_opts = { 'session.type': 'ext:sqla', 'session.bind': kernel.db.engine, 'session.table': make_cache_table(MetaData()), 'session.cookie_expires': False, 'session.auto': True} class BeforeRequestMiddleware(object): def __init__(self, app): self.app = app def __call__(self, e, h): e['PATH_INFO'] = e['PATH_INFO'].rstrip('/') return self.app(e, h) Jinja2Template.defaults = { 'widget': loadWidget, 'is_ajax': is_ajax, 'modules': kernel.module.modules, 'request': request, 't': translate, 'translate': translate, 'datetime': datetime, 'translator': kernel.translator } Jinja2Template.settings = { 'filters': { 'floatint': lambda num: num if isinstance(num, int) else '%.1f' % num, 'e_quotes': lambda text: text.replace('"', '"').replace("'", "'") if text else text, 'announcement': lambda text: str(text).split('<!-- page break -->')[0] if text else '', } } @app.route('/static/<path:path>') def static(path): return static_file(path, './templates/default/static/') @app.route('/file/<path:path>') @app.route('/files/<path:path>') def file(path): path = re.sub(r'(.*)\._(.*)_\.(.*)', r'\1.\3', path) return static_file(path, './files/') @app.post('/widget/<name:path>') def widget(name): try: data = request.json['data'] if request.json is not None and 'data' in request.json else {} return loadWidget(name, data, wrap=False) except ValueError: bottle.response.status = 404 @app.error(404) def error404(error): return template("404") @app.hook('before_request') def before_request(): request.session = request.environ.get('beaker.session') request.db = kernel.db.Database() request.user = User(request.session, request.db) @app.hook('after_request') def after_request(): if hasattr(request, 'db'): request.db.close() app = BeforeRequestMiddleware(app) app = SessionMiddleware(app, session_opts) if run: bottle.run(app, host='192.168.1.2', port=3000)
try: from webtest import TestApp except ImportError: TestApp = None try: clsmap["ext:sqla"]._init_dependencies() except InvalidCacheBackendError: raise SkipTest("an appropriate SQLAlchemy backend is not installed") import sqlalchemy as sa from beaker.ext.sqla import make_cache_table engine = sa.create_engine("sqlite://") metadata = sa.MetaData() cache_table = make_cache_table(metadata) metadata.create_all(engine) def simple_app(environ, start_response): extra_args = {} clear = False if environ.get("beaker.clear"): clear = True extra_args["type"] = "ext:sqla" extra_args["bind"] = engine extra_args["table"] = cache_table extra_args["data_dir"] = "./cache" cache = environ["beaker.cache"].get_cache("testcache", **extra_args) if clear: cache.clear()
try: from webtest import TestApp except ImportError: TestApp = None try: clsmap['ext:sqla']._init_dependencies() except InvalidCacheBackendError: raise SkipTest("an appropriate SQLAlchemy backend is not installed") import sqlalchemy as sa from beaker.ext.sqla import make_cache_table engine = sa.create_engine('sqlite://') metadata = sa.MetaData() cache_table = make_cache_table(metadata) metadata.create_all(engine) def simple_app(environ, start_response): extra_args = {} clear = False if environ.get('beaker.clear'): clear = True extra_args['type'] = 'ext:sqla' extra_args['bind'] = engine extra_args['table'] = cache_table extra_args['data_dir'] = './cache' cache = environ['beaker.cache'].get_cache('testcache', **extra_args) if clear: cache.clear() try: