# HG changeset patch # User paulb # Date 1121377124 0 # Node ID 7e8fc7d249412997416f4c7af356c6669b2645c1 # Parent 6b270cffc3d4144a810c259c91a9ed6ddb8f9d2c [project @ 2005-07-14 21:38:44 by paulb] Introduced a Form class in XSLForms.Fields which provides a nicer abstraction over processed request parameters. Added XSLForms.Resources which provides WebStack resource classes that permit subclasses to implement a higher-level method that deals with form information directly (rather than having to explicitly process the field data). diff -r 6b270cffc3d4 -r 7e8fc7d24941 XSLForms/Fields.py --- a/XSLForms/Fields.py Thu Jul 14 16:18:48 2005 +0000 +++ b/XSLForms/Fields.py Thu Jul 14 21:38:44 2005 +0000 @@ -40,7 +40,7 @@ class FieldsError(Exception): pass -class Fields: +class FieldProcessor: """ A class which converts fields in the documented form to XML @@ -307,6 +307,32 @@ return libxml2dom.createDocument(EMPTY_NAMESPACE, name, None) +# NOTE: Legacy name exposure. + +Fields = FieldProcessor + +class Form(FieldProcessor): + + "A collection of documents processed from form fields." + + def __init__(self, *args, **kw): + FieldProcessor.__init__(self, *args, **kw) + self.parameters = {} + self.documents = {} + + def set_parameters(self, parameters): + self.parameters = parameters + self.documents = self.make_documents(self.parameters.items()) + + def get_parameters(self): + return self.parameters + + def get_documents(self): + return self.documents + + def get_selectors(self): + return FieldProcessor.get_selectors(self, self.parameters.items(), self.documents) + if __name__ == "__main__": items = [ @@ -362,7 +388,7 @@ # Create an object to interpret the test data. - fields = Fields("iso-8859-1") + fields = FieldProcessor("iso-8859-1") t = time.time() documents = fields.make_documents(items) diff -r 6b270cffc3d4 -r 7e8fc7d24941 XSLForms/Resources.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/XSLForms/Resources.py Thu Jul 14 21:38:44 2005 +0000 @@ -0,0 +1,99 @@ +#!/usr/bin/env python + +"Resources for use with WebStack." + +import WebStack.Generic +import XSLForms.Fields + +class XSLFormsResource: + + "A generic XSLForms resource for use with WebStack." + + path_encoding = "iso-8859-1" + encoding = "utf-8" + + def get_fields_from_body(self, trans, encoding): + + """ + From the given transaction 'trans' and using the stated text 'encoding' + get the field values from the request body and return a dictionary + mapping field names to lists of such values. + """ + + 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(self, trans): + + """ + Respond to the request described by the given transaction 'trans'. + """ + + global XSLForms # NOTE: Strangely required to avoid UnboundLocalError! + + # Only obtain field information according to the stated method. + + method = trans.get_request_method() + path_info = trans.get_path_info() + + # Handle typical request methods, processing request information. + + if method == "GET": + + # Get the fields from the request path (URL). + + form = XSLForms.Fields.Form(encoding=self.path_encoding, values_are_lists=1) + parameters = trans.get_fields_from_path() + form.set_parameters(parameters) + + elif method == "POST": + + # Get the fields from the request body. + + form = XSLForms.Fields.Form(encoding=self.encoding, values_are_lists=1) + + # Handle requests for in-page updates. + + 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. + + form.set_parameters(parameters) + + else: + + # Initialise empty containers. + + parameters = {} + documents = {} + + # Call an overridden method with the processed request information. + + self.respond_to_form(trans, form) + + def respond_to_form(self, trans, form): + + """ + Respond to the request described by the given transaction 'trans', using + the given 'form' object to conveniently retrieve field (request + parameter) information and structured form information (as DOM-style XML + documents). + """ + + trans.set_response_code(500) + raise WebStack.Generic.EndOfResponse + +# vim: tabstop=4 expandtab shiftwidth=4