| 1 | | """Pylons environment configuration""" |
|---|
| 2 | | import os |
|---|
| 3 | | |
|---|
| 4 | | {{if template_engine == 'mako'}} |
|---|
| 5 | | from mako.lookup import TemplateLookup |
|---|
| 6 | | {{elif template_engine == 'genshi'}} |
|---|
| 7 | | from genshi.template import TemplateLoader |
|---|
| 8 | | {{elif template_engine == 'jinja'}} |
|---|
| 9 | | from jinja import ChoiceLoader, Environment, FileSystemLoader |
|---|
| 10 | | {{endif}} |
|---|
| 11 | | from pylons import config |
|---|
| 12 | | |
|---|
| 13 | | from pylons.i18n import ugettext |
|---|
| 14 | | from genshi.filters import Translator |
|---|
| 15 | | from tg import defaults |
|---|
| 16 | | |
|---|
| 17 | | {{if sqlalchemy}} |
|---|
| 18 | | from sqlalchemy import engine_from_config |
|---|
| 19 | | {{endif}} |
|---|
| 20 | | |
|---|
| 21 | | import {{package}}.lib.app_globals as app_globals |
|---|
| 22 | | #import {{package}}.lib.helpers |
|---|
| 23 | | #from {{package}}.config.routing import make_map |
|---|
| 24 | | {{if sqlalchemy}} |
|---|
| 25 | | from {{package}}.model import init_model, DBSession, metadata |
|---|
| 26 | | {{endif}} |
|---|
| 27 | | {{if identity=='sqlalchemy'}} |
|---|
| 28 | | from {{package}}.model import User, Group, Permission |
|---|
| 29 | | {{endif}} |
|---|
| 30 | | |
|---|
| 31 | | {{if template_engine == 'genshi'}} |
|---|
| 32 | | def template_loaded(template): |
|---|
| 33 | | "Plug-in our i18n function to Genshi." |
|---|
| 34 | | template.filters.insert(0, Translator(ugettext)) |
|---|
| 35 | | {{endif}} |
|---|
| 36 | | |
|---|
| 37 | | def load_environment(global_conf, app_conf): |
|---|
| 38 | | """Configure the Pylons environment via the ``pylons.config`` |
|---|
| 39 | | object |
|---|
| 40 | | """ |
|---|
| 41 | | # Pylons paths |
|---|
| 42 | | root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
|---|
| 43 | | paths = dict(root=root, |
|---|
| 44 | | controllers=os.path.join(root, 'controllers'), |
|---|
| 45 | | static_files=os.path.join(root, 'public'), |
|---|
| 46 | | templates=[os.path.join(root, 'templates')]) |
|---|
| 47 | | |
|---|
| 48 | | # Initialize config with the basic options |
|---|
| 49 | | config.init_app(global_conf, app_conf, package='{{package}}', paths=paths) |
|---|
| 50 | | |
|---|
| 51 | | # This setups up a set of default route that enables a standard |
|---|
| 52 | | # TG2 style object dispatch. Fell free to overide it with |
|---|
| 53 | | # custom routes. TODO: Link to TG2+routes doc. |
|---|
| 54 | | make_map = defaults.make_default_route_map |
|---|
| 55 | | |
|---|
| 56 | | config['routes.map'] = make_map() |
|---|
| 57 | | config['pylons.app_globals'] = app_globals.Globals() |
|---|
| 58 | | #config['pylons.h'] = {{package}}.lib.helpers |
|---|
| 59 | | |
|---|
| 60 | | {{if identity == "sqlalchemy"}} |
|---|
| 61 | | |
|---|
| 62 | | config['identity'] = {'user_class':User, |
|---|
| 63 | | 'group_class':Group, |
|---|
| 64 | | 'permission_class':Permission, |
|---|
| 65 | | 'users_table':'tg_user', |
|---|
| 66 | | 'groups_table':'tg_group', |
|---|
| 67 | | 'permissions_table':'tg_permission', |
|---|
| 68 | | 'password_encryption_method':'sha', |
|---|
| 69 | | } |
|---|
| 70 | | |
|---|
| 71 | | |
|---|
| 72 | | {{endif}} |
|---|
| 73 | | |
|---|
| 74 | | {{if template_engine == 'mako'}} |
|---|
| 75 | | # Create the Mako TemplateLookup, with the default auto-escaping |
|---|
| 76 | | config['pylons.app_globals'].mako_lookup = TemplateLookup( |
|---|
| 77 | | directories=paths['templates'], |
|---|
| 78 | | module_directory=os.path.join(app_conf['cache_dir'], 'templates'), |
|---|
| 79 | | input_encoding='utf-8', output_encoding='utf-8', |
|---|
| 80 | | imports=['from webhelpers.html import escape'], |
|---|
| 81 | | default_filters=['escape']) |
|---|
| 82 | | {{elif template_engine == 'genshi'}} |
|---|
| 83 | | |
|---|
| 84 | | # Create the Genshi TemplateLoader |
|---|
| 85 | | config['pylons.app_globals'].genshi_loader = TemplateLoader( |
|---|
| 86 | | paths['templates'], auto_reload=True) |
|---|
| 87 | | {{elif template_engine == 'jinja'}} |
|---|
| 88 | | |
|---|
| 89 | | # Create the Jinja Environment |
|---|
| 90 | | config['pylons.app_globals'].jinja_env = Environment(loader=ChoiceLoader( |
|---|
| 91 | | [FileSystemLoader(path) for path in paths['templates']])) |
|---|
| 92 | | # Jinja's unable to request c's attributes without strict_c |
|---|
| 93 | | config['pylons.strict_c'] = True |
|---|
| 94 | | {{endif}} |
|---|
| 95 | | |
|---|
| 96 | | # If you'd like to change the default template engine used to render |
|---|
| 97 | | # text/html content, edit these options. |
|---|
| 98 | | default_template_engine = 'genshi' |
|---|
| 99 | | default_template_engine_options = {} |
|---|
| 100 | | config['buffet.template_engines'].pop() |
|---|
| 101 | | config.add_template_engine(default_template_engine, '${package}.templates', |
|---|
| 102 | | default_template_engine_options) |
|---|
| 103 | | |
|---|
| 104 | | {{if sqlalchemy}} |
|---|
| 105 | | # Setup SQLAlchemy database engine |
|---|
| 106 | | engine = engine_from_config(config, 'sqlalchemy.') |
|---|
| 107 | | config['pylons.app_globals'].sa_engine = engine |
|---|
| 108 | | # Pass the engine to initmodel, to be able to introspect tables |
|---|
| 109 | | init_model(engine) |
|---|
| 110 | | DBSession.configure(bind=engine) |
|---|
| 111 | | metadata.bind = engine |
|---|
| 112 | | {{endif}} |
|---|
| 113 | | |
|---|
| 114 | | # CONFIGURATION OPTIONS HERE (note: all config options will override |
|---|
| 115 | | # any Pylons config options) |
|---|