Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

vigigraph / vigigraph / controllers / root.py @ bc2d4167

History | View | Annotate | Download (2.45 KB)

1
# -*- coding: utf-8 -*-
2
# vim:set expandtab tabstop=4 shiftwidth=4: 
3
"""Vigigraph Controller"""
4

    
5
import logging
6
from tg import expose, flash, require, request, redirect
7
from pylons.i18n import ugettext as _
8
from repoze.what.predicates import Any, not_anonymous
9

    
10
from vigilo.turbogears.controllers import BaseController
11
from vigigraph.controllers.error import ErrorController
12
from vigigraph.controllers.rpc import RpcController
13
from vigilo.turbogears.controllers.proxy import ProxyController
14

    
15
__all__ = ['RootController']
16

    
17
LOGGER = logging.getLogger(__name__)
18

    
19
# pylint: disable-msg=R0201
20
class RootController(BaseController):
21
    """
22
    The root controller for the vigigraph application.
23
    """
24
    error = ErrorController()
25
    rpc = RpcController()
26
    nagios = ProxyController('nagios', '/nagios/')
27
    rrdgraph = ProxyController('rrdgraph', '/rrdgraph/')
28

    
29
    @expose('index.html')
30
    @require(Any(not_anonymous(), msg=_("You need to be authenticated")))
31
    def index(self):
32
        """Handle the front-page."""
33
        return dict(page='index')
34

    
35
    @expose('login.html')
36
    def login(self, came_from='/'):
37
        """Start the user login."""
38
        login_counter = request.environ['repoze.who.logins']
39
        if login_counter > 0:
40
            flash(_('Wrong credentials'), 'warning')
41
        return dict(page='login', login_counter=str(login_counter),
42
                    came_from=came_from)
43
    
44
    @expose()
45
    def post_login(self, came_from='/'):
46
        """
47
        Redirect the user to the initially requested page on successful
48
        authentication or redirect her back to the login page if login failed.
49
        
50
        """
51
        if not request.identity:
52
            login_counter = request.environ['repoze.who.logins'] + 1
53
            redirect('/login', came_from=came_from, __logins=login_counter)
54
        userid = request.identity['repoze.who.userid']
55
        LOGGER.info(_('"%(username)s" logged in (from %(IP)s)') % {
56
                'username': userid,
57
                'IP': request.remote_addr,
58
            })
59
        flash(_('Welcome back, %s!') % userid)
60
        redirect(came_from)
61

    
62
    @expose()
63
    def post_logout(self, came_from='/'):
64
        """
65
        Redirect the user to the initially requested page on logout and say
66
        goodbye as well.
67
        
68
        """
69
        LOGGER.info(_('Some user logged out (from %(IP)s)') % {
70
                'IP': request.remote_addr,
71
            })
72
        flash(_('We hope to see you soon!'))
73
        redirect(came_from)