Source

moviedemo / moviedemo / controllers / sproxdemo.py

Full commit
"""Rest Controller Info"""
from tg import tmpl_context, request
from tg.decorators import expose, validate

from tg.controllers import RestController, redirect
from moviedemo.model import DBSession, Movie, Genre, Director

from sprox.dojo.tablebase import DojoTableBase as TableBase
from sprox.dojo.fillerbase import DojoTableFiller as TableFiller

class MovieTableFiller(TableFiller):
    __model__ = Movie
    __add_fields__ = {'related_movies':None}
    def directors(self, obj):
        directors = ', '.join(['<a href="/directors/'+str(d.director_id)+'">'+d.name+'</a>' 
                               for d in obj.directors])
        return directors.join(('<div>', '</div>'))

    def related_movies(self, obj):
        return ', '.join([movie.title for movie in obj.genre.movies if movie!=obj])

movie_filler = MovieTableFiller(DBSession)

class MovieTable(TableBase):
    __model__ = Movie
    __omit_fields__ = ['movie_id', 'genre_id', '__actions__']
    __xml_fields__ = ['directors']
    __add_fields__ = {'related_movies':None}
    __column_widths__ = {'description':"30em",
                         'release_date':"7em",
                         'genre':"4em",
                         }
    __url__ = "/movies.json"
    
movie_table = MovieTable(DBSession)

#uncomment this if you want to play with the "normal" table code
#from sprox.tablebase import TableBase
#from sprox.fillerbase import TableFiller
#class MovieTableFiller(TableFiller):
#    __model__ = Movie
#    __add_fields__ = {'related_movies':None}
#    def directors(self, obj):
#        directors = ', '.join(['<a href="/directors/'+str(d.director_id)+'">'+d.name+'</a>' 
#                               for d in obj.directors])
#        return directors.join(('<div>', '</div>'))

#    def related_movies(self, obj):
#        return ', '.join([movie.title for movie in obj.genre.movies if movie!=obj])

#movie_filler = MovieTableFiller(DBSession)

#class MovieTable(TableBase):
#    __model__ = Movie
#    __omit_fields__ = ['movie_id', 'genre_id', '__actions__']
#    __xml_fields__ = ['directors']
#    __add_fields__ = {'related_movies':None}

#movie_table = MovieTable(DBSession);

class SproxMovieController(RestController):

    @expose('moviedemo.templates.sproxdemo.movies.get_all')
    @expose('json')
    def get_all(self, *args, **kw):
        if request.response_type == 'application/json':
            return movie_filler.get_value(**kw)

        value = []
        tmpl_context.widget = movie_table
        return dict(value=value)
 
# uncomment this to work sprox w/out dojo
#    @expose('moviedemo.templates.sproxdemo.movies.get_all')
#    def get_all(self):
#        tmpl_context.widget = movie_table
#        value = movie_filler.get_value()
#        return dict(value=value)

class DirectorMovieTableFiller(TableFiller):
    __model__ = Movie
    __add_fields__ = {'related_movies':None}
    
    def directors(self, obj, **kw):
        director = kw['director']
        directors = ', '.join(['<a href="/directors/'+str(d.director_id)+'">'+d.name+'</a>' 
                               for d in obj.directors if d!=director])
        return directors.join(('<div>', '</div>'))

    def related_movies(self, obj, **kw):
        return ', '.join([movie.title for movie in obj.genre.movies if movie!=obj])

    def _do_get_provider_count_and_objs(self, director=None, **kw):
        movies = DBSession.query(Movie).filter(Movie.directors.contains(director)).all()
        return len(movies), movies

director_movie_filler = DirectorMovieTableFiller(DBSession)

class DirectorMovieTable(TableBase):
    __model__ = Movie
    __omit_fields__ = ['movie_id', 'genre_id', '__actions__']
    __xml_fields__ = ['other directors']
    __add_fields__ = {'related_movies':None}
    __headers__ = {'directors': 'other directors'}
    
director_movie_table = DirectorMovieTable(DBSession);

class SproxDirectorController(RestController):
    
    @expose('moviedemo.templates.sproxdemo.directors.get_one')
    def get_one(self, director_id):
        tmpl_context.widget = director_movie_table
        director = DBSession.query(Director).get(director_id)
        value = director_movie_filler.get_value(director=director)
        return dict(director=director, value=value)