vigigraph / vigigraph / controllers / root.py @ 77b7746d
History | View | Annotate | Download (3.63 KB)
1 |
# -*- coding: utf-8 -*-
|
---|---|
2 |
# vim:set expandtab tabstop=4 shiftwidth=4:
|
3 |
# Copyright (C) 2006-2016 CS-SI
|
4 |
# License: GNU GPL v2 <http://www.gnu.org/licenses/gpl-2.0.html>
|
5 |
|
6 |
"""Vigigraph Controller"""
|
7 |
|
8 |
# pylint: disable-msg=W0613
|
9 |
# W0613: Unused argument : les arguments des contrôleurs sont les composants
|
10 |
# de la query-string de l'URL
|
11 |
|
12 |
import gettext |
13 |
import os.path |
14 |
import logging |
15 |
|
16 |
from tg import expose, require, config, response |
17 |
from tg.i18n import lazy_ugettext as l_, get_lang |
18 |
from repoze.what.predicates import Any, All, not_anonymous, \ |
19 |
has_permission, in_group |
20 |
from pkg_resources import resource_filename |
21 |
|
22 |
from vigilo.turbogears.controllers.auth import AuthController |
23 |
from vigilo.turbogears.controllers.custom import CustomController |
24 |
from vigilo.turbogears.controllers.error import ErrorController |
25 |
from vigilo.turbogears.controllers.proxy import ProxyController |
26 |
from vigilo.turbogears.controllers.api.root import ApiRootController |
27 |
|
28 |
from vigigraph.controllers.rpc import RpcController |
29 |
|
30 |
__all__ = ['RootController']
|
31 |
|
32 |
LOGGER = logging.getLogger(__name__) |
33 |
|
34 |
# pylint: disable-msg=R0201
|
35 |
class RootController(AuthController): |
36 |
"""
|
37 |
The root controller for the vigigraph application.
|
38 |
"""
|
39 |
error = ErrorController() |
40 |
rpc = RpcController() |
41 |
nagios = ProxyController('nagios', '/nagios/', |
42 |
not_anonymous(l_('You need to be authenticated')))
|
43 |
vigirrd = ProxyController('vigirrd', '/vigirrd/', |
44 |
not_anonymous(l_('You need to be authenticated')))
|
45 |
api = ApiRootController() |
46 |
custom = CustomController() |
47 |
|
48 |
@expose('index.html') |
49 |
@require(All(
|
50 |
not_anonymous(msg=l_("You need to be authenticated")),
|
51 |
Any( |
52 |
config.is_manager, |
53 |
has_permission('vigigraph-access',
|
54 |
msg=l_("You don't have access to VigiGraph")),
|
55 |
) |
56 |
)) |
57 |
def index(self): |
58 |
"""Handle the front-page."""
|
59 |
return dict(page='index') |
60 |
|
61 |
@expose()
|
62 |
def i18n(self): |
63 |
# Repris de tg.i18n.translation:_get_translator.
|
64 |
conf = config.current_conf() |
65 |
try:
|
66 |
localedir = conf['localedir']
|
67 |
except KeyError: |
68 |
localedir = os.path.join(conf['paths']['root'], 'i18n') |
69 |
|
70 |
lang = get_lang() |
71 |
|
72 |
# Localise le fichier *.mo actuellement chargé
|
73 |
# et génère le chemin jusqu'au *.js correspondant.
|
74 |
filename = gettext.find(conf['package'].__name__, localedir,
|
75 |
languages=lang) |
76 |
js = filename[:-3] + '.js' |
77 |
# Récupère et envoie le contenu du fichier de traduction *.js.
|
78 |
fhandle = open(js, 'r') |
79 |
translations = fhandle.read() |
80 |
fhandle.close() |
81 |
|
82 |
# Même chose pour les thèmes
|
83 |
themes_filename = gettext.find( |
84 |
'vigilo-themes',
|
85 |
resource_filename('vigilo.themes.i18n', ''), |
86 |
languages=lang) |
87 |
themes_js = themes_filename[:-3] + '.js' |
88 |
fhandle = open(themes_js, 'r') |
89 |
translations += fhandle.read() |
90 |
fhandle.close() |
91 |
|
92 |
# Extensions Enterprise
|
93 |
try:
|
94 |
ent_filename = gettext.find( |
95 |
'vigilo-vigigraph-enterprise',
|
96 |
resource_filename('vigilo.vigigraph_enterprise.i18n', ''), |
97 |
languages=lang) |
98 |
except ImportError: |
99 |
pass
|
100 |
else:
|
101 |
# Le nom du fichier sera None s'il n'existe pas
|
102 |
# de traductions dans la langue demandée.
|
103 |
if ent_filename is not None: |
104 |
fhandle = open(ent_filename[:-3] + '.js', 'r') |
105 |
translations += fhandle.read() |
106 |
fhandle.close() |
107 |
|
108 |
response.headers['Content-Type'] = 'text/javascript; charset=utf-8' |
109 |
return translations
|