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