Source

moviedemo / moviedemo / controllers / root.py

"""Main Controller"""
from moviedemo.lib.base import BaseController
from tg import expose, flash, require, url, request, redirect
from pylons.i18n import ugettext as _
#from tg import redirect, validate
from moviedemo.model import DBSession, metadata
from moviedemo.controllers.error import ErrorController
from moviedemo import model
from catwalk.tg2 import Catwalk
from repoze.what import predicates
from moviedemo.controllers.secure import SecureController

# =====================================

from tgext.crud import CrudRestController
from moviedemo.model import DBSession, Movie
from sprox.tablebase import TableBase
from sprox.fillerbase import TableFiller

from sprox.formbase import AddRecordForm

class MovieAddForm(AddRecordForm):
    __model__ = Movie
    __omit_fields__ = ['genre_id', 'movie_id']
movie_add_form = MovieAddForm(DBSession)

 
from sprox.formbase import EditableForm

class MovieEditForm(EditableForm):
    __model__ = Movie
    __omit_fields__ = ['genre_id', 'movie_id']
movie_edit_form = MovieEditForm(DBSession)


from sprox.fillerbase import EditFormFiller

class MovieEditFiller(EditFormFiller):
    __model__ = Movie
movie_edit_filler = MovieEditFiller(DBSession)

#this is de-activated so that the controller uses the Sprox form.
from tw.core import WidgetsList
from tw.forms import TableForm, TextField, CalendarDatePicker, SingleSelectField, TextArea
from formencode.validators import Int, NotEmpty, DateConverter, DateValidator

class MovieForm(TableForm):
    class fields(WidgetsList):
        title = TextField(validator=NotEmpty)
        description = TextArea(attrs=dict(rows=7, cols=50))
        release_date = CalendarDatePicker(validator=DateConverter())
        genrechoices = ((None, '----------'),
                         (1,"action"),
                         (2,"animation"),
                         (3,"comedy"),
                         (4,"documentary"),
                         (5,"drama"),
                         (6,"sci-fi"))
        genre = SingleSelectField(options=genrechoices)
_movie_add_form = MovieForm("create_movie_form")
#end deactivation

def email_info():
    print 'emailing'

class MovieTable(TableBase):
    __model__ = Movie
    __omit_fields__ = ['genre_id', 'movie_id']
movie_table = MovieTable(DBSession)

class MovieTableFiller(TableFiller):
    __model__ = Movie
movie_table_filler = MovieTableFiller(DBSession)

from sprox.dojo.tablebase import DojoTableBase
class DojoMovieTable(DojoTableBase):
    __model__ = Movie
    __omit_fields__ = ['genre_id', 'movie_id']
dojo_movie_table = DojoMovieTable(DBSession)

from sprox.dojo.fillerbase import DojoTableFiller
class DojoMovieTableFiller(DojoTableFiller):
    __model__ = Movie
dojo_movie_table_filler = DojoMovieTableFiller(DBSession)

class MovieController(CrudRestController):
    model = Movie

    table =  dojo_movie_table
    table_filler = dojo_movie_table_filler
    
    new_form = movie_add_form
    edit_form = movie_edit_form
    edit_filler = movie_edit_filler
    
    @expose()
    def post(self, **kw):
        email_info()
        return super(MovieController, self).post(**kw)
    
    def _post_delete(self, **kw):
        "not allowed"
        

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

from tgext.admin import AdminController
    
class DeclarativeMovieController(CrudRestController):
    model = Movie
    
    class add_form_type(AddRecordForm):
        __model__ = Movie
        __omit_fields__ = ['genre_id', 'movie_id']

    class edit_form_type(EditableForm):
        __model__ = Movie
        __omit_fields__ = ['genre_id', 'movie_id']

    class edit_filler_type(EditFormFiller):
        __model__ = Movie

    class table_type(TableBase):
        __model__ = Movie
        __omit_fields__ = ['genre_id', 'movie_id']

    class table_filler_type(TableFiller):
        __model__ = Movie

# =====================================

import inspect
from sqlalchemy.orm import class_mapper

models = {}
for m in dir(model):
    m = getattr(model, m)
    if not inspect.isclass(m):
        continue
    try:
        mapper = class_mapper(m)
        models[m.__name__.lower()] = m
    except:
        pass

class RootController(BaseController):
    movie = MovieController(DBSession, models)

    secc = SecureController()
    admin = Catwalk(model, DBSession)
    error = ErrorController()

    @expose('moviedemo.templates.index')
    def index(self):
        return dict(page='index')

    @expose('moviedemo.templates.about')
    def about(self):
        return dict(page='about')

    @expose('moviedemo.templates.authentication')
    def auth(self):
        return dict(page='auth')

    @expose('moviedemo.templates.index')
    @require(predicates.has_permission('manage', msg=_('Only for managers')))
    def manage_permission_only(self, **kw):
        return dict(page='managers stuff')

    @expose('moviedemo.templates.index')
    @require(predicates.is_user('editor', msg=_('Only for the editor')))
    def editor_user_only(self, **kw):
        return dict(page='editor stuff')

    @expose('moviedemo.templates.login')
    def login(self, came_from=url('/')):
        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('/')):
        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('/')):
        flash(_('We hope to see you soon!'))
        redirect(came_from)