1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/XSLForms/Resources.py Thu Jul 14 21:38:44 2005 +0000
1.3 @@ -0,0 +1,99 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"Resources for use with WebStack."
1.7 +
1.8 +import WebStack.Generic
1.9 +import XSLForms.Fields
1.10 +
1.11 +class XSLFormsResource:
1.12 +
1.13 + "A generic XSLForms resource for use with WebStack."
1.14 +
1.15 + path_encoding = "iso-8859-1"
1.16 + encoding = "utf-8"
1.17 +
1.18 + def get_fields_from_body(self, trans, encoding):
1.19 +
1.20 + """
1.21 + From the given transaction 'trans' and using the stated text 'encoding'
1.22 + get the field values from the request body and return a dictionary
1.23 + mapping field names to lists of such values.
1.24 + """
1.25 +
1.26 + text = trans.get_request_stream().read().decode(encoding)
1.27 + parameters = {}
1.28 + for text_line in text.split("\r\n"):
1.29 + text_parts = text_line.split("=")
1.30 + text_name, text_value = text_parts[0], "=".join(text_parts[1:])
1.31 + if not parameters.has_key(text_name):
1.32 + parameters[text_name] = []
1.33 + # NOTE: Workaround from posted text.
1.34 + if text_value[-1] == "\x00":
1.35 + text_value = text_value[:-1]
1.36 + parameters[text_name].append(text_value)
1.37 + return parameters
1.38 +
1.39 + def respond(self, trans):
1.40 +
1.41 + """
1.42 + Respond to the request described by the given transaction 'trans'.
1.43 + """
1.44 +
1.45 + global XSLForms # NOTE: Strangely required to avoid UnboundLocalError!
1.46 +
1.47 + # Only obtain field information according to the stated method.
1.48 +
1.49 + method = trans.get_request_method()
1.50 + path_info = trans.get_path_info()
1.51 +
1.52 + # Handle typical request methods, processing request information.
1.53 +
1.54 + if method == "GET":
1.55 +
1.56 + # Get the fields from the request path (URL).
1.57 +
1.58 + form = XSLForms.Fields.Form(encoding=self.path_encoding, values_are_lists=1)
1.59 + parameters = trans.get_fields_from_path()
1.60 + form.set_parameters(parameters)
1.61 +
1.62 + elif method == "POST":
1.63 +
1.64 + # Get the fields from the request body.
1.65 +
1.66 + form = XSLForms.Fields.Form(encoding=self.encoding, values_are_lists=1)
1.67 +
1.68 + # Handle requests for in-page updates.
1.69 +
1.70 + if path_info in self.in_page_resources.keys():
1.71 + parameters = self.get_fields_from_body(trans, self.encoding)
1.72 + else:
1.73 + parameters = trans.get_fields_from_body(self.encoding)
1.74 +
1.75 + # Get the XML representation of the request.
1.76 +
1.77 + form.set_parameters(parameters)
1.78 +
1.79 + else:
1.80 +
1.81 + # Initialise empty containers.
1.82 +
1.83 + parameters = {}
1.84 + documents = {}
1.85 +
1.86 + # Call an overridden method with the processed request information.
1.87 +
1.88 + self.respond_to_form(trans, form)
1.89 +
1.90 + def respond_to_form(self, trans, form):
1.91 +
1.92 + """
1.93 + Respond to the request described by the given transaction 'trans', using
1.94 + the given 'form' object to conveniently retrieve field (request
1.95 + parameter) information and structured form information (as DOM-style XML
1.96 + documents).
1.97 + """
1.98 +
1.99 + trans.set_response_code(500)
1.100 + raise WebStack.Generic.EndOfResponse
1.101 +
1.102 +# vim: tabstop=4 expandtab shiftwidth=4