Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

vigiboard / vigiboard / controllers / root.py @ 20367931

History | View | Annotate | Download (3.35 KB)

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

    
5
from tg import expose, flash, require, url, request, redirect
6

    
7
import pylons
8
from pylons.i18n import ugettext as _, lazy_ugettext as l_
9
from catwalk.tg2 import Catwalk
10
from repoze.what import predicates
11

    
12
from vigiboard.lib.base import BaseController
13
from vigiboard.model import DBSession 
14
from vigiboard.controllers.error import ErrorController
15
from vigiboard import model
16
from vigiboard.controllers.secure import SecureController
17

    
18
from vigiboard.controllers.vigiboard_ctl import VigiboardController
19

    
20
__all__ = ['RootController']
21

    
22
class RootController(BaseController):
23
    """
24
    The root controller for the vigiboard application.
25
    
26
    All the other controllers and WSGI applications should be mounted on this
27
    controller. For example::
28
    
29
        panel = ControlPanelController()
30
        another_app = AnotherWSGIApplication()
31
    
32
    Keep in mind that WSGI applications shouldn't be mounted directly: They
33
    must be wrapped around with :class:`tg.controllers.WSGIAppController`.
34
    
35
    """
36
    secc = SecureController()
37
    
38
    admin = Catwalk(model, DBSession)
39
    
40
    error = ErrorController()
41

    
42
    vigiboard = VigiboardController()
43

    
44
    @expose('vigiboard.templates.index')
45
    def index(self):
46
        """Handle the front-page."""
47
        return dict(page='index')
48

    
49
    @expose('vigiboard.templates.about')
50
    def about(self):
51
        """Handle the 'about' page."""
52
        return dict(page='about')
53

    
54
    @expose('vigiboard.templates.authentication')
55
    def auth(self):
56
        """Display some information about auth* on this application."""
57
        return dict(page='auth')
58

    
59
    @expose('vigiboard.templates.index')
60
    @require(predicates.has_permission('manage', msg=l_('Only for managers')))
61
    def manage_permission_only(self, **kw):
62
        """Illustrate how a page for managers only works."""
63
        return dict(page='managers stuff')
64

    
65
    @expose('vigiboard.templates.index')
66
    @require(predicates.is_user('editor', msg=l_('Only for the editor')))
67
    def editor_user_only(self, **kw):
68
        """Illustrate how a page exclusive for the editor works."""
69
        return dict(page='editor stuff')
70

    
71
    @expose('vigiboard.templates.login')
72
    def login(self, came_from=url('/')):
73
        """Start the user login."""
74
        login_counter = request.environ['repoze.who.logins']
75
        if login_counter > 0:
76
            flash(_('Wrong credentials'), 'warning')
77
        return dict(page='login', login_counter=str(login_counter),
78
                    came_from=came_from)
79
    
80
    @expose()
81
    def post_login(self, came_from=url('/')):
82
        """
83
        Redirect the user to the initially requested page on successful
84
        authentication or redirect her back to the login page if login failed.
85
        
86
        """
87
        if not request.identity:
88
            login_counter = request.environ['repoze.who.logins'] + 1
89
            redirect(url('/login', came_from=came_from, __logins=login_counter))
90
        userid = request.identity['repoze.who.userid']
91
        flash(_('Welcome back, %s!') % userid)
92
        redirect(came_from)
93

    
94
    @expose()
95
    def post_logout(self, came_from=url('/')):
96
        """
97
        Redirect the user to the initially requested page on logout and say
98
        goodbye as well.
99
        
100
        """
101
        flash(_('We hope to see you soon!'))
102
        redirect(came_from)