acq:oilsweb
This is an old revision of the document!
Table of Contents
oilsweb
oilsweb.* is a Python layer which augments Pylons. In general it provides additional CGI and session variable handling, ILS-specific context variables, and ILS-specific utility code. Below is a breakdown of the classes.
- oilsweb.lib.request.RequestMgr
- Aggregates the Pylons global request, response, session, and context objects
- Handles template rendering and skin path mangling
- Handles pre-render finalization, like saving session variables and inserting the ILS context into the Pylons context.
- oilsweb.lib.context.Context
- Global context object for managing CGI params, session variables, and runtime template variables
- Each oils application (e.g. /oils/acq) will define one or more SubContext objects, which specify the app-specific variables.
- A single variable is modeled as a ContextItem.
- oilsweb.lib.context.SubContext
- Each application will define one or more SubContext's to contain application variables
- oilsweb.lib.context.ContextItem
- A single CGI/session/template variable. All CGI/Session variables are automatically accessible to the template.
- The value for a ContextItem is stored in the defined field (e.g. ctx.acq.query), while all additional metadata (fields defined in the ContextItem constructor) are stored in a field by the same name, with the addition of a trailing underscore (e.g. ctx.acq.query_.cgi_name).
- The ContextItem contstructer offers the following flags:
- name - The variable name
- cgi_name - What name to store this variable under as a CGI param. The existance of this key implies the ability to pass this variable around from page to page. However, the variable will only be passed to the next page if explicitly requested, either by using the cgi_name in a form, or by calling context.make_query_string(), which encodes all cgi_name ContextItems whose value does not match the default.
- default_value - default value for the item, used if no value is provided via CGI. If this item is not a CGI param, the default_value can be any value, including references to functions.
- multi - If true, this item may contain an array of CGI values. This is only used if cgi_name is true
- session - It true, move this value to the session cache at page render time
Example
Defining the SubContext
Application contexts are generally defined in the __init__.py file in the application's controller directory (e.g. oilsweb/controllers/acq/init.py).
from oilsweb.lib.context import Context, SubContext, ContextItem class AcqContext(SubContext): def __init__(self): ... self.search_source = ContextItem(cgi_name='acq.ss', multi=True) # multiple sources are allowed self.picklist = ContextItem() # this value will not be passed around as a CGI param self.picklist_name = ContextItem(cgi_name='acq.pln') ... Context.apply_sub_context('acq', AcqContext)
Using the RequestMgr class
The RequestMgr wraps up some of the common workflow into a single class. One of its jobs is to maintain a reference to the current context.
def create(self, **kwargs): r = RequestMgr() if r.ctx.acq.picklist_name: # this variable comes directly from the acq SubContext definition # create new picklist using the provided name ... return r.render('acq/picklist/create.html')
Note, inside a template, the above context item would be referenced as "c.oils.acq.picklist_name".
Pre-defined Contexts
- CoreContext
- Maintains context items that are common to all applications. Examples include the logged in user, the authtoken, etc.
- Items in the core context are defined in the "core" namespace (e.g. r.ctx.core.authtoken)
- UtilContext
- Provides a set of generic function utilities, accessible from within a template.
acq/oilsweb.1202775967.txt.gz · Last modified: 2022/02/10 13:33 (external edit)