1.1 --- a/examples/Common/Candidate/__init__.py Sun Nov 18 01:42:34 2007 +0000
1.2 +++ b/examples/Common/Candidate/__init__.py Sun Nov 18 01:42:50 2007 +0000
1.3 @@ -4,8 +4,8 @@
1.4
1.5 import WebStack.Generic
1.6 from WebStack.Repositories.Directory import DirectoryRepository
1.7 -from XSLForms.Resources.WebResources import XSLFormsResource, input, output, resources
1.8 -import XSLForms.Utils
1.9 +from XSLForms.Resources.WebResources import \
1.10 + XSLFormsResource, input, output, resources, prepare_resources as xslforms_prepare_resources
1.11 import os
1.12 import libxml2dom
1.13
1.14 @@ -36,15 +36,15 @@
1.15 def __init__(self, repository):
1.16 self.repository = repository
1.17
1.18 - def respond_to_form(self, trans, form):
1.19 + def select_activity(self, trans, form):
1.20 + self.set_activity(trans, "admin")
1.21
1.22 - """
1.23 - Respond to a request having the given transaction 'trans' and the given
1.24 - 'form' information.
1.25 - """
1.26 + def create_document(self, trans, form):
1.27 + is_new = XSLFormsResource.create_document(self, trans, form)
1.28 + trans.get_attributes()["form_is_new"] = is_new
1.29 + return is_new
1.30
1.31 - parameters = form.get_parameters()
1.32 - documents = form.get_documents()
1.33 + def respond_to_input(self, trans, form):
1.34
1.35 # Get the "show" and "edit" resource paths.
1.36 # NOTE: These should be obtained from the site map.
1.37 @@ -53,15 +53,6 @@
1.38 show_path = trans.get_path_without_info() + trans.update_path(vpath, "show")
1.39 edit_path = trans.get_path_without_info() + trans.update_path(vpath, "edit")
1.40
1.41 - # Ensure the presence of a document.
1.42 -
1.43 - form_is_new = 0
1.44 - if documents.has_key("admin"):
1.45 - admin = documents["admin"]
1.46 - else:
1.47 - admin = form.new_instance("admin")
1.48 - form_is_new = 1
1.49 -
1.50 # Redirect if one of the CVs is to be shown or edited.
1.51
1.52 selectors = form.get_selectors()
1.53 @@ -76,13 +67,12 @@
1.54
1.55 # Add and remove elements according to the selectors found.
1.56
1.57 - XSLForms.Utils.remove_elements(selectors.get("remove"))
1.58 - XSLForms.Utils.add_elements(selectors.get("new"), "cv", "cvs")
1.59 + self.remove_elements(selectors.get("remove"))
1.60 + self.add_elements(selectors.get("new"), "cv", "cvs")
1.61
1.62 - # Transform, adding enumerations/ranges.
1.63 + def respond_to_document(self, trans, form):
1.64
1.65 - init_xsl = self.prepare_initialiser("admin")
1.66 - admin = self.get_result([init_xsl], admin)
1.67 + admin = self.get_document(trans)
1.68
1.69 # Synchronise the repository with the CVs found.
1.70
1.71 @@ -92,33 +82,49 @@
1.72 name = key[len("candidate-"):]
1.73 # NOTE: Apostrophes not quoted.
1.74 if not cvs.xpath("cv[@name = '%s']" % name):
1.75 - if form_is_new:
1.76 + if trans.get_attributes()["form_is_new"]:
1.77 cv = admin.createElement("cv")
1.78 cv.setAttribute("name", name)
1.79 cvs.appendChild(cv)
1.80 else:
1.81 del self.repository[key]
1.82
1.83 - # Start the response.
1.84 +class CandidateUtils:
1.85 +
1.86 + "Methods used by candidate-related resources."
1.87 +
1.88 + def __init__(self, repository):
1.89 + self.repository = repository
1.90
1.91 - trans.set_content_type(WebStack.Generic.ContentType("application/xhtml+xml", encoding))
1.92 + def select_activity(self, trans, form):
1.93 + self.set_activity(trans, "candidate")
1.94
1.95 - # Ensure that an output stylesheet exists.
1.96 + def create_document(self, trans, form):
1.97 + documents = form.get_documents()
1.98 + fields = trans.get_fields_from_path()
1.99 + name = fields.get("name", [u"None"])[0]
1.100
1.101 - trans_xsl = self.prepare_output("admin")
1.102 - stylesheet_parameters = {}
1.103 + # Ensure the presence of a document.
1.104
1.105 - # Complete the response.
1.106 + if documents.has_key("candidate"):
1.107 + self.set_document(trans, documents["candidate"])
1.108 + else:
1.109 + if self.repository is None or not self.repository.has_key("candidate-%s" % name):
1.110 + self.set_document(trans, form.new_instance("candidate"))
1.111 + else:
1.112 + self.set_document(trans, libxml2dom.parseString(self.repository["candidate-%s" % name]))
1.113
1.114 - self.send_output(trans, [trans_xsl], admin, stylesheet_parameters)
1.115 + def init_document(self, trans, form):
1.116 + status_xml = self.prepare_document("status")
1.117 + XSLFormsResource.init_document(self, trans, form, references={"status" : status_xml})
1.118
1.119 -class DisplayResource(XSLForms.Resources.WebResources.XSLFormsResource):
1.120 +class DisplayResource(CandidateUtils, XSLFormsResource):
1.121
1.122 "A resource providing editing facilities for a job candidate profile."
1.123
1.124 resource_dir = resources(__file__)
1.125 template_resources = {
1.126 - "candidate_display" : output("candidate_display_template.xhtml")
1.127 + "candidate" : output("candidate_display_template.xhtml")
1.128 }
1.129 init_resources = {
1.130 "candidate" : input("candidate_template.xhtml")
1.131 @@ -127,52 +133,7 @@
1.132 "status" : "candidate_status.xml"
1.133 }
1.134
1.135 - def __init__(self, repository):
1.136 - self.repository = repository
1.137 -
1.138 - def respond_to_form(self, trans, form):
1.139 -
1.140 - """
1.141 - Respond to a request having the given transaction 'trans' and the given
1.142 - 'form' information.
1.143 - """
1.144 -
1.145 - parameters = form.get_parameters()
1.146 - documents = form.get_documents()
1.147 - fields = trans.get_fields_from_path()
1.148 - name = fields.get("name", [u"None"])[0]
1.149 -
1.150 - # Ensure the presence of a document.
1.151 -
1.152 - if documents.has_key("candidate"):
1.153 - candidate = documents["candidate"]
1.154 - else:
1.155 - if self.repository is None or not self.repository.has_key("candidate-%s" % name):
1.156 - candidate = form.new_instance("candidate")
1.157 - else:
1.158 - candidate = libxml2dom.parseString(self.repository["candidate-%s" % name])
1.159 -
1.160 - # Transform, adding enumerations/ranges.
1.161 -
1.162 - init_xsl = self.prepare_initialiser("candidate")
1.163 - status_xml = self.prepare_document("status")
1.164 - candidate = self.get_result([init_xsl], candidate, references={"status" : status_xml})
1.165 -
1.166 - # Start the response.
1.167 -
1.168 - trans.set_content_type(WebStack.Generic.ContentType("application/xhtml+xml", encoding))
1.169 -
1.170 - # Ensure that an output stylesheet exists.
1.171 - # Handle the "show" operation.
1.172 -
1.173 - trans_xsl = self.prepare_output("candidate_display")
1.174 - stylesheet_parameters = {}
1.175 -
1.176 - # Complete the response.
1.177 -
1.178 - self.send_output(trans, [trans_xsl], candidate, stylesheet_parameters)
1.179 -
1.180 -class CandidateResource(XSLForms.Resources.WebResources.XSLFormsResource):
1.181 +class CandidateResource(CandidateUtils, XSLFormsResource):
1.182
1.183 "A resource providing editing facilities for a job candidate profile."
1.184
1.185 @@ -187,18 +148,20 @@
1.186 "status" : "candidate_status.xml"
1.187 }
1.188
1.189 - def __init__(self, repository):
1.190 - self.repository = repository
1.191 + def respond_to_input(self, trans, form):
1.192
1.193 - def respond_to_form(self, trans, form):
1.194 + # Add and remove elements according to the selectors found.
1.195
1.196 - """
1.197 - Respond to a request having the given transaction 'trans' and the given
1.198 - 'form' information.
1.199 - """
1.200 + selectors = form.get_selectors()
1.201 + self.remove_elements(selectors.get("remove"))
1.202 + self.add_elements(selectors.get("add-skill"), "skill", "skills")
1.203 + self.add_elements(selectors.get("add-qualification"), "qualification", "qualifications")
1.204 + self.add_elements(selectors.get("add-employment"), "employment", "experience")
1.205
1.206 + def respond_to_document(self, trans, form):
1.207 +
1.208 + candidate = self.get_document(trans)
1.209 parameters = form.get_parameters()
1.210 - documents = form.get_documents()
1.211 fields = trans.get_fields_from_path()
1.212 name = fields.get("name", [u"None"])[0]
1.213
1.214 @@ -209,30 +172,6 @@
1.215 show_path = trans.get_path_without_info() + trans.update_path(vpath, "show")
1.216 admin_path = trans.get_path_without_info() + trans.update_path(vpath, "")
1.217
1.218 - # Ensure the presence of a document.
1.219 -
1.220 - if documents.has_key("candidate"):
1.221 - candidate = documents["candidate"]
1.222 - else:
1.223 - if self.repository is None or not self.repository.has_key("candidate-%s" % name):
1.224 - candidate = form.new_instance("candidate")
1.225 - else:
1.226 - candidate = libxml2dom.parseString(self.repository["candidate-%s" % name])
1.227 -
1.228 - # Add and remove elements according to the selectors found.
1.229 -
1.230 - selectors = form.get_selectors()
1.231 - XSLForms.Utils.remove_elements(selectors.get("remove"))
1.232 - XSLForms.Utils.add_elements(selectors.get("add-skill"), "skill", "skills")
1.233 - XSLForms.Utils.add_elements(selectors.get("add-qualification"), "qualification", "qualifications")
1.234 - XSLForms.Utils.add_elements(selectors.get("add-employment"), "employment", "experience")
1.235 -
1.236 - # Transform, adding enumerations/ranges.
1.237 -
1.238 - init_xsl = self.prepare_initialiser("candidate")
1.239 - status_xml = self.prepare_document("status")
1.240 - candidate = self.get_result([init_xsl], candidate, references={"status" : status_xml})
1.241 -
1.242 # Redirect if the CV is to be shown.
1.243
1.244 if parameters.has_key("show"):
1.245 @@ -252,19 +191,6 @@
1.246 self.repository["candidate-%s" % name] = candidate.toString()
1.247 trans.redirect(trans.encode_path(admin_path))
1.248
1.249 - # Start the response.
1.250 -
1.251 - trans.set_content_type(WebStack.Generic.ContentType("application/xhtml+xml", encoding))
1.252 -
1.253 - # Ensure that an output stylesheet exists.
1.254 -
1.255 - trans_xsl = self.prepare_output("candidate")
1.256 - stylesheet_parameters = {}
1.257 -
1.258 - # Complete the response.
1.259 -
1.260 - self.send_output(trans, [trans_xsl], candidate, stylesheet_parameters)
1.261 -
1.262 # Site map initialisation.
1.263
1.264 def get_site(fsencoding=None, use_cwd=0):
1.265 @@ -297,6 +223,6 @@
1.266
1.267 def prepare_resources():
1.268 for cls in [AdminResource, DisplayResource, CandidateResource]:
1.269 - XSLForms.Resources.WebResources.prepare_resources(cls)
1.270 + xslforms_prepare_resources(cls)
1.271
1.272 # vim: tabstop=4 expandtab shiftwidth=4
2.1 --- a/examples/Common/Configurator/__init__.py Sun Nov 18 01:42:34 2007 +0000
2.2 +++ b/examples/Common/Configurator/__init__.py Sun Nov 18 01:42:50 2007 +0000
2.3 @@ -3,8 +3,8 @@
2.4 "A WebStack application for a system configurator."
2.5
2.6 import WebStack.Generic
2.7 -import XSLForms.Utils
2.8 -import XSLForms.Resources.WebResources
2.9 +from XSLForms.Resources.WebResources import \
2.10 + XSLFormsResource, input, output, resources, prepare_resources as xslforms_prepare_resources
2.11 import os
2.12
2.13 # Site map imports.
2.14 @@ -19,16 +19,16 @@
2.15
2.16 # Resource classes.
2.17
2.18 -class ConfiguratorResource(XSLForms.Resources.WebResources.XSLFormsResource):
2.19 +class ConfiguratorResource(XSLFormsResource):
2.20
2.21 "A resource providing a system configurator."
2.22
2.23 - resource_dir = os.path.join(os.path.split(__file__)[0], "Resources")
2.24 + resource_dir = resources(__file__)
2.25 template_resources = {
2.26 - "configuration" : ("config_template.xhtml", "config_output.xsl")
2.27 + "configuration" : output("config_template.xhtml")
2.28 }
2.29 init_resources = {
2.30 - "configuration" : ("config_template.xhtml", "config_input.xsl")
2.31 + "configuration" : input("config_template.xhtml")
2.32 }
2.33 transform_resources = {
2.34 "filter" : ["config_filter.xsl"]
2.35 @@ -52,97 +52,74 @@
2.36 "accessories" : ("configuration", "config_output_accessories.xsl", "accessories-node")
2.37 }
2.38
2.39 - def respond_to_form(self, trans, form):
2.40 + def select_activity(self, trans, form):
2.41 + self.set_activity(trans, "configuration")
2.42
2.43 - """
2.44 - Respond to a request having the given transaction 'trans' and the given
2.45 - 'form' information.
2.46 - """
2.47 -
2.48 - in_page_resource = self.get_in_page_resource(trans)
2.49 - parameters = form.get_parameters()
2.50 - documents = form.get_documents()
2.51 + def respond_to_input(self, trans, form):
2.52
2.53 # Creating selected elements.
2.54
2.55 selectors = form.get_selectors(create=1)
2.56
2.57 - # Ensure the presence of a document.
2.58 -
2.59 - if documents.has_key("configuration"):
2.60 - configuration = documents["configuration"]
2.61 - else:
2.62 - configuration = form.new_instance("configuration")
2.63 -
2.64 # Add and remove elements according to the selectors found.
2.65
2.66 - XSLForms.Utils.add_elements(selectors.get("add-memory-unit"), "memory-unit")
2.67 - XSLForms.Utils.remove_elements(selectors.get("remove-memory-unit"))
2.68 - XSLForms.Utils.add_elements(selectors.get("add-storage-unit"), "storage-unit")
2.69 - XSLForms.Utils.remove_elements(selectors.get("remove-storage-unit"))
2.70 - XSLForms.Utils.add_elements(selectors.get("add-hard-disk"), "hard-disk")
2.71 - XSLForms.Utils.remove_elements(selectors.get("remove-hard-disk"))
2.72 + self.add_elements(selectors.get("add-memory-unit"), "memory-unit")
2.73 + self.remove_elements(selectors.get("remove-memory-unit"))
2.74 + self.add_elements(selectors.get("add-storage-unit"), "storage-unit")
2.75 + self.remove_elements(selectors.get("remove-storage-unit"))
2.76 + self.add_elements(selectors.get("add-hard-disk"), "hard-disk")
2.77 + self.remove_elements(selectors.get("remove-hard-disk"))
2.78
2.79 # Send a response according to certain parameters.
2.80 # When exported, an XML version of the data is returned.
2.81
2.82 + parameters = form.get_parameters()
2.83 +
2.84 if parameters.has_key("export"):
2.85 trans.set_content_type(WebStack.Generic.ContentType("text/xml", encoding))
2.86 - configuration.toStream(trans.get_response_stream(), trans.get_response_stream_encoding())
2.87 + self.get_document(trans).toStream(trans.get_response_stream(), trans.get_response_stream_encoding())
2.88 + raise WebStack.Generic.EndOfResponse
2.89
2.90 - # When not exported, the data is transformed to produce a normal Web
2.91 - # page.
2.92 + def init_document(self, trans, form):
2.93
2.94 - else:
2.95 -
2.96 - # Transform, adding enumerations/ranges.
2.97 + # Transform, adding enumerations/ranges.
2.98
2.99 - init_xsl = self.prepare_initialiser("configuration")
2.100 - configuration = self.get_result([init_xsl], configuration,
2.101 - references={
2.102 - "accessories" : self.prepare_document("accessories"),
2.103 - "base-system" : self.prepare_document("base-system"),
2.104 - "cpu" : self.prepare_document("cpu"),
2.105 - "hard-disk" : self.prepare_document("hard-disk"),
2.106 - "keyboard" : self.prepare_document("keyboard"),
2.107 - "memory-unit" : self.prepare_document("memory-unit"),
2.108 - "mouse" : self.prepare_document("mouse"),
2.109 - "screen" : self.prepare_document("screen"),
2.110 - "storage-unit" : self.prepare_document("storage-unit")
2.111 - })
2.112 + XSLFormsResource.init_document(self, trans, form,
2.113 + references={
2.114 + "accessories" : self.prepare_document("accessories"),
2.115 + "base-system" : self.prepare_document("base-system"),
2.116 + "cpu" : self.prepare_document("cpu"),
2.117 + "hard-disk" : self.prepare_document("hard-disk"),
2.118 + "keyboard" : self.prepare_document("keyboard"),
2.119 + "memory-unit" : self.prepare_document("memory-unit"),
2.120 + "mouse" : self.prepare_document("mouse"),
2.121 + "screen" : self.prepare_document("screen"),
2.122 + "storage-unit" : self.prepare_document("storage-unit")
2.123 + })
2.124
2.125 - # Filter out inappropriate choices.
2.126 -
2.127 - filter_xsl_list = self.prepare_transform("filter")
2.128 - configuration = self.get_result(filter_xsl_list, configuration)
2.129 + def respond_to_document(self, trans, form):
2.130
2.131 - # Start the response.
2.132 + # Filter out inappropriate choices.
2.133
2.134 - trans.set_content_type(WebStack.Generic.ContentType("application/xhtml+xml", encoding))
2.135 -
2.136 - # Ensure that an output stylesheet exists.
2.137 + filter_xsl_list = self.prepare_transform("filter")
2.138 + self.set_document(trans, self.get_result(filter_xsl_list, self.get_document(trans)))
2.139
2.140 - if in_page_resource in self.in_page_resources.keys():
2.141 - trans_xsl = self.prepare_fragment(in_page_resource)
2.142 - stylesheet_parameters = self.prepare_parameters(parameters)
2.143 - else:
2.144 - trans_xsl = self.prepare_output("configuration")
2.145 - stylesheet_parameters = {}
2.146 + def create_output(self, trans, form):
2.147 +
2.148 + # Complete the response.
2.149 +
2.150 + stylesheet_parameters = {}
2.151
2.152 - # Complete the response.
2.153 + try:
2.154 + stylesheet_parameters["locale"] = trans.get_content_languages()[0]
2.155 + except IndexError:
2.156 + pass
2.157
2.158 - try:
2.159 - stylesheet_parameters["locale"] = trans.get_content_languages()[0]
2.160 - except IndexError:
2.161 - pass
2.162 - self.send_output(trans, [trans_xsl], configuration, stylesheet_parameters,
2.163 - references={"translations" : self.prepare_document("translations")})
2.164 -
2.165 - #from XSLTools import XSLOutput
2.166 - #import sys
2.167 - #proc = XSLOutput.Processor([trans_xsl], parameters=stylesheet_parameters,
2.168 - # references={"translations" : self.prepare_document("translations")})
2.169 - #proc.send_output(sys.stderr, "iso-8859-1", configuration)
2.170 + XSLFormsResource.create_output(self, trans, form,
2.171 + stylesheet_parameters=stylesheet_parameters,
2.172 + references={
2.173 + "translations" : self.prepare_document("translations")
2.174 + })
2.175
2.176 # Site map initialisation.
2.177
2.178 @@ -176,6 +153,6 @@
2.179
2.180 def prepare_resources():
2.181 for cls in [ConfiguratorResource]:
2.182 - XSLForms.Resources.WebResources.prepare_resources(cls)
2.183 + xslforms_prepare_resources(cls)
2.184
2.185 # vim: tabstop=4 expandtab shiftwidth=4