8.2 The AppContext Class
Description: The AppContext Class
Keywords: albatross

#
8.2 The `AppContext` Class

All execution contexts used by Albatross application classes are derived from the `AppContext` class. The class acts as a proxy and redirects all `ResourceMixin` and `TemplateLoader` class methods to the application object. This allows the application to cache resources so that they do not need to be defined for every request.

The class places very few assumptions about how you wish to structure and deploy your application and is suitable as a base class for all application execution context classes.

Figure: The `AppContext` class

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

Method | Mixin
--- | ---
`absolute_base_url()` | `AppContext`
`base_url()` | `AppContext`
`clear_locals()` | `AppContext`
`current_url()` | `AppContext`
`del_header(name)` | `ResponseMixin`
`eval_expr(expr)` | `NamespaceMixin`
`flush_content()` | `ExecuteMixin`
`flush_html()` | `ExecuteMixin`
`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`
`load_template(name)` | `AppContext`
`load_template_once(name)` | `AppContext`
`make_alias(name)` | `NamespaceMixin`
`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`
`req_equals(name)` | `AppContext`
`reset_content()` | `ExecuteMixin`
`run_template(name)` | `AppContext`
`run_template_once(name)` | `AppContext`
`send_content(data)` | `ResponseMixin`
`send_redirect(loc)` | `ResponseMixin`
`set_globals(dict)` | `NamespaceMixin`
`set_header(name, value)` | `ResponseMixin`
`set_page(name, *args)` | `AppContext`
`set_request(req)` | `AppContext`
`set_value(name, value)` | `NamespaceMixin`
`write_content(data)` | `ExecuteMixin`
`write_headers()` | `ResponseMixin`

There are a number of new methods and attributes introduced by the class.

`__init__`( | app)

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

  The app argument specifies the application object. This is saved in the `app` member.

`app`
  Stores the app argument to the constructor.

`get_macro`( | name)

  Returns the result of the `get_macro()` method of the application in the `app` member.

`register_macro`( | name, macro)

  Returns the result of the `register_macro()` method of the application in the `app` member.

`get_lookup`( | name)

  Returns the result of the `get_lookup()` method of the application in the `app` member.

`register_lookup`( | name, lookup)

  Returns the result of the `register_lookup()` method of the application in the `app` member.

`get_tagclass`( | name)

  Returns the result of the `get_tagclass()` method of the application in the `app` member.

`load_template`( | name)

  Returns the result of the `load_template()` method of the application in the `app` member.

`load_template_once`( | name)

  Returns the result of the `load_template_once()` method of the application in the `app` member.

`run_template`( | name)

  Calls the application `load_template()` method to load the template specified in the name argument and sets the execution context global namespace to the globals of the function which called this method. The template `to_html()` method is then called to execute the template.

`run_template_once`( | name)

  Calls the application `load_template_once()` method. If the template specified in the name argument is loaded or reloaded the method sets the execution context global namespace to the globals of the function which called this method, then the template `to_html()` method is then called to execute the template.

`clear_locals`( | )

  Overrides the `NamespaceMixin` `clear_locals()` method to retain the `__page__` local namespace value.

`set_page`( | name [, ...])

  Sets the current application page to that specified in the name argument. If changing pages and there is a current page defined then before changing pages the `page_leave()` function/method will be called for the current page. The `locals.__page__` member is then set to name and the new page is loaded. Any addition arguments passed to the method will be passed to the `page_enter()` function/method code which is associated with the new page.

  Refer to page in section 7.10 for an explanation of page functions/methods.

`push_page`( | name [, ...])

  Pushes an application page onto the page stack. The current page can be returned to by calling the `pop_page()` method. The `page_leave()` function/method of the current page is not called. The new page is loaded and it's `page_enter()` function/method is called. Any additional arguments given will be passed to the `page_enter()` function/method associated with the new page.

`pop_page`( | )

  Pops the current page from the page stack and returns to the page that was current when the `push_page()` method was called. The `page_leave()` function/method of the current page is called prior to loading the original page. The `page_enter()` function/method of the original page is not called.

`set_request`( | req)

  Saves the browser request specified in the req argument as the `request`.

`req_equals`( | name)

  Returns whether or not the browser request contains a non-empty field with a name which matches the name argument.

`base_url`( | )

  Returns the result of the application `base_url()` method.

`current_url`( | )

  Returns the path component (see the standard `urlparse` module) of the URI used to request the current page.

`absolute_base_url`( | )

  Returns the base_url parameter to the application constructor transformed into an absolute URL.

`redirect_url`( | loc)

  Returns an absolute URL for the application page identifier specified in the loc parameter.

`redirect`( | loc)

  Raises a `Redirect` exception requesting a redirect to the location in the loc parameter from the application `run()` method.

  If the loc parameter contains either a scheme or netloc (from the standard `urlparse` module), or begins with a ``/'' then is it used without modification for the `Redirect` exception. Other forms of loc are assumed to be page identifiers and are passed to `redirect_url()` before being raised as a `Redirect` exception.