Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

vigigraph / vigigraph / controllers / root.py @ 1626571f

History | View | Annotate | Download (4.13 KB)

1 0931fc44 Thomas BURGUIERE
# -*- coding: utf-8 -*-
2 1626571f Francois POIROTTE
# vim:set expandtab tabstop=4 shiftwidth=4:
3 079f14f1 Thomas BURGUIERE
"""Vigigraph Controller"""
4 0931fc44 Thomas BURGUIERE
5 39e0262e Francois POIROTTE
import logging
6 311c08cd Francois POIROTTE
from tg import expose, flash, require, request, redirect
7 7c9baa43 Francois POIROTTE
from pylons.i18n import ugettext as _, lazy_ugettext as l_, get_lang
8 0c6b774c Francois POIROTTE
from repoze.what.predicates import Any, All, not_anonymous, \
9
                                    has_permission, in_group
10 1626571f Francois POIROTTE
from pkg_resources import resource_filename
11 0931fc44 Thomas BURGUIERE
12 bc2d4167 Francois POIROTTE
from vigilo.turbogears.controllers import BaseController
13 6fca36fb Francois POIROTTE
from vigilo.turbogears.controllers.error import ErrorController
14 bc2d4167 Francois POIROTTE
from vigilo.turbogears.controllers.proxy import ProxyController
15 d9afe37a Aurelien BOMPARD
from vigilo.turbogears.controllers.api.root import ApiRootController
16
17 6fca36fb Francois POIROTTE
from vigigraph.controllers.rpc import RpcController
18 0931fc44 Thomas BURGUIERE
19
__all__ = ['RootController']
20
21 39e0262e Francois POIROTTE
LOGGER = logging.getLogger(__name__)
22 0931fc44 Thomas BURGUIERE
23 16a7bac7 Francois POIROTTE
# pylint: disable-msg=R0201
24 0931fc44 Thomas BURGUIERE
class RootController(BaseController):
25
    """
26
    The root controller for the vigigraph application.
27
    """
28
    error = ErrorController()
29 079f14f1 Thomas BURGUIERE
    rpc = RpcController()
30 945bd396 Francois POIROTTE
    nagios = ProxyController('nagios', '/nagios/',
31
        not_anonymous(l_('You need to be authenticated')))
32 0b29eb32 Aurelien BOMPARD
    vigirrd = ProxyController('vigirrd', '/vigirrd/',
33 945bd396 Francois POIROTTE
        not_anonymous(l_('You need to be authenticated')))
34 d9afe37a Aurelien BOMPARD
    api = ApiRootController("/api")
35 0931fc44 Thomas BURGUIERE
36 98c1bcf7 Thomas BURGUIERE
    @expose('index.html')
37 0c6b774c Francois POIROTTE
    @require(All(
38
        not_anonymous(msg=l_("You need to be authenticated")),
39
        Any(
40
            in_group('managers'),
41 945bd396 Francois POIROTTE
            has_permission('vigigraph-access',
42 ea060d8c Francois POIROTTE
                msg=l_("You don't have access to VigiGraph")),
43 0c6b774c Francois POIROTTE
        )
44
    ))
45 0931fc44 Thomas BURGUIERE
    def index(self):
46
        """Handle the front-page."""
47
        return dict(page='index')
48
49 7c9baa43 Francois POIROTTE
    @expose()
50
    def i18n(self):
51
        import gettext
52
        import pylons
53
        import os.path
54
55
        # Repris de pylons.i18n.translation:_get_translator.
56
        conf = pylons.config.current_conf()
57
        try:
58
            rootdir = conf['pylons.paths']['root']
59
        except KeyError:
60
            rootdir = conf['pylons.paths'].get('root_path')
61
        localedir = os.path.join(rootdir, 'i18n')
62
63 1626571f Francois POIROTTE
        lang = get_lang()
64
65 7c9baa43 Francois POIROTTE
        # Localise le fichier *.mo actuellement chargé
66
        # et génère le chemin jusqu'au *.js correspondant.
67
        filename = gettext.find(conf['pylons.package'], localedir,
68 1626571f Francois POIROTTE
            languages=lang)
69 7c9baa43 Francois POIROTTE
        js = filename[:-3] + '.js'
70
71 1626571f Francois POIROTTE
        themes_filename = gettext.find(
72
            'vigilo-themes',
73
            resource_filename('vigilo.themes.i18n', ''),
74
            languages=lang)
75
        themes_js = themes_filename[:-3] + '.js'
76
77 7c9baa43 Francois POIROTTE
        # 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 1626571f Francois POIROTTE
82
        fhandle = open(themes_js, 'r')
83
        translations += fhandle.read()
84
        fhandle.close()
85 7c9baa43 Francois POIROTTE
        return translations
86
87 98c1bcf7 Thomas BURGUIERE
    @expose('login.html')
88 311c08cd Francois POIROTTE
    def login(self, came_from='/'):
89 0931fc44 Thomas BURGUIERE
        """Start the user login."""
90
        login_counter = request.environ['repoze.who.logins']
91
        if login_counter > 0:
92
            flash(_('Wrong credentials'), 'warning')
93
        return dict(page='login', login_counter=str(login_counter),
94
                    came_from=came_from)
95 1626571f Francois POIROTTE
96 0931fc44 Thomas BURGUIERE
    @expose()
97 311c08cd Francois POIROTTE
    def post_login(self, came_from='/'):
98 0931fc44 Thomas BURGUIERE
        """
99
        Redirect the user to the initially requested page on successful
100
        authentication or redirect her back to the login page if login failed.
101 1626571f Francois POIROTTE

102 0931fc44 Thomas BURGUIERE
        """
103
        if not request.identity:
104
            login_counter = request.environ['repoze.who.logins'] + 1
105 311c08cd Francois POIROTTE
            redirect('/login', came_from=came_from, __logins=login_counter)
106 0931fc44 Thomas BURGUIERE
        userid = request.identity['repoze.who.userid']
107 39e0262e Francois POIROTTE
        LOGGER.info(_('"%(username)s" logged in (from %(IP)s)') % {
108
                'username': userid,
109
                'IP': request.remote_addr,
110
            })
111 0931fc44 Thomas BURGUIERE
        flash(_('Welcome back, %s!') % userid)
112
        redirect(came_from)
113
114
    @expose()
115 311c08cd Francois POIROTTE
    def post_logout(self, came_from='/'):
116 0931fc44 Thomas BURGUIERE
        """
117
        Redirect the user to the initially requested page on logout and say
118
        goodbye as well.
119 1626571f Francois POIROTTE

120 0931fc44 Thomas BURGUIERE
        """
121 5b2869b1 Francois POIROTTE
        LOGGER.info(_('Some user logged out (from %(IP)s)') % {
122
                'IP': request.remote_addr,
123
            })
124 0931fc44 Thomas BURGUIERE
        flash(_('We hope to see you soon!'))
125
        redirect(came_from)