Package: bricks.controller
Controller Classes

Controller

They are handy controller classes

API Reference

Modules

bricks.controller.__inift__

Controller Classes

Module Attributes

__file__ = 'C:\\Documents and Settings\\James\\Desktop\\PythonWeb\\bricks\\bricks\\controller\\__init__.pyc'

__name__ = 'bricks.controller'

__path__ = ['C:\\Documents and Settings\\James\\Desktop\\PythonWeb\\bricks\\bricks\\controller']

Classes

class Controller

asd

Controller

Each exposed method is expected to return an iterable (talk about efficiency) It may also call start_response if it likes but only once. The default start response is called if not. Mappings are made to variables which don't start with an underscore.

def __call__(self, environ, start_response)

A very handy module for starting the class

[show source]
C:\Documents and Settings\James\Desktop\PythonWeb\bricks\bricks\controller\__init__.py

   32: def __call__(self, environ, start_response):
   33:     "A very ``handy`` module for *starting* the class"
   34:     called = []
   35:     self.view = View(environ)
   36:     #self.model = loadModel(environ)
   37:     def new_start_response(status, headers, exc_info=None):
   38:         if len(called) > 0:
   39:             raise Exception('start_response() already called')
   40:         called.append(1)
   41:         return start_response(status, headers, exc_info)
   42:     self.start_response = new_start_response
   43:     self.environ = environ
   44: 
   45:     path = self.environ['bricks.path'].split('/')
   46:     methodName = 'index'
   47:     args = []
   48:     if len(path) > 0 and path[0] <> '':
   49:         methodName = path[0]
   50:         args = path[1:]
   51: 
   52: 
   53: 
   54:     if not hasattr(self, methodName):
   55: 
   56:         raise web.wsgi.exception.HTTPError404('No such action %s'%methodName)
   57:     method = getattr(self, methodName)
   58:     if not hasattr(method,'expose'):
   59:         raise web.wsgi.exception.HTTPError401('Action %s is not exposed'%repr(methodName))
   60:     if method.expose == 1:
   61:         self.setup()
   62:         result = method(*args)
   63:         if len(called) > 0:
   64:             return result
   65:         else:
   66:             self.start_response('200 OK', [('Content-Type', 'text/html')])
   67:             return result            
   68:         #~ for data in method(*args):
   69:             #~ if len(called) > 0:
   70:                 #~ yield data
   71:             #~ else:
   72:                 #~ self.start_response('200 OK', [('Content-Type', 'text/html')])
   73:                 #~ yield data
   74:     else:
   75:         # Custom auth handling
   76:         authCalled = []
   77:         def auth_start_response(status, headers):
   78:             authCalled.append(1)
   79:             self.start_response(status, headers)
   80: 
   81:         def auth(self, auth_start_response, params):
   82:             if not params.has_key('app'):
   83:                 params['app'] = self.environ['bricks.app']
   84:             if not self.environ.has_key('web.auth'):
   85:                 raise Exception('No auth middleware found')
   86:             #if not self.environ.has_key('web.auth'):
   87:             #    raise Exception('No session middleware found for the auth middleware')
   88:             #store = environ['web.session'].store('auth')
   89:             #if not store.has_key('redirect'):
   90:             #    store['redirect'] = environ['bricks.url']
   91:             #    environ['database.connection'].commit()
   92:             if not self.environ.has_key('web.auth.user'): # No user signed in
   93:                 auth_start_response('403 User not signed in', [])
   94:             elif not self.environ['web.auth.user'].authorise(**params):
   95:                 auth_start_response('403 You do not have permission to access this application', [])
   96: 
   97:         handler = auth
   98:         if method.expose.has_key('handler') and method.expose['handler'] <> None:
   99:             handler = method.expose['handler']
  100:             del method.expose['handler']
  101:         handler(self, auth_start_response, method.expose)
  102: 
  103:         self.environ['database.connection'].commit()
  104:         if authCalled:
  105:             pass
  106: 
  107:         else:
  108:             result = method(*args)
  109:             if len(called) > 0:
  110:                 return result
  111:             else:
  112:                 self.start_response('200 OK', [('Content-Type', 'text/html')])
  113:                 return result            

def setup(self)
[show source]
C:\Documents and Settings\James\Desktop\PythonWeb\bricks\bricks\controller\__init__.py

   29: def setup(self):
   30:     pass