Parent: [77e831] (diff)

Child: [e16326] (diff)

Download this file

app_cfg.py    121 lines (94 with data), 4.2 kB

  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
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# -*- coding: utf-8 -*-
"""
Global configuration file for TG2-specific settings in allura.
This file complements development/deployment.ini.
Please note that **all the argument values are strings**. If you want to
convert them into boolean, for example, you should use the
:func:`paste.deploy.converters.asbool` function, as in::
from paste.deploy.converters import asbool
setting = asbool(global_conf.get('the_setting'))
"""
import logging
import pkg_resources
from tg.configuration import AppConfig, config
from paste.deploy.converters import asbool
from routes import Mapper
from webhelpers.html import literal
import ew
import allura
import allura.lib.helpers as h
from allura.lib import app_globals, custom_middleware
log = logging.getLogger(__name__)
class ForgeConfig(AppConfig):
def __init__(self, root_controller='root'):
AppConfig.__init__(self)
self.root_controller = root_controller
self.package = allura
self.renderers = [ 'json', 'genshi', 'mako', 'jinja' ]
self.default_renderer = 'genshi'
self.use_sqlalchemy = False
self.use_toscawidgets = True
self.use_transaction_manager = False
# self.handle_status_codes = [ 403, 404 ]
self.handle_status_codes = [ 403, 404 ]
def add_error_middleware(self, global_conf, app):
app = AppConfig.add_error_middleware(self, global_conf, app)
app = custom_middleware.LoginRedirectMiddleware(app)
return app
def after_init_config(self):
config['pylons.strict_c'] = True
def add_core_middleware(self, app):
if asbool(config.get('auth.method', 'local')=='sfx'):
import sfx.middleware
d = h.config_with_prefix(config, 'auth.')
d.update(h.config_with_prefix(config, 'sfx.'))
app = sfx.middleware.SfxMiddleware(app, d)
return super(ForgeConfig, self).add_core_middleware(app)
def setup_routes(self):
map = Mapper(directory=config['pylons.paths']['controllers'],
always_scan=config['debug'])
# Setup a default route for the root of object dispatch
map.connect('*url', controller=self.root_controller,
action='routes_placeholder')
config['routes.map'] = map
def setup_jinja_renderer(self):
from jinja2 import ChoiceLoader, Environment, PackageLoader
from tg.render import render_jinja
loaders = {'allura': PackageLoader('allura', 'templates')}
for ep in pkg_resources.iter_entry_points('allura'):
if not ep.module_name in loaders:
log.info('Registering templates for application %s', ep.module_name)
try:
loaders[ep.module_name] = PackageLoader(ep.module_name, 'templates')
except ImportError:
log.warning('Cannot import entry point %s', ep)
continue
config['pylons.app_globals'].jinja2_env = Environment(
loader=ChoiceLoader(loaders.values()),
auto_reload=self.auto_reload_templates,
autoescape=True,
extensions=['jinja2.ext.do'])
# Jinja's unable to request c's attributes without strict_c
config['pylons.strict_c'] = True
self.render_functions.jinja = render_jinja
class JinjaEngine(ew.TemplateEngine):
def __init__(self, entry_point, config):
import jinja2
self.jinja2 = jinja2
super(JinjaEngine, self).__init__(entry_point, config)
@property
def _environ(self):
return config['pylons.app_globals'].jinja2_env
def load(self, template_name):
try:
return self._environ.get_template(template_name)
except self.jinja2.TemplateNotFound:
raise ew.errors.TemplateNotFound, '%s not found' % template_name
def parse(self, template_text, filepath=None):
return self._environ.from_string(template_text)
def render(self, template, context):
context = self.context(context)
with ew.utils.push_context(ew.widget_context, render_context=context):
text = template.render(**context)
return literal(text)
base_config = ForgeConfig()