chula.www.controller.base – Base controller

Generic base controller used by all web requests.

class chula.www.controller.base.Controller(env, config)

The Controller class helps manage all web requests. This is done by all requests being an instance of this class, or a subclass thereof, see: isinstance().

__init__(env, config)

Initialize the web request, performing tasks common to all web requests.

Parameters:

Default chula.www.adapters.env.BaseEnv.content_type is set to text/html if not specified.

_gc()

Complete garbage collection. The intended purpose is to allow consolidated garbage collection specific to each project. This method gets called in the adapter just before sending data to the browser.

Warning

Do not call this method, it gets called automatically.

_pre_session_persist()

Provide mechanism for removing items from session just prior to being persisted. This is useful when you want to have unserializeable objects that need to be casted to a different type before being persisted to the database. This usually means casting to a json encodable type.

>>> from chula.www.controller import base
>>>
>>> class Foo(base.Controller):
...     def _pre_session_persist(self):
...         self.database_connection.close()
...         del self.database_connection
>>>
execute()

Provide a consistent method name for execution by the handler. This method is to be rebound by the UrlMapper.

Warning

Do not call this method, it gets called automatically.

redirect(destination, type='TEMPORARY')

Redirect the browser to another page.

Parameters:
  • destination (str) – URL of target destination
  • type – Type of redirection to make
Return type:

str

Currently the supported redirection types are:

Example of how to perform a redirection

>>> from chula.www.controller import base
>>>
>>> class Foo(base.Controller):
...     def foo(self):
...         url = '/some/other/page'
...         return self.redirect(url)
>>>

Note

This method needs to support integer values as well.

Useful attributes

chula.www.controller.base.content_type

Attribute holding the chula.www.adapters.env.BaseEnv.content_type. Mutating this value will determine the outgoing HTTP Content-Type.

Here’s how you would set the content type inside a controller:

>>> from chula import json
>>> from chula.www.controller import base
>>>
>>> class Foo(base.Controller):
...     def bar(self):
...         self.content_type = 'application/json'
...         payload = {'testing':'hello world'}
...         return json.dumps(payload, indent=2)
>>>
chula.www.controller.base.config

Attribute holding the chula.config.Config object.

chula.www.controller.base.env

Attribute holding the chula.www.adapters.env.BaseEnv object.

Example of how to reference GET variables:

>>> from chula.www.controller import base
>>>
>>> class Foo(base.Controller):
...     def http_get_variables(self):
...         html = []
...         html.append('<ul>')
...         for key, value in self.env.form_get.iteritems():
...             html.append('<li>%s = %s</li>' % (key, value))
...         html.append('</ul>')
...         return ''.join(html)
>>>

Example of how to reference POST variables:

>>> from chula.www.controller import base
>>>
>>> class Foo(base.Controller):
...     def http_get_variables(self):
...         html = []
...         html.append('<ul>')
...         for key, value in self.env.form_post.iteritems():
...             html.append('<li>%s = %s</li>' % (key, value))
...         html.append('</ul>')
...         return ''.join(html)
>>>

Example of how to reference raw HTTP POST data:

>>> from chula.www.controller import base
>>>
>>> class Foo(base.Controller):
...     def http_get_variables(self):
...         return self.env.form_raw
>>>
chula.www.controller.base.form

Attribute referencing chula.www.adapters.env.BaseEnv.form. Rember this attribute is just a pointer to the combined form attribute, if you need more control reference one of the form_foo attributes in chula.www.adapters.env.BaseEnv.form.

Example of how to reference form variables:

>>> from chula.www.controller import base
>>>
>>> class Foo(base.Controller):
...     def form_variables(self):
...         html = []
...         html.append('<ul>')
...         for key, value in self.form.iteritems():
...             html.append('<li>%s = %s</li>' % (key, value))
...         html.append('</ul>')
...         return ''.join(html)
>>>
chula.www.controller.base.log

Attribute holding chula.logger.Logger object. The intended use of this attribute is to allow app specific logging. The default configuration will send error messags to chula.config.Config.log and debug messages (and higher) to chula.config.Config.log suffixed with .debug

chula.www.controller.base.model

Attribute holding chula.collection.Collection object. The intended use of this attribute is to populate with data, and pass it to the view.

chula.www.controller.base.session

Attribute holding an instance of chula.session.Session object.

Note

This attribute only exists if chula.config.Config.session is True

Note

This attribute is also automatically exposed to the model attribute.

Table Of Contents

Previous topic

chula.www.adapters.env – HTTP environment

Next topic

chula.www.controller.error – Error controller

This Page