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
 
\includegraphics[]{appcontext}

There are a few 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.

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.9 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.

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.

send_content(data)
This overrides the send_content() method defined in the ExecuteMixin class and redirects the content to the browser request write_content() method.