1 #!/usr/bin/env python 2 3 "Resources for use with WebStack." 4 5 import WebStack.Generic 6 import XSLForms.Fields 7 8 class XSLFormsResource: 9 10 "A generic XSLForms resource for use with WebStack." 11 12 path_encoding = "iso-8859-1" 13 encoding = "utf-8" 14 15 def get_fields_from_body(self, trans, encoding): 16 17 """ 18 From the given transaction 'trans' and using the stated text 'encoding' 19 get the field values from the request body and return a dictionary 20 mapping field names to lists of such values. 21 """ 22 23 text = trans.get_request_stream().read().decode(encoding) 24 parameters = {} 25 for text_line in text.split("\r\n"): 26 text_parts = text_line.split("=") 27 text_name, text_value = text_parts[0], "=".join(text_parts[1:]) 28 if not parameters.has_key(text_name): 29 parameters[text_name] = [] 30 # NOTE: Workaround from posted text. 31 if text_value[-1] == "\x00": 32 text_value = text_value[:-1] 33 parameters[text_name].append(text_value) 34 return parameters 35 36 def respond(self, trans): 37 38 """ 39 Respond to the request described by the given transaction 'trans'. 40 """ 41 42 global XSLForms # NOTE: Strangely required to avoid UnboundLocalError! 43 44 # Only obtain field information according to the stated method. 45 46 method = trans.get_request_method() 47 path_info = trans.get_path_info() 48 49 # Handle typical request methods, processing request information. 50 51 if method == "GET": 52 53 # Get the fields from the request path (URL). 54 55 form = XSLForms.Fields.Form(encoding=self.path_encoding, values_are_lists=1) 56 parameters = trans.get_fields_from_path() 57 form.set_parameters(parameters) 58 59 elif method == "POST": 60 61 # Get the fields from the request body. 62 63 form = XSLForms.Fields.Form(encoding=self.encoding, values_are_lists=1) 64 65 # Handle requests for in-page updates. 66 67 if path_info in self.in_page_resources.keys(): 68 parameters = self.get_fields_from_body(trans, self.encoding) 69 else: 70 parameters = trans.get_fields_from_body(self.encoding) 71 72 # Get the XML representation of the request. 73 74 form.set_parameters(parameters) 75 76 else: 77 78 # Initialise empty containers. 79 80 parameters = {} 81 documents = {} 82 83 # Call an overridden method with the processed request information. 84 85 self.respond_to_form(trans, form) 86 87 def respond_to_form(self, trans, form): 88 89 """ 90 Respond to the request described by the given transaction 'trans', using 91 the given 'form' object to conveniently retrieve field (request 92 parameter) information and structured form information (as DOM-style XML 93 documents). 94 """ 95 96 trans.set_response_code(500) 97 raise WebStack.Generic.EndOfResponse 98 99 # vim: tabstop=4 expandtab shiftwidth=4