# HG changeset patch # User paulb # Date 1195350170 0 # Node ID 4693ada989ee1d5705bd33e026b1e9a5374901bc # Parent 23436560e9509ffdb8f44cbf346fae8bb6df67a8 [project @ 2007-11-18 01:42:45 by paulb] Changed the example to use the modular request handling methods. diff -r 23436560e950 -r 4693ada989ee examples/Common/Candidate/__init__.py --- a/examples/Common/Candidate/__init__.py Sun Nov 18 01:42:34 2007 +0000 +++ b/examples/Common/Candidate/__init__.py Sun Nov 18 01:42:50 2007 +0000 @@ -4,8 +4,8 @@ import WebStack.Generic from WebStack.Repositories.Directory import DirectoryRepository -from XSLForms.Resources.WebResources import XSLFormsResource, input, output, resources -import XSLForms.Utils +from XSLForms.Resources.WebResources import \ + XSLFormsResource, input, output, resources, prepare_resources as xslforms_prepare_resources import os import libxml2dom @@ -36,15 +36,15 @@ def __init__(self, repository): self.repository = repository - def respond_to_form(self, trans, form): + def select_activity(self, trans, form): + self.set_activity(trans, "admin") - """ - Respond to a request having the given transaction 'trans' and the given - 'form' information. - """ + def create_document(self, trans, form): + is_new = XSLFormsResource.create_document(self, trans, form) + trans.get_attributes()["form_is_new"] = is_new + return is_new - parameters = form.get_parameters() - documents = form.get_documents() + def respond_to_input(self, trans, form): # Get the "show" and "edit" resource paths. # NOTE: These should be obtained from the site map. @@ -53,15 +53,6 @@ show_path = trans.get_path_without_info() + trans.update_path(vpath, "show") edit_path = trans.get_path_without_info() + trans.update_path(vpath, "edit") - # Ensure the presence of a document. - - form_is_new = 0 - if documents.has_key("admin"): - admin = documents["admin"] - else: - admin = form.new_instance("admin") - form_is_new = 1 - # Redirect if one of the CVs is to be shown or edited. selectors = form.get_selectors() @@ -76,13 +67,12 @@ # Add and remove elements according to the selectors found. - XSLForms.Utils.remove_elements(selectors.get("remove")) - XSLForms.Utils.add_elements(selectors.get("new"), "cv", "cvs") + self.remove_elements(selectors.get("remove")) + self.add_elements(selectors.get("new"), "cv", "cvs") - # Transform, adding enumerations/ranges. + def respond_to_document(self, trans, form): - init_xsl = self.prepare_initialiser("admin") - admin = self.get_result([init_xsl], admin) + admin = self.get_document(trans) # Synchronise the repository with the CVs found. @@ -92,33 +82,49 @@ name = key[len("candidate-"):] # NOTE: Apostrophes not quoted. if not cvs.xpath("cv[@name = '%s']" % name): - if form_is_new: + if trans.get_attributes()["form_is_new"]: cv = admin.createElement("cv") cv.setAttribute("name", name) cvs.appendChild(cv) else: del self.repository[key] - # Start the response. +class CandidateUtils: + + "Methods used by candidate-related resources." + + def __init__(self, repository): + self.repository = repository - trans.set_content_type(WebStack.Generic.ContentType("application/xhtml+xml", encoding)) + def select_activity(self, trans, form): + self.set_activity(trans, "candidate") - # Ensure that an output stylesheet exists. + def create_document(self, trans, form): + documents = form.get_documents() + fields = trans.get_fields_from_path() + name = fields.get("name", [u"None"])[0] - trans_xsl = self.prepare_output("admin") - stylesheet_parameters = {} + # Ensure the presence of a document. - # Complete the response. + if documents.has_key("candidate"): + self.set_document(trans, documents["candidate"]) + else: + if self.repository is None or not self.repository.has_key("candidate-%s" % name): + self.set_document(trans, form.new_instance("candidate")) + else: + self.set_document(trans, libxml2dom.parseString(self.repository["candidate-%s" % name])) - self.send_output(trans, [trans_xsl], admin, stylesheet_parameters) + def init_document(self, trans, form): + status_xml = self.prepare_document("status") + XSLFormsResource.init_document(self, trans, form, references={"status" : status_xml}) -class DisplayResource(XSLForms.Resources.WebResources.XSLFormsResource): +class DisplayResource(CandidateUtils, XSLFormsResource): "A resource providing editing facilities for a job candidate profile." resource_dir = resources(__file__) template_resources = { - "candidate_display" : output("candidate_display_template.xhtml") + "candidate" : output("candidate_display_template.xhtml") } init_resources = { "candidate" : input("candidate_template.xhtml") @@ -127,52 +133,7 @@ "status" : "candidate_status.xml" } - def __init__(self, repository): - self.repository = repository - - def respond_to_form(self, trans, form): - - """ - Respond to a request having the given transaction 'trans' and the given - 'form' information. - """ - - parameters = form.get_parameters() - documents = form.get_documents() - fields = trans.get_fields_from_path() - name = fields.get("name", [u"None"])[0] - - # Ensure the presence of a document. - - if documents.has_key("candidate"): - candidate = documents["candidate"] - else: - if self.repository is None or not self.repository.has_key("candidate-%s" % name): - candidate = form.new_instance("candidate") - else: - candidate = libxml2dom.parseString(self.repository["candidate-%s" % name]) - - # Transform, adding enumerations/ranges. - - init_xsl = self.prepare_initialiser("candidate") - status_xml = self.prepare_document("status") - candidate = self.get_result([init_xsl], candidate, references={"status" : status_xml}) - - # Start the response. - - trans.set_content_type(WebStack.Generic.ContentType("application/xhtml+xml", encoding)) - - # Ensure that an output stylesheet exists. - # Handle the "show" operation. - - trans_xsl = self.prepare_output("candidate_display") - stylesheet_parameters = {} - - # Complete the response. - - self.send_output(trans, [trans_xsl], candidate, stylesheet_parameters) - -class CandidateResource(XSLForms.Resources.WebResources.XSLFormsResource): +class CandidateResource(CandidateUtils, XSLFormsResource): "A resource providing editing facilities for a job candidate profile." @@ -187,18 +148,20 @@ "status" : "candidate_status.xml" } - def __init__(self, repository): - self.repository = repository + def respond_to_input(self, trans, form): - def respond_to_form(self, trans, form): + # Add and remove elements according to the selectors found. - """ - Respond to a request having the given transaction 'trans' and the given - 'form' information. - """ + selectors = form.get_selectors() + self.remove_elements(selectors.get("remove")) + self.add_elements(selectors.get("add-skill"), "skill", "skills") + self.add_elements(selectors.get("add-qualification"), "qualification", "qualifications") + self.add_elements(selectors.get("add-employment"), "employment", "experience") + def respond_to_document(self, trans, form): + + candidate = self.get_document(trans) parameters = form.get_parameters() - documents = form.get_documents() fields = trans.get_fields_from_path() name = fields.get("name", [u"None"])[0] @@ -209,30 +172,6 @@ show_path = trans.get_path_without_info() + trans.update_path(vpath, "show") admin_path = trans.get_path_without_info() + trans.update_path(vpath, "") - # Ensure the presence of a document. - - if documents.has_key("candidate"): - candidate = documents["candidate"] - else: - if self.repository is None or not self.repository.has_key("candidate-%s" % name): - candidate = form.new_instance("candidate") - else: - candidate = libxml2dom.parseString(self.repository["candidate-%s" % name]) - - # Add and remove elements according to the selectors found. - - selectors = form.get_selectors() - XSLForms.Utils.remove_elements(selectors.get("remove")) - XSLForms.Utils.add_elements(selectors.get("add-skill"), "skill", "skills") - XSLForms.Utils.add_elements(selectors.get("add-qualification"), "qualification", "qualifications") - XSLForms.Utils.add_elements(selectors.get("add-employment"), "employment", "experience") - - # Transform, adding enumerations/ranges. - - init_xsl = self.prepare_initialiser("candidate") - status_xml = self.prepare_document("status") - candidate = self.get_result([init_xsl], candidate, references={"status" : status_xml}) - # Redirect if the CV is to be shown. if parameters.has_key("show"): @@ -252,19 +191,6 @@ self.repository["candidate-%s" % name] = candidate.toString() trans.redirect(trans.encode_path(admin_path)) - # Start the response. - - trans.set_content_type(WebStack.Generic.ContentType("application/xhtml+xml", encoding)) - - # Ensure that an output stylesheet exists. - - trans_xsl = self.prepare_output("candidate") - stylesheet_parameters = {} - - # Complete the response. - - self.send_output(trans, [trans_xsl], candidate, stylesheet_parameters) - # Site map initialisation. def get_site(fsencoding=None, use_cwd=0): @@ -297,6 +223,6 @@ def prepare_resources(): for cls in [AdminResource, DisplayResource, CandidateResource]: - XSLForms.Resources.WebResources.prepare_resources(cls) + xslforms_prepare_resources(cls) # vim: tabstop=4 expandtab shiftwidth=4 diff -r 23436560e950 -r 4693ada989ee examples/Common/Configurator/__init__.py --- a/examples/Common/Configurator/__init__.py Sun Nov 18 01:42:34 2007 +0000 +++ b/examples/Common/Configurator/__init__.py Sun Nov 18 01:42:50 2007 +0000 @@ -3,8 +3,8 @@ "A WebStack application for a system configurator." import WebStack.Generic -import XSLForms.Utils -import XSLForms.Resources.WebResources +from XSLForms.Resources.WebResources import \ + XSLFormsResource, input, output, resources, prepare_resources as xslforms_prepare_resources import os # Site map imports. @@ -19,16 +19,16 @@ # Resource classes. -class ConfiguratorResource(XSLForms.Resources.WebResources.XSLFormsResource): +class ConfiguratorResource(XSLFormsResource): "A resource providing a system configurator." - resource_dir = os.path.join(os.path.split(__file__)[0], "Resources") + resource_dir = resources(__file__) template_resources = { - "configuration" : ("config_template.xhtml", "config_output.xsl") + "configuration" : output("config_template.xhtml") } init_resources = { - "configuration" : ("config_template.xhtml", "config_input.xsl") + "configuration" : input("config_template.xhtml") } transform_resources = { "filter" : ["config_filter.xsl"] @@ -52,97 +52,74 @@ "accessories" : ("configuration", "config_output_accessories.xsl", "accessories-node") } - def respond_to_form(self, trans, form): + def select_activity(self, trans, form): + self.set_activity(trans, "configuration") - """ - Respond to a request having the given transaction 'trans' and the given - 'form' information. - """ - - in_page_resource = self.get_in_page_resource(trans) - parameters = form.get_parameters() - documents = form.get_documents() + def respond_to_input(self, trans, form): # Creating selected elements. selectors = form.get_selectors(create=1) - # Ensure the presence of a document. - - if documents.has_key("configuration"): - configuration = documents["configuration"] - else: - configuration = form.new_instance("configuration") - # Add and remove elements according to the selectors found. - XSLForms.Utils.add_elements(selectors.get("add-memory-unit"), "memory-unit") - XSLForms.Utils.remove_elements(selectors.get("remove-memory-unit")) - XSLForms.Utils.add_elements(selectors.get("add-storage-unit"), "storage-unit") - XSLForms.Utils.remove_elements(selectors.get("remove-storage-unit")) - XSLForms.Utils.add_elements(selectors.get("add-hard-disk"), "hard-disk") - XSLForms.Utils.remove_elements(selectors.get("remove-hard-disk")) + self.add_elements(selectors.get("add-memory-unit"), "memory-unit") + self.remove_elements(selectors.get("remove-memory-unit")) + self.add_elements(selectors.get("add-storage-unit"), "storage-unit") + self.remove_elements(selectors.get("remove-storage-unit")) + self.add_elements(selectors.get("add-hard-disk"), "hard-disk") + self.remove_elements(selectors.get("remove-hard-disk")) # Send a response according to certain parameters. # When exported, an XML version of the data is returned. + parameters = form.get_parameters() + if parameters.has_key("export"): trans.set_content_type(WebStack.Generic.ContentType("text/xml", encoding)) - configuration.toStream(trans.get_response_stream(), trans.get_response_stream_encoding()) + self.get_document(trans).toStream(trans.get_response_stream(), trans.get_response_stream_encoding()) + raise WebStack.Generic.EndOfResponse - # When not exported, the data is transformed to produce a normal Web - # page. + def init_document(self, trans, form): - else: - - # Transform, adding enumerations/ranges. + # Transform, adding enumerations/ranges. - init_xsl = self.prepare_initialiser("configuration") - configuration = self.get_result([init_xsl], configuration, - references={ - "accessories" : self.prepare_document("accessories"), - "base-system" : self.prepare_document("base-system"), - "cpu" : self.prepare_document("cpu"), - "hard-disk" : self.prepare_document("hard-disk"), - "keyboard" : self.prepare_document("keyboard"), - "memory-unit" : self.prepare_document("memory-unit"), - "mouse" : self.prepare_document("mouse"), - "screen" : self.prepare_document("screen"), - "storage-unit" : self.prepare_document("storage-unit") - }) + XSLFormsResource.init_document(self, trans, form, + references={ + "accessories" : self.prepare_document("accessories"), + "base-system" : self.prepare_document("base-system"), + "cpu" : self.prepare_document("cpu"), + "hard-disk" : self.prepare_document("hard-disk"), + "keyboard" : self.prepare_document("keyboard"), + "memory-unit" : self.prepare_document("memory-unit"), + "mouse" : self.prepare_document("mouse"), + "screen" : self.prepare_document("screen"), + "storage-unit" : self.prepare_document("storage-unit") + }) - # Filter out inappropriate choices. - - filter_xsl_list = self.prepare_transform("filter") - configuration = self.get_result(filter_xsl_list, configuration) + def respond_to_document(self, trans, form): - # Start the response. + # Filter out inappropriate choices. - trans.set_content_type(WebStack.Generic.ContentType("application/xhtml+xml", encoding)) - - # Ensure that an output stylesheet exists. + filter_xsl_list = self.prepare_transform("filter") + self.set_document(trans, self.get_result(filter_xsl_list, self.get_document(trans))) - if in_page_resource in self.in_page_resources.keys(): - trans_xsl = self.prepare_fragment(in_page_resource) - stylesheet_parameters = self.prepare_parameters(parameters) - else: - trans_xsl = self.prepare_output("configuration") - stylesheet_parameters = {} + def create_output(self, trans, form): + + # Complete the response. + + stylesheet_parameters = {} - # Complete the response. + try: + stylesheet_parameters["locale"] = trans.get_content_languages()[0] + except IndexError: + pass - try: - stylesheet_parameters["locale"] = trans.get_content_languages()[0] - except IndexError: - pass - self.send_output(trans, [trans_xsl], configuration, stylesheet_parameters, - references={"translations" : self.prepare_document("translations")}) - - #from XSLTools import XSLOutput - #import sys - #proc = XSLOutput.Processor([trans_xsl], parameters=stylesheet_parameters, - # references={"translations" : self.prepare_document("translations")}) - #proc.send_output(sys.stderr, "iso-8859-1", configuration) + XSLFormsResource.create_output(self, trans, form, + stylesheet_parameters=stylesheet_parameters, + references={ + "translations" : self.prepare_document("translations") + }) # Site map initialisation. @@ -176,6 +153,6 @@ def prepare_resources(): for cls in [ConfiguratorResource]: - XSLForms.Resources.WebResources.prepare_resources(cls) + xslforms_prepare_resources(cls) # vim: tabstop=4 expandtab shiftwidth=4