Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

vigigraph / vigigraph / controllers / root.py @ ef840436

History | View | Annotate | Download (3.1 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 tg.predicates import Any, All, not_anonymous, has_permission, in_group
19
from pkg_resources import resource_filename
20

    
21
from vigilo.turbogears.controllers.auth import AuthController
22
from vigilo.turbogears.controllers.custom import CustomController
23
from vigilo.turbogears.controllers.error import ErrorController
24
from vigilo.turbogears.controllers.proxy import ProxyController
25
from vigilo.turbogears.controllers.api.root import ApiRootController
26

    
27
from vigigraph.controllers.rpc import RpcController
28

    
29
__all__ = ['RootController']
30

    
31
LOGGER = logging.getLogger(__name__)
32

    
33
# pylint: disable-msg=R0201
34
class RootController(AuthController):
35
    """
36
    The root controller for the vigigraph application.
37
    """
38
    error = ErrorController()
39
    rpc = RpcController()
40
    nagios = ProxyController('nagios', '/nagios/',
41
        not_anonymous(l_('You need to be authenticated')))
42
    vigirrd = ProxyController('vigirrd', '/vigirrd/',
43
        not_anonymous(l_('You need to be authenticated')))
44
    api = ApiRootController()
45
    custom = CustomController()
46

    
47
    @expose('index.html')
48
    @require(All(
49
        not_anonymous(msg=l_("You need to be authenticated")),
50
        Any(
51
            config.is_manager,
52
            has_permission('vigigraph-access',
53
                msg=l_("You don't have access to VigiGraph")),
54
        )
55
    ))
56
    def index(self):
57
        """Handle the front-page."""
58
        return dict(page='index')
59

    
60
    @expose()
61
    def i18n(self):
62
        # Repris de tg.i18n.translation:_get_translator.
63
        conf = config.current_conf()
64
        try:
65
            localedir = conf['localedir']
66
        except KeyError:
67
            localedir = os.path.join(conf['paths']['root'], 'i18n')
68

    
69
        lang = get_lang()
70
        modules = (
71
            (conf['package'].__name__, localedir),
72
            ('vigilo-themes', resource_filename('vigilo.themes.i18n', '')),
73
            ('vigilo-vigigraph-enterprise',
74
             resource_filename('vigilo.vigigraph_enterprise.i18n', '')),
75
        )
76

    
77
        # Charge et installe le fichier JS de traduction de chaque module
78
        translations = "babel.Translations.load("
79
        for domain, directory in modules:
80
            try:
81
                mofile = gettext.find(domain, directory, languages=lang)
82
                if mofile is None:
83
                    continue
84

    
85
                fhandle = open(mofile[:-3] + '.js', 'r')
86
                translations += fhandle.read()
87
                fhandle.close()
88
                translations += ").load("
89
            except ImportError:
90
                pass
91
        translations += "{}).install()"
92

    
93
        response.headers['Content-Type'] = 'text/javascript; charset=utf-8'
94
        return translations