Revision 5f2cd70a
Utilisation des templates et des fichiers statiques du thème.
Ajout d'une dépendance vers vigilo-themes-default à cet effet.
git-svn-id: https://vigilo-dev.si.c-s.fr/svn@853 b22e2e97-25c9-44ff-b637-2e5ceca36478
vigiboard/config/app_cfg.py | ||
---|---|---|
17 | 17 |
from tg.configuration import AppConfig |
18 | 18 |
|
19 | 19 |
class MyAppConfig(AppConfig): |
20 |
"""We overload AppConfig to prevent it from loading init_model()""" |
|
21 |
|
|
22 |
def __init__(self): |
|
23 |
AppConfig.__init__(self) |
|
20 |
"""We overload AppConfig for our needs.""" |
|
21 |
|
|
22 |
def __init__(self, app_name): |
|
23 |
super(MyAppConfig, self).__init__() |
|
24 |
self.__app_name = app_name |
|
25 |
self.__tpl_translator = None |
|
26 |
|
|
27 |
def __setup_template_translator(self): |
|
28 |
from pkg_resources import resource_filename |
|
29 |
import gettext |
|
30 |
from tg.i18n import get_lang |
|
31 |
|
|
32 |
if self.__tpl_translator is None: |
|
33 |
i18n_dir = resource_filename('vigilo.themes', 'i18n') |
|
34 |
lang = get_lang() |
|
35 |
|
|
36 |
# During unit tests, no language is defined |
|
37 |
# which results in an error if gettext.translation |
|
38 |
# is used to retrieve translations. |
|
39 |
if lang is None: |
|
40 |
self.__tpl_translator = gettext.NullTranslations() |
|
41 |
else: |
|
42 |
self.__tpl_translator = gettext.translation( |
|
43 |
'theme', i18n_dir, get_lang()) |
|
44 |
|
|
45 |
def setup_paths(self): |
|
46 |
"""Add egg-aware search path to genshi.""" |
|
47 |
super(MyAppConfig, self).setup_paths() |
|
48 |
from pkg_resources import resource_filename |
|
49 |
|
|
50 |
app_templates = resource_filename( |
|
51 |
'vigilo.themes.templates', self.__app_name) |
|
52 |
common_templates = resource_filename( |
|
53 |
'vigilo.themes.templates', 'common') |
|
54 |
self.paths['templates'] = [app_templates, common_templates] |
|
55 |
|
|
56 |
def setup_genshi_renderer(self): |
|
57 |
"""Setup templates to use an alternate translator.""" |
|
58 |
# On reprend plusieurs éléments de "tg.configuration". |
|
59 |
from genshi.template import TemplateLoader |
|
60 |
from genshi.filters import Translator |
|
61 |
from tg.render import render_genshi |
|
62 |
from pkg_resources import resource_filename |
|
63 |
from tg.configuration import config |
|
64 |
|
|
65 |
def template_loaded(template): |
|
66 |
"""Called when a template is done loading.""" |
|
67 |
self.__setup_template_translator() |
|
68 |
template.filters.insert(0, Translator(self.__tpl_translator.ugettext)) |
|
69 |
|
|
70 |
def my_render_genshi(template_name, template_vars, **kwargs): |
|
71 |
self.__setup_template_translator() |
|
72 |
template_vars['l_'] = self.__tpl_translator.ugettext |
|
73 |
return render_genshi(template_name, template_vars, **kwargs) |
|
74 |
|
|
75 |
loader = TemplateLoader(search_path=self.paths.templates, |
|
76 |
auto_reload=self.auto_reload_templates, |
|
77 |
callback=template_loaded) |
|
78 |
|
|
79 |
config['pylons.app_globals'].genshi_loader = loader |
|
80 |
self.render_functions.genshi = my_render_genshi |
|
24 | 81 |
|
25 | 82 |
def setup_sqlalchemy(self): |
26 | 83 |
""" |
... | ... | |
34 | 91 |
from vigiboard import model |
35 | 92 |
from vigiboard.lib import app_globals, helpers |
36 | 93 |
|
37 |
base_config = MyAppConfig() |
|
94 |
base_config = MyAppConfig('vigiboard')
|
|
38 | 95 |
base_config.renderers = [] |
39 | 96 |
|
97 |
# Pour gérer les thèmes, la notation "pointée" n'est pas utilisée. |
|
98 |
# À la place, on indique le nom complet du template (ex: "index.html") |
|
99 |
# lors de l'appel au décorateur @expose. |
|
100 |
base_config.use_dotted_templatenames = False |
|
101 |
|
|
102 |
# On définit la variable à False. En réalité, le comportement |
|
103 |
# est le même que si elle valait toujours True, sauf que l'on |
|
104 |
# met en place les middlewares nous même pour pouvoir gérer les |
|
105 |
# thèmes (cf. ./middleware.py). |
|
106 |
base_config.serve_static = False |
|
107 |
|
|
40 | 108 |
base_config.package = vigiboard |
41 | 109 |
|
42 | 110 |
#Set the default renderer |
Also available in: Unified diff