8.3.1 The SimpleAppContext Class

##
8.3.1 The `SimpleAppContext` Class

The `SimpleAppContext` class is intended to be used for applications which store state at the browser in hidden HTML fields. An inheritance diagram illustrates the relationship to the `SimpleContext` class described above.

Figure: The `SimpleAppContext` class

The methods available in `SimpleAppContext` and the location of their definition are show below.

Method | Mixin
--- | ---
`absolute_base_url()` | `AppContext`
`add_header(name, value)` | `ResponseMixin`
`add_session_vars(*names)` | `SessionBase`
`base_url()` | `AppContext`
`clear_locals()` | `AppContext`
`current_url()` | `AppContext`
`decode_session(text)` | `SessionBase`
`default_session_var(name, value)` | `SessionBase`
`del_header(name)` | `ResponseMixin`
`del_session_vars(*names)` | `SessionBase`
`encode_session()` | `HiddenFieldSessionMixin`
`eval_expr(expr)` | `NamespaceMixin`
`flush_content()` | `ExecuteMixin`
`flush_html()` | `ExecuteMixin`
`form_close()` | `SimpleAppContext`
`form_open()` | `NameRecorderMixin`
`get_header(name)` | `ResponseMixin`
`get_lookup(name)` | `AppContext`
`get_macro(name)` | `AppContext`
`get_macro_arg(name)` | `ExecuteMixin`
`get_tagclass(name)` | `AppContext`
`get_value(name)` | `NamespaceMixin`
`has_value(name)` | `NamespaceMixin`
`has_values(*names)` | `NamespaceMixin`
`input_add(itype, name, unused_value, return_list)` | `NameRecorderMixin`
`load_session()` | `HiddenFieldSessionMixin`
`load_template(name)` | `AppContext`
`load_template_once(name)` | `AppContext`
`make_alias(name)` | `NamespaceMixin`
`merge_request()` | `NameRecorderMixin`
`parsed_request_uri()` | `AppContext`
`pop_content_trap()` | `ExecuteMixin`
`pop_macro_args()` | `ExecuteMixin`
`pop_page(target_page)` | `AppContext`
`push_content_trap()` | `ExecuteMixin`
`push_macro_args(args)` | `ExecuteMixin`
`push_page(name, *args)` | `AppContext`
`redirect(loc)` | `AppContext`
`redirect_url(loc)` | `AppContext`
`register_lookup(name, lookup)` | `AppContext`
`register_macro(name, macro)` | `AppContext`
`remove_session()` | `SessionBase`
`req_equals(name)` | `AppContext`
`reset_content()` | `ExecuteMixin`
`run_template(name)` | `AppContext`
`run_template_once(name)` | `AppContext`
`save_session()` | `HiddenFieldSessionMixin`
`send_content(data)` | `ResponseMixin`
`send_redirect(loc)` | `ResponseMixin`
`session_vars()` | `SessionBase`
`set_globals(dict)` | `NamespaceMixin`
`set_header(name, value)` | `ResponseMixin`
`set_page(name, *args)` | `AppContext`
`set_request(req)` | `AppContext`
`set_save_session(flag)` | `SessionBase`
`set_value(name, value)` | `NamespaceMixin`
`should_save_session()` | `SessionBase`
`write_content(data)` | `ExecuteMixin`
`write_headers()` | `ResponseMixin`

The `SimpleAppContext` class provides the following functionality to your application.

- Application state is stored inside hidden fields in the HTML.

  This function is performed by the `HiddenFieldSessionMixin` mixin class which places a hidden field named `__albstate__` inside each HTML form constructed using `<al-form>` tags. The session data is pickled, compressed, then base64 encoded. No encryption is performed, so this is not suitable for storing sensitive data.

  If you refer back to the Albatross application processing sequence described in section 4.1 you will note where the session is loaded into and saved from the context. These steps correspond to the `load_session()` and `save_session()` methods of the execution context respectively.

  In the `HiddenFieldSessionMixin` class, the `load_session()` method retrieves the encoded session data from the `__albstate__` field in the browser request. The `save_session()` method does not do anything because the session has already been saved into each form produced in the HTML output.

- All input fields in an HTML form which are left empty by the browser will be set to `None` when the request is merged into the local namespace.

  The `NameRecorderMixin` mixin class encodes the names of all `<al-input>` fields in the form inside a hidden field named `__albform__`.

  Any input field which is left empty in the browser when the form is submitted will not exist in the browser request to the server. When the toolkit merges the browser request into the application context, the `__albform__` field is used to detect the fields missing from the browser request.

The methods implemented in the `SimpleAppContext` class are:

`__init__`( | app)

  When you inherit from the `SimpleAppContext` class you must call this constructor.

  The app argument is passed to the `AppContext` constructor.

`form_close`( | )

  Invokes the `form_close()` method of the `NameRecorderMixin` class and `encode_session()` of the `HiddenFieldSessionMixin` class.