Source

wdmmg / wdmmg / lib / base.py

"""The base Controller API

Provides the BaseController class for subclassing.
"""
from pylons.controllers import WSGIController
from pylons.templating import literal, cached_template, pylons_globals
from pylons import tmpl_context as c, request, config, app_globals
from pylons.controllers.util import abort

import wdmmg
from wdmmg import model

def render(template_name, extra_vars=None, cache_key=None, 
           cache_type=None, cache_expire=None, method='xhtml'):
    # Create a render callable for the cache function
    def render_template():
        # Pull in extra vars if needed
        globs = extra_vars or {}
        
        # Second, get the globals
        globs.update(pylons_globals())
        globs['g'] = app_globals

        # Grab a template reference
        template = globs['app_globals'].genshi_loader.load(template_name)
        
        return literal(template.generate(**globs).render(method=method,
                                                         encoding=None))
    
    return cached_template(template_name, render_template, cache_key=cache_key,
                           cache_type=cache_type, cache_expire=cache_expire,
                           ns_options=('method'), method=method)

class BaseController(WSGIController):

    def __call__(self, environ, start_response):
        """Invoke the Controller"""
        # WSGIController.__call__ dispatches to the Controller method
        # the request is routed to. This routing information is
        # available in environ['pylons.routes_dict']
        try:
            return WSGIController.__call__(self, environ, start_response)
        finally:
            model.mongo.connection.end_request()
    
    def __before__(self, action, **params):
        c.q = ''
        c.items_per_page = int(request.params.get('items_per_page', 20))