# HG changeset patch # User paulb # Date 1195350154 0 # Node ID 23436560e9509ffdb8f44cbf346fae8bb6df67a8 # Parent bce6a0ba82b468ff2c03a1fa499f21050a3e2927 [project @ 2007-11-18 01:42:34 by paulb] Added modular request handling methods. diff -r bce6a0ba82b4 -r 23436560e950 XSLForms/Resources/WebResources.py --- a/XSLForms/Resources/WebResources.py Sun Nov 18 01:42:24 2007 +0000 +++ b/XSLForms/Resources/WebResources.py Sun Nov 18 01:42:34 2007 +0000 @@ -23,6 +23,7 @@ import XSLForms.Fields import XSLForms.Prepare import XSLForms.Output +import XSLForms.Utils import XSLForms.Resources.Common from XSLTools import XSLOutput import os @@ -76,10 +77,35 @@ * encoding - the assumed encoding of characters in request bodies + + To provide actual functionality to resources, either override the + 'respond_to_form' method and write the code for obtaining input, + initialising documents, creating output, and so on in that method, or + provide implementations for the following methods: + + * select_activity - sets the activity name which will be used by the + default implementations of the other methods + + * create_document - creates or obtains a document for the resource's + activity (need not be overridden) + + * respond_to_input - application logic relying on any input from the + request, including submitted document + information + + * init_document - initialises the document according to the + 'init_resources' attribute described above (need + not be overridden) + + * respond_to_document - application logic relying on any information + from the initialised document + + * create_output - creates and sends final output to the user (need + not be overridden) """ - #path_encoding = "utf-8" - #encoding = "utf-8" + path_encoding = "utf-8" + encoding = "utf-8" template_resources = {} in_page_resources = {} init_resources = {} @@ -296,12 +322,153 @@ documents). """ - trans.set_response_code(500) - trans.set_content_type(WebStack.Generic.ContentType("text/plain")) - out = trans.get_response_stream() - out.write("Resource not fully defined to respond.") + self.select_activity(trans, form) + self.create_document(trans, form) + self.respond_to_input(trans, form) + self.init_document(trans, form) + self.respond_to_document(trans, form) + self.create_output(trans, form) raise WebStack.Generic.EndOfResponse + # Modular methods for responding to requests. + + def select_activity(self, trans, form): + + """ + Using the given transaction 'trans' and 'form' information, select the + activity being performed and set the 'current_activity' attribute in the + transaction. + """ + + pass + + def create_document(self, trans, form): + + """ + Using the given transaction 'trans' and 'form' information, create the + document involved in the current activity and set the 'current_document' + attribute in the transaction. + + Return whether a new document was created. + """ + + documents = form.get_documents() + activity = self.get_activity(trans) + + if documents.has_key(activity): + self.set_document(trans, documents[activity]) + return 0 + else: + self.set_document(trans, form.new_instance(activity)) + return 1 + + def respond_to_input(self, trans, form): + + """ + Using the given transaction 'trans' and 'form' information, perform the + parts of the current activity which rely on the information supplied in + the current document. + """ + + pass + + def init_document(self, trans, form, stylesheet_parameters=None, + stylesheet_expressions=None, references=None): + + """ + Using the given transaction 'trans' and 'form' information, initialise + the current document. + """ + + # Transform, adding enumerations/ranges. + + init_xsl = self.prepare_initialiser(self.get_activity(trans)) + self.set_document( + trans, + self.get_result( + [init_xsl], self.get_document(trans), stylesheet_parameters, + stylesheet_expressions, references + ) + ) + + def respond_to_document(self, trans, form): + + """ + Using the given transaction 'trans' and 'form' information, perform the + parts of the current activity which rely on a populated version of the + current document. + """ + + pass + + def create_output(self, trans, form, content_type=None, + stylesheet_parameters=None, stylesheet_expressions=None, references=None): + + """ + Using the given transaction 'trans' and 'form' information, create the + output for the current activity using the previously set attributes in + the transaction. + """ + + attributes = trans.get_attributes() + in_page_resource = self.get_in_page_resource(trans) + parameters = form.get_parameters() + + # Start the response. + + encoding = attributes.get("encoding") or self.encoding + content_type = content_type or WebStack.Generic.ContentType("application/xhtml+xml", encoding) + trans.set_content_type(content_type) + + # Ensure that an output stylesheet exists. + + stylesheet_parameters = stylesheet_parameters or {} + + if in_page_resource in self.in_page_resources.keys(): + trans_xsl = self.prepare_fragment(in_page_resource) + stylesheet_parameters.update(self.prepare_parameters(parameters)) + else: + trans_xsl = self.prepare_output(self.get_activity(trans)) + + # Complete the response. + + self.send_output(trans, [trans_xsl], self.get_document(trans), + stylesheet_parameters, stylesheet_expressions, references) + + # Helper methods related to the modular handling of requests. + + def set_activity(self, trans, name): + trans.get_attributes()["current_activity"] = name + + def get_activity(self, trans): + return trans.get_attributes().get("current_activity") + + def set_document(self, trans, doc): + trans.get_attributes()["current_document"] = doc + + def get_document(self, trans): + return trans.get_attributes().get("current_document") + + # General helper methods. + + def add_elements(self, positions, *element_names): + + """ + At the given 'positions', typically obtained as "selectors", add the + hierarchy of elements given in the 'element_names' parameters. + """ + + XSLForms.Utils.add_elements(positions, *element_names) + + def remove_elements(self, positions): + + """ + Remove elements at the given 'positions', typically obtained as + "selectors". + """ + + XSLForms.Utils.remove_elements(positions) + def prepare_output(self, output_identifier): """