SidebarPage

Webware page template class for pages with a sidebar.

class SidebarPage.SidebarPage

Bases: Page

Webware page template class for pages with a sidebar.

SidebarPage is an abstract superclass for pages that have a sidebar (as well as a header and “content well”). Sidebars are normally used for navigation (e.g., a menu or list of links), showing small bits of info and occasionally a simple form (such as login or search).

Subclasses should override cornerTitle(), writeSidebar() and writeContent() (and title() if necessary; see Page).

The utility methods menuHeading() and menuItem() can be used by subclasses, typically in their implementation of writeSidebar().

Webware itself uses this class: Examples/ExamplePage and Admin/AdminPage both inherit from it.

__init__()

Subclasses must invoke super.

actions()

The allowed actions.

Returns a list or a set of method names that are allowable actions from HTML forms. The default implementation returns []. See _respond for more about actions.

application()

The Application instance we’re using.

awake(transaction)

Let servlet awake.

Makes instance variables from the transaction. This is where Page becomes unthreadsafe, as the page is tied to the transaction. This is also what allows us to implement functions like write, where you don’t need to pass in the transaction or response.

callMethodOfServlet(url, method, *args, **kwargs)

Call a method of another servlet.

See Application.callMethodOfServlet for details. The main difference is that here you don’t have to pass in the transaction as the first argument.

canBeReused()

Returns whether a single servlet instance can be reused.

The default is True, but subclasses con override to return False. Keep in mind that performance may seriously be degraded if instances can’t be reused. Also, there’s no known good reasons not to reuse an instance. Remember the awake() and sleep() methods are invoked for every transaction. But just in case, your servlet can refuse to be reused.

canBeThreaded()

Declares whether servlet can be threaded.

Returns False because of the instance variables we set up in awake.

close()
cornerTitle()
defaultAction()

The default action in a Page is to writeHTML().

static endResponse()

End response.

When this method is called during awake or respond, servlet processing will end immediately, and the accumulated response will be sent.

Note that sleep will still be called, providing a chance to clean up or free any resources.

forward(url)

Forward request.

Forwards this request to another servlet. See Application.forward for details. The main difference is that here you don’t have to pass in the transaction as the first argument.

handleAction(action)

Handle action.

Invoked by _respond when a legitimate action has been found in a form. Invokes preAction, the actual action method and postAction.

Subclasses rarely override this method.

htBodyArgs()

The attributes for the <body> element.

Returns the arguments used for the HTML <body> tag. Invoked by writeBody().

With the prevalence of stylesheets (CSS), you can probably skip this particular HTML feature, but for historical reasons this sets the page to black text on white.

htRootArgs()

The attributes for the <html> element.

Returns the arguments used for the root HTML tag. Invoked by writeHTML() and preAction().

Authors are encouraged to specify a lang attribute, giving the document’s language.

htTitle()

The page title as HTML.

Return self.title(). Subclasses sometimes override this to provide an HTML enhanced version of the title. This is the method that should be used when including the page title in the actual page contents.

static htmlDecode(s)

HTML decode special characters.

Alias for WebUtils.Funcs.htmlDecode. Decodes HTML entities.

static htmlEncode(s)

HTML encode special characters. Alias for WebUtils.Funcs.htmlEncode, quotes the special characters &, <, >, and “

includeURL(url)

Include output from other servlet.

Includes the response of another servlet in the current servlet’s response. See Application.includeURL for details. The main difference is that here you don’t have to pass in the transaction as the first argument.

lastModified(_trans)

Get time of last modification.

Return this object’s Last-Modified time (as a float), or None (meaning don’t know or not applicable).

log(message)

Log a message.

This can be invoked to print messages concerning the servlet. This is often used by self to relay important information back to developers.

menuHeading(title)
menuItem(title, url=None, suffix=None, indentLevel=1)
methodNameForAction(name)

Return method name for an action name.

Invoked by _respond() to determine the method name for a given action name which has been derived as the value of an _action_ field. Since this is usually the label of an HTML submit button in a form, it is often needed to transform it in order to get a valid method name (for instance, blanks could be replaced by underscores and the like). This default implementation of the name transformation is the identity, it simply returns the name. Subclasses should override this method when action names don’t match their method names; they could “mangle” the action names or look the method names up in a dictionary.

name()

Return the name which is simple the name of the class.

Subclasses should not override this method. It is used for logging and debugging.

static notImplemented(trans)
open()
outputEncoding()

Get the default output encoding of the application.

postAction(actionName)

Things to do after actions.

Simply close the html tag (</html>).

preAction(actionName)

Things to do before actions.

For a page, we first writeDocType(), <html>, and then writeHead().

request()

The request (HTTPRequest) we’re handling.

respond(transaction)

Respond to a request.

Invokes the appropriate respondToSomething() method depending on the type of request (e.g., GET, POST, PUT, …).

respondToGet(transaction)

Respond to GET.

