from Common import * from WebUtils import Funcs from HTTPContent import HTTPContent, HTTPContentError from Application import EndResponse class Page(HTTPContent): """The standard web page template. Page is a type of HTTPContent that is more convenient for servlets which represent HTML pages generated in response to GET and POST requests. In fact, this is the most common type of Servlet. Subclasses typically override `writeHeader`, `writeBody` and `writeFooter`. They might also choose to override `writeHTML` entirely. When developing a full-blown website, it's common to create a subclass of `Page` called `SitePage` which defines the common look and feel of the website and provides site-specific convenience methods. Then all other pages in your application then inherit from `SitePage`. """ ## Transactions ## def defaultAction(self): """The default action in a Page is to writeHTML().""" self.writeHTML() ## Generating results ## def title(self): """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. """ return self.__class__.__name__ def htTitle(self): """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. """ return self.title() def htBodyArgs(self): """The atrributes for the element. Returns the arguments used for the HTML 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. """ return 'text="black" bgcolor="white"' def writeHTML(self): """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 (be sure to call ``self.response().setHeader('Content-type', 'mime/type')`` in this case). """ self.writeDocType() self.writeln('') self.writeHead() self.writeBody() self.writeln('') def writeDocType(self): """Write the DOCTYPE tag. Invoked by `writeHTML` to write the ```` tag. By default this gives the HTML 4.01 Transitional DOCTYPE, which is a good guess for what most people send. Be warned, though, that some browsers render HTML differently based on the DOCTYPE (particular newer browsers, like Mozilla, do this). Subclasses may override to specify something else. You can find out more about doc types by searching for DOCTYPE on the web, or visiting: http://www.htmlhelp.com/tools/validator/doctype.html """ # @@ sgd-2003-01-29 - restored the 4.01 transitional as # per discussions on the mailing list for the 0.8 release. self.writeln('') def writeHead(self): """Write the element of the page. Writes the ```` portion of the page by writing the ``...`` tags and invoking `writeHeadParts` in between. """ wr = self.writeln wr('') self.writeHeadParts() wr('') def writeHeadParts(self): """Write the parts included in the element. Writes the parts inside the ``...`` tags. Invokes `writeTitle` and then `writeMetaData`, `writeStyleSheet` and `writeJavaScript`. Subclasses should override the `title` method and the three latter methods only. """ self.writeTitle() self.writeMetaData() self.writeStyleSheet() self.writeJavaScript() def writeTitle(self): """Write the element of the page. Writes the ``<title>`` portion of the page. Uses `title`, which is where you should override. """ self.writeln('\t<title>%s' % self.title()) def writeMetaData(self): """Write the meta data for the page. This default implementation does nothing. Subclasses should override if necessary. A typical implementation is: self.writeln('\t') """ pass def writeStyleSheet(self): """Write the CSS for the page. This default implementation does nothing. Subclasses should override if necessary. A typical implementation is: self.writeln('\t') """ pass def writeJavaScript(self): """Write the JavaScript for the page. This default implementation does nothing. Subclasses should override if necessary. A typical implementation is: self.writeln('\t') """ pass def writeBody(self): """Write the element of the page. Writes the ```` portion of the page by writing the ``...`` (making use of `htBodyArgs`) and invoking `writeBodyParts` in between. """ wr = self.writeln bodyArgs = self.htBodyArgs() if bodyArgs: wr('' % bodyArgs) else: wr('') self.writeBodyParts() wr('') def writeBodyParts(self): """Write the parts included in the 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`. """ self.writeContent() def writeContent(self): """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`. """ self.writeln('

This page has not yet customized its content.

') def preAction(self, actionName): """Things to do before actions. For a page, we first writeDocType(), , and then writeHead(). """ self.writeDocType() self.writeln('') self.writeHead() def postAction(self, actionName): """Things to do after actions. Simply close the html tag (). """ self.writeln('') ## Convenience Methods ## def htmlEncode(self, s): """HTML encode special characters. Alias for `WebUtils.Funcs.htmlEncode`, quotes the special characters &, <, >, and \" """ return Funcs.htmlEncode(s) def htmlDecode(self, s): """HTML decode special characters. Alias for `WebUtils.Funcs.htmlDecode`. Decodes HTML entities. """ return Funcs.htmlDecode(s) ## Validate HTML output (developer debugging) ## def validateHTML(self, closingTags=''): """Validate the response. Validate the current response data using Web Design Group's HTML validator available at http://www.htmlhelp.com/tools/validator/ Make sure you install the offline validator (called ``validate``) which can be called from the command-line. The ``validate`` script must be in your path. Add this method to your SitePage (the servlet from which all your servlets inherit), override Page.writeBodyParts() in your SitePage like so:: def writeBodyParts(self): Page.writeBodyParts() self.validateHTML() The ``closingtags`` param is a string which is appended to the page before validation. Typically, it would be the string ````. At the point this method is called (e.g. from `writeBodyParts`) the page is not yet 100% complete, so we have to fake it. """ # don't bother validating if the servlet has redirected status = self.response().header('status', None) if status and status.find('Redirect') != -1: return response = self.response().rawResponse() contents = response['contents'] + closingTags from WebUtils import WDGValidator errorText = WDGValidator.validateHTML(contents) if not errorText: return self.write(errorText)