1.1 --- a/XSLForms/Resources/WebResources.py Sun Nov 18 01:42:24 2007 +0000
1.2 +++ b/XSLForms/Resources/WebResources.py Sun Nov 18 01:42:34 2007 +0000
1.3 @@ -23,6 +23,7 @@
1.4 import XSLForms.Fields
1.5 import XSLForms.Prepare
1.6 import XSLForms.Output
1.7 +import XSLForms.Utils
1.8 import XSLForms.Resources.Common
1.9 from XSLTools import XSLOutput
1.10 import os
1.11 @@ -76,10 +77,35 @@
1.12
1.13 * encoding - the assumed encoding of characters in request
1.14 bodies
1.15 +
1.16 + To provide actual functionality to resources, either override the
1.17 + 'respond_to_form' method and write the code for obtaining input,
1.18 + initialising documents, creating output, and so on in that method, or
1.19 + provide implementations for the following methods:
1.20 +
1.21 + * select_activity - sets the activity name which will be used by the
1.22 + default implementations of the other methods
1.23 +
1.24 + * create_document - creates or obtains a document for the resource's
1.25 + activity (need not be overridden)
1.26 +
1.27 + * respond_to_input - application logic relying on any input from the
1.28 + request, including submitted document
1.29 + information
1.30 +
1.31 + * init_document - initialises the document according to the
1.32 + 'init_resources' attribute described above (need
1.33 + not be overridden)
1.34 +
1.35 + * respond_to_document - application logic relying on any information
1.36 + from the initialised document
1.37 +
1.38 + * create_output - creates and sends final output to the user (need
1.39 + not be overridden)
1.40 """
1.41
1.42 - #path_encoding = "utf-8"
1.43 - #encoding = "utf-8"
1.44 + path_encoding = "utf-8"
1.45 + encoding = "utf-8"
1.46 template_resources = {}
1.47 in_page_resources = {}
1.48 init_resources = {}
1.49 @@ -296,12 +322,153 @@
1.50 documents).
1.51 """
1.52
1.53 - trans.set_response_code(500)
1.54 - trans.set_content_type(WebStack.Generic.ContentType("text/plain"))
1.55 - out = trans.get_response_stream()
1.56 - out.write("Resource not fully defined to respond.")
1.57 + self.select_activity(trans, form)
1.58 + self.create_document(trans, form)
1.59 + self.respond_to_input(trans, form)
1.60 + self.init_document(trans, form)
1.61 + self.respond_to_document(trans, form)
1.62 + self.create_output(trans, form)
1.63 raise WebStack.Generic.EndOfResponse
1.64
1.65 + # Modular methods for responding to requests.
1.66 +
1.67 + def select_activity(self, trans, form):
1.68 +
1.69 + """
1.70 + Using the given transaction 'trans' and 'form' information, select the
1.71 + activity being performed and set the 'current_activity' attribute in the
1.72 + transaction.
1.73 + """
1.74 +
1.75 + pass
1.76 +
1.77 + def create_document(self, trans, form):
1.78 +
1.79 + """
1.80 + Using the given transaction 'trans' and 'form' information, create the
1.81 + document involved in the current activity and set the 'current_document'
1.82 + attribute in the transaction.
1.83 +
1.84 + Return whether a new document was created.
1.85 + """
1.86 +
1.87 + documents = form.get_documents()
1.88 + activity = self.get_activity(trans)
1.89 +
1.90 + if documents.has_key(activity):
1.91 + self.set_document(trans, documents[activity])
1.92 + return 0
1.93 + else:
1.94 + self.set_document(trans, form.new_instance(activity))
1.95 + return 1
1.96 +
1.97 + def respond_to_input(self, trans, form):
1.98 +
1.99 + """
1.100 + Using the given transaction 'trans' and 'form' information, perform the
1.101 + parts of the current activity which rely on the information supplied in
1.102 + the current document.
1.103 + """
1.104 +
1.105 + pass
1.106 +
1.107 + def init_document(self, trans, form, stylesheet_parameters=None,
1.108 + stylesheet_expressions=None, references=None):
1.109 +
1.110 + """
1.111 + Using the given transaction 'trans' and 'form' information, initialise
1.112 + the current document.
1.113 + """
1.114 +
1.115 + # Transform, adding enumerations/ranges.
1.116 +
1.117 + init_xsl = self.prepare_initialiser(self.get_activity(trans))
1.118 + self.set_document(
1.119 + trans,
1.120 + self.get_result(
1.121 + [init_xsl], self.get_document(trans), stylesheet_parameters,
1.122 + stylesheet_expressions, references
1.123 + )
1.124 + )
1.125 +
1.126 + def respond_to_document(self, trans, form):
1.127 +
1.128 + """
1.129 + Using the given transaction 'trans' and 'form' information, perform the
1.130 + parts of the current activity which rely on a populated version of the
1.131 + current document.
1.132 + """
1.133 +
1.134 + pass
1.135 +
1.136 + def create_output(self, trans, form, content_type=None,
1.137 + stylesheet_parameters=None, stylesheet_expressions=None, references=None):
1.138 +
1.139 + """
1.140 + Using the given transaction 'trans' and 'form' information, create the
1.141 + output for the current activity using the previously set attributes in
1.142 + the transaction.
1.143 + """
1.144 +
1.145 + attributes = trans.get_attributes()
1.146 + in_page_resource = self.get_in_page_resource(trans)
1.147 + parameters = form.get_parameters()
1.148 +
1.149 + # Start the response.
1.150 +
1.151 + encoding = attributes.get("encoding") or self.encoding
1.152 + content_type = content_type or WebStack.Generic.ContentType("application/xhtml+xml", encoding)
1.153 + trans.set_content_type(content_type)
1.154 +
1.155 + # Ensure that an output stylesheet exists.
1.156 +
1.157 + stylesheet_parameters = stylesheet_parameters or {}
1.158 +
1.159 + if in_page_resource in self.in_page_resources.keys():
1.160 + trans_xsl = self.prepare_fragment(in_page_resource)
1.161 + stylesheet_parameters.update(self.prepare_parameters(parameters))
1.162 + else:
1.163 + trans_xsl = self.prepare_output(self.get_activity(trans))
1.164 +
1.165 + # Complete the response.
1.166 +
1.167 + self.send_output(trans, [trans_xsl], self.get_document(trans),
1.168 + stylesheet_parameters, stylesheet_expressions, references)
1.169 +
1.170 + # Helper methods related to the modular handling of requests.
1.171 +
1.172 + def set_activity(self, trans, name):
1.173 + trans.get_attributes()["current_activity"] = name
1.174 +
1.175 + def get_activity(self, trans):
1.176 + return trans.get_attributes().get("current_activity")
1.177 +
1.178 + def set_document(self, trans, doc):
1.179 + trans.get_attributes()["current_document"] = doc
1.180 +
1.181 + def get_document(self, trans):
1.182 + return trans.get_attributes().get("current_document")
1.183 +
1.184 + # General helper methods.
1.185 +
1.186 + def add_elements(self, positions, *element_names):
1.187 +
1.188 + """
1.189 + At the given 'positions', typically obtained as "selectors", add the
1.190 + hierarchy of elements given in the 'element_names' parameters.
1.191 + """
1.192 +
1.193 + XSLForms.Utils.add_elements(positions, *element_names)
1.194 +
1.195 + def remove_elements(self, positions):
1.196 +
1.197 + """
1.198 + Remove elements at the given 'positions', typically obtained as
1.199 + "selectors".
1.200 + """
1.201 +
1.202 + XSLForms.Utils.remove_elements(positions)
1.203 +
1.204 def prepare_output(self, output_identifier):
1.205
1.206 """