Invoked in response to a GET request method. All methods are passed to _respond.

respondToHead(trans)

Respond to a HEAD request.

A correct but inefficient implementation.

respondToPost(transaction)

Respond to POST.

Invoked in response to a POST request method. All methods are passed to _respond.

response()

The response (HTTPResponse) we’re handling.

runMethodForTransaction(transaction, method, *args, **kw)
static runTransaction(transaction)
sendRedirectAndEnd(url, status=None)

Send redirect and end.

Sends a redirect back to the client and ends the response. This is a very popular pattern.

sendRedirectPermanentAndEnd(url)

Send permanent redirect and end.

sendRedirectSeeOtherAndEnd(url)

Send redirect to a URL to be retrieved with GET and end.

This is the proper method for the Post/Redirect/Get pattern.

sendRedirectTemporaryAndEnd(url)

Send temporary redirect and end.

serverSidePath(path=None)

Return the filesystem path of the page on the server.

session()

The session object.

This provides a state for the current user (associated with a browser instance, really). If no session exists, then a session will be created.

sessionEncode(url=None)

Utility function to access Session.sessionEncode.

Takes a url and adds the session ID as a parameter. This is for cases where you don’t know if the client will accepts cookies.

setFactory(factory)
sleep(transaction)

Let servlet sleep again.

We unset some variables. Very boring.

title()

The page title.

Subclasses often override this method to provide a custom title. This title should be absent of HTML tags. This implementation returns the name of the class, which is sometimes appropriate and at least informative.

transaction()

The Transaction we’re currently handling.

static urlDecode(s)

Turn special % characters into actual characters.

This method does the same as the urllib.unquote_plus() function.

static urlEncode(s)

Quotes special characters using the % substitutions.

This method does the same as the urllib.quote_plus() function.

write(*args)

Write to output.

Writes the arguments, which are turned to strings (with str) and concatenated before being written to the response. Unicode strings must be encoded before they can be written.

writeBanner()
writeBody()

Write the <body> element of the page.

Writes the <body> portion of the page by writing the <body>...</body> (making use of htBodyArgs) and invoking writeBodyParts in between.

writeBodyParts()

Write the parts included in the <body> element.

Invokes writeContent. Subclasses should only override this method to provide additional page parts such as a header, sidebar and footer, that a subclass doesn’t normally have to worry about writing.

For writing page-specific content, subclasses should override writeContent instead. This method is intended to be overridden by your SitePage.

See SidebarPage for an example override of this method.

Invoked by writeBody.

writeContent()

Write the unique, central content for the page.

Subclasses should override this method (not invoking super) to write their unique page content.

Invoked by writeBodyParts.

writeContextsMenu()
writeDocType()

Write the DOCTYPE tag.

Invoked by writeHTML to write the <!DOCTYPE ...> tag.

By default this gives the HTML 5 DOCTYPE.

Subclasses may override to specify something else.

writeExceptionReport(handler)

Write extra information to the exception report.

The handler argument is the exception handler, and information is written there (using writeTitle, write, and writeln). This information is added to the exception report.

See ExceptionHandler for more information.

writeHTML()

Write all the HTML for the page.

Subclasses may override this method (which is invoked by _respond) or more commonly its constituent methods, writeDocType, writeHead and writeBody.

You will want to override this method if:
  • you want to format the entire HTML page yourself

  • if you want to send an HTML page that has already been generated

  • if you want to use a template that generates the entire page

  • if you want to send non-HTML content; in this case, be sure to call self.response().setHeader(‘Content-Type’, ‘mime/type’).

writeHead()

Write the <head> element of the page.

Writes the <head> portion of the page by writing the <head>...</head> tags and invoking writeHeadParts in between.

writeHeadParts()

Write the parts included in the <head> element.

Writes the parts inside the <head>...</head> tags. Invokes writeTitle and then writeMetaData, writeStyleSheet and writeJavaScript. Subclasses should override the title method and the three latter methods only.

writeJavaScript()

Write the JavaScript for the page.

This default implementation does nothing. Subclasses should override if necessary.

A typical implementation is:

self.writeln('<script src="ajax.js"></script>')
writeMetaData()

Write the meta data for the page.

This default implementation only specifies the output encoding. Subclasses should override if necessary.

writeSidebar()
writeStyleSheet()

We’re using a simple internal style sheet.

This way we avoid having to care about where an external style sheet should be located when this class is used in another context.

writeTitle()

Write the <title> element of the page.

Writes the <title> portion of the page. Uses title, which is where you should override.

writeVersions()
writeWebwareDocsMenu()
writeWebwareExitsMenu()
writeWebwareSidebarSections()

Write sidebar sections.

This method (and consequently the methods it invokes) are provided for Webware’s example and admin pages. It writes sections such as contexts, e-mails, exits and versions.

writeln(*args)

Write to output with newline.

Writes the arguments (like write), adding a newline after. Unicode strings must be encoded before they can be written.