# HG changeset patch # User paulb # Date 1121377175 0 # Node ID 541381504c4db99c877e09b31c0be2694e5e1742 # Parent 7e8fc7d249412997416f4c7af356c6669b2645c1 [project @ 2005-07-14 21:39:35 by paulb] Changed the resource to be a higher-level XSLForms resource employing form objects and implementing the respond_to_form method. diff -r 7e8fc7d24941 -r 541381504c4d examples/Common/Configurator/__init__.py --- a/examples/Common/Configurator/__init__.py Thu Jul 14 21:38:44 2005 +0000 +++ b/examples/Common/Configurator/__init__.py Thu Jul 14 21:39:35 2005 +0000 @@ -3,15 +3,15 @@ "A WebStack application for a system configurator." import WebStack.Generic -import XSLForms.Fields import XSLForms.Output import XSLForms.Prepare import XSLForms.Utils +import XSLForms.Resources import XSLOutput import libxml2dom import os -class ConfiguratorResource: +class ConfiguratorResource(XSLForms.Resources.XSLFormsResource): "A resource providing a system configurator." @@ -23,61 +23,28 @@ "/hard-disks" : ("config_output_harddisks.xsl", "hard-disks-node") } - def get_fields_from_body(self, trans, encoding): - text = trans.get_request_stream().read().decode(encoding) - parameters = {} - for text_line in text.split("\r\n"): - text_parts = text_line.split("=") - text_name, text_value = text_parts[0], "=".join(text_parts[1:]) - if not parameters.has_key(text_name): - parameters[text_name] = [] - # NOTE: Workaround from posted text. - if text_value[-1] == "\x00": - text_value = text_value[:-1] - parameters[text_name].append(text_value) - return parameters + def respond_to_form(self, trans, form): - def respond(self, trans): - - global XSLForms # NOTE: Strangely required to avoid UnboundLocalError! + """ + Respond to a request having the given transaction 'trans' and the given + 'form' information. + """ - # Only obtain field information according to the stated method. - - method = trans.get_request_method() path_info = trans.get_path_info() - - if method == "GET": - fields = XSLForms.Fields.Fields(encoding="iso-8859-1", values_are_lists=1) - parameters = trans.get_fields_from_path() - documents = fields.make_documents(parameters.items()) - elif method == "POST": - fields = XSLForms.Fields.Fields(encoding=self.encoding, values_are_lists=1) - - # Handle requests for fragments. - - if path_info in self.in_page_resources.keys(): - parameters = self.get_fields_from_body(trans, self.encoding) - else: - parameters = trans.get_fields_from_body(self.encoding) - - # Get the XML representation of the request. - - documents = fields.make_documents(parameters.items()) - else: - trans.set_response_code(405) - raise WebStack.Generic.EndOfResponse + parameters = form.get_parameters() + documents = form.get_documents() + selectors = form.get_selectors() # Ensure the presence of a document. if documents.has_key("configuration"): configuration = documents["configuration"] else: - configuration = fields.new_instance("configuration") + configuration = form.new_instance("configuration") #print "*", configuration.toString() # Add and remove elements according to the selectors found. - selectors = fields.get_selectors(parameters.items(), documents) 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")