Source

baptism_records / Baptism-Records / baptism_records / controllers / root.py

# -*- coding: utf-8 -*-
"""Main Controller"""

from tg import expose, flash, require, url, request, redirect
from tgext.admin import AdminController
from pylons.i18n import ugettext as _, lazy_ugettext as l_
# from catwalk.tg2 import Catwalk
from repoze.what import predicates

from baptism_records.lib.base import BaseController
from baptism_records.model import DBSession, metadata
from baptism_records.controllers.error import ErrorController
from baptism_records import model
from baptism_records.controllers.secure import SecureController

from baptism_records.model.baptism_model import *
from baptism_records.model.auth import *

__all__ = ['RootController', 'ChristianController']

from tg import tmpl_context, redirect, validate
from baptism_records.widgets.christian_form import create_christian_form, edit_christian_form

from tgext.crud import CrudRestController
from sprox.tablebase import TableBase
from sprox.formbase import EditableForm, AddRecordForm
from sprox.fillerbase import TableFiller, EditFormFiller

from tw.forms import *

import logging

log = logging.getLogger(__name__)

from baptism_records.controllers.christian import *


class RootController(BaseController):
    """
    The root controller for the Baptism-Records application.
    
    All the other controllers and WSGI applications should be mounted on this
    controller. For example::
    
        panel = ControlPanelController()
        another_app = AnotherWSGIApplication()
    
    Keep in mind that WSGI applications shouldn't be mounted directly: They
    must be wrapped around with :class:`tg.controllers.WSGIAppController`.
    
    """
    secc = SecureController()
    
    # admin = Catwalk(model, DBSession)
    users = AdminController([User, Group, Permission], DBSession)
    admin = AdminController(
            [BookLocation, Christian, Church, Marriage, 
            Minister, PostalCode, Religious, Remark],
            DBSession)
    
    error = ErrorController()
    
    christians = ChristianController()
    c = CRUDdyChristianController(DBSession)
    
    complete = CompleteController()
    
    book = BookListController()
    
    @expose('baptism_records.templates.index')
    def index(self):
        """Handle the front-page."""
        return dict(page='index')

    @expose('baptism_records.templates.about')
    def about(self):
        """Handle the 'about' page."""
        return dict(page='about')

    @expose('baptism_records.templates.authentication')
    def auth(self):
        """Display some information about auth* on this application."""
        return dict(page='auth')

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

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

    @expose('baptism_records.templates.login')
    def login(self, came_from=url('/')):
        """Start the user login."""
        login_counter = request.environ['repoze.who.logins']
        if login_counter > 0:
            flash(_('Wrong credentials'), 'warning')
        return dict(page='login', login_counter=str(login_counter),
                    came_from=came_from)
    
    @expose()
    def post_login(self, came_from=url('/')):
        """
        Redirect the user to the initially requested page on successful
        authentication or redirect her back to the login page if login failed.
        
        """
        if not request.identity:
            login_counter = request.environ['repoze.who.logins'] + 1
            redirect(url('/login', came_from=came_from, __logins=login_counter))
        userid = request.identity['repoze.who.userid']
        flash(_('Welcome back, %s!') % userid)
        redirect(came_from)

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