1.1 --- a/examples/Common/Candidate/__init__.py Tue Nov 15 00:24:15 2005 +0000
1.2 +++ b/examples/Common/Candidate/__init__.py Tue Nov 15 00:25:51 2005 +0000
1.3 @@ -6,6 +6,7 @@
1.4 import XSLForms.Resources.WebResources
1.5 import XSLForms.Utils
1.6 import os
1.7 +import libxml2dom
1.8
1.9 # Site map imports.
1.10
1.11 @@ -14,6 +15,158 @@
1.12
1.13 # Resource classes.
1.14
1.15 +class AdminResource(XSLForms.Resources.WebResources.XSLFormsResource):
1.16 +
1.17 + "A resource providing administration facilities for job candidate profiles."
1.18 +
1.19 + resource_dir = os.path.join(os.path.split(__file__)[0], "Resources")
1.20 + encoding = "utf-8"
1.21 + template_resources = {
1.22 + "admin" : ("admin_template.xhtml", "admin_output.xsl")
1.23 + }
1.24 + init_resources = {
1.25 + "admin" : ("admin_template.xhtml", "admin_input.xsl")
1.26 + }
1.27 +
1.28 + def respond_to_form(self, trans, form):
1.29 +
1.30 + """
1.31 + Respond to a request having the given transaction 'trans' and the given
1.32 + 'form' information.
1.33 + """
1.34 +
1.35 + parameters = form.get_parameters()
1.36 + documents = form.get_documents()
1.37 +
1.38 + # Get the "show" and "edit" resource paths.
1.39 + # NOTE: These should be obtained from the site map.
1.40 +
1.41 + parts = trans.get_processed_virtual_path_info(self.path_encoding).split("/")
1.42 + show_path = "/".join(parts[:-1] + ["show"])
1.43 + edit_path = "/".join(parts[:-1] + ["edit"])
1.44 +
1.45 + # Ensure the presence of a document.
1.46 +
1.47 + form_is_new = 0
1.48 + if documents.has_key("admin"):
1.49 + admin = documents["admin"]
1.50 + else:
1.51 + admin = form.new_instance("admin")
1.52 + form_is_new = 1
1.53 +
1.54 + # Redirect if one of the CVs is to be shown or edited.
1.55 +
1.56 + selectors = form.get_selectors()
1.57 + if selectors.has_key("show"):
1.58 + name = selectors["show"][0].getAttribute("name")
1.59 + self._redirect(trans, show_path + "?name=%s" % name.encode(self.path_encoding))
1.60 + elif selectors.has_key("edit"):
1.61 + name = selectors["edit"][0].getAttribute("name")
1.62 + self._redirect(trans, edit_path + "?name=%s" % name.encode(self.path_encoding))
1.63 +
1.64 + # Add and remove elements according to the selectors found.
1.65 +
1.66 + XSLForms.Utils.remove_elements(selectors.get("remove"))
1.67 + XSLForms.Utils.add_elements(selectors.get("new"), "cv", "cvs")
1.68 +
1.69 + # Transform, adding enumerations/ranges.
1.70 +
1.71 + init_xsl = self.prepare_initialiser("admin")
1.72 + admin = self.get_result([init_xsl], admin)
1.73 +
1.74 + # Synchronise the session with the CVs found.
1.75 +
1.76 + cvs = admin.xpath("/admin/cvs")[0]
1.77 + session = trans.get_session()
1.78 + for key in session.keys():
1.79 + if key.startswith("candidate-"):
1.80 + name = key[len("candidate-"):]
1.81 + # NOTE: Apostrophes not quoted.
1.82 + if not cvs.xpath("cv[@name = '%s']" % name):
1.83 + if form_is_new:
1.84 + cv = admin.createElement("cv")
1.85 + cv.setAttribute("name", name)
1.86 + cvs.appendChild(cv)
1.87 + else:
1.88 + del session[key]
1.89 +
1.90 + # Start the response.
1.91 +
1.92 + trans.set_content_type(WebStack.Generic.ContentType("application/xhtml+xml", self.encoding))
1.93 +
1.94 + # Ensure that an output stylesheet exists.
1.95 +
1.96 + trans_xsl = self.prepare_output("admin")
1.97 + stylesheet_parameters = {}
1.98 +
1.99 + # Complete the response.
1.100 +
1.101 + self.send_output(trans, [trans_xsl], admin, stylesheet_parameters)
1.102 +
1.103 + def _redirect(self, trans, path):
1.104 + trans.set_response_code(302)
1.105 + trans.set_header_value("Location", path)
1.106 + raise WebStack.Generic.EndOfResponse
1.107 +
1.108 +class DisplayResource(XSLForms.Resources.WebResources.XSLFormsResource):
1.109 +
1.110 + "A resource providing editing facilities for a job candidate profile."
1.111 +
1.112 + resource_dir = os.path.join(os.path.split(__file__)[0], "Resources")
1.113 + encoding = "utf-8"
1.114 + template_resources = {
1.115 + "candidate_display" : ("candidate_display_template.xhtml", "candidate_display_output.xsl")
1.116 + }
1.117 + init_resources = {
1.118 + "candidate" : ("candidate_template.xhtml", "candidate_input.xsl")
1.119 + }
1.120 + document_resources = {
1.121 + "status" : "candidate_status.xml"
1.122 + }
1.123 +
1.124 + def respond_to_form(self, trans, form):
1.125 +
1.126 + """
1.127 + Respond to a request having the given transaction 'trans' and the given
1.128 + 'form' information.
1.129 + """
1.130 +
1.131 + parameters = form.get_parameters()
1.132 + documents = form.get_documents()
1.133 + fields = trans.get_fields_from_path()
1.134 + name = fields.get("name", [u"None"])[0]
1.135 +
1.136 + # Ensure the presence of a document.
1.137 +
1.138 + if documents.has_key("candidate"):
1.139 + candidate = documents["candidate"]
1.140 + else:
1.141 + session = trans.get_session(create=0)
1.142 + if session is None or not session.has_key("candidate-%s" % name.encode("utf-8")):
1.143 + candidate = form.new_instance("candidate")
1.144 + else:
1.145 + candidate = libxml2dom.parseString(session["candidate-%s" % name.encode("utf-8")])
1.146 +
1.147 + # Transform, adding enumerations/ranges.
1.148 +
1.149 + init_xsl = self.prepare_initialiser("candidate")
1.150 + status_xml = self.prepare_document("status")
1.151 + candidate = self.get_result([init_xsl], candidate, references={"status" : status_xml})
1.152 +
1.153 + # Start the response.
1.154 +
1.155 + trans.set_content_type(WebStack.Generic.ContentType("application/xhtml+xml", self.encoding))
1.156 +
1.157 + # Ensure that an output stylesheet exists.
1.158 + # Handle the "show" operation.
1.159 +
1.160 + trans_xsl = self.prepare_output("candidate_display")
1.161 + stylesheet_parameters = {}
1.162 +
1.163 + # Complete the response.
1.164 +
1.165 + self.send_output(trans, [trans_xsl], candidate, stylesheet_parameters)
1.166 +
1.167 class CandidateResource(XSLForms.Resources.WebResources.XSLFormsResource):
1.168
1.169 "A resource providing editing facilities for a job candidate profile."
1.170 @@ -29,8 +182,6 @@
1.171 document_resources = {
1.172 "status" : "candidate_status.xml"
1.173 }
1.174 - in_page_resources = {
1.175 - }
1.176
1.177 def respond_to_form(self, trans, form):
1.178
1.179 @@ -39,16 +190,28 @@
1.180 'form' information.
1.181 """
1.182
1.183 - in_page_resource = self.get_in_page_resource(trans)
1.184 parameters = form.get_parameters()
1.185 documents = form.get_documents()
1.186 + fields = trans.get_fields_from_path()
1.187 + name = fields.get("name", [u"None"])[0]
1.188 +
1.189 + # Get the "show" resource path.
1.190 + # NOTE: This should be obtained from the site map.
1.191 +
1.192 + parts = trans.get_processed_virtual_path_info(self.path_encoding).split("/")
1.193 + show_path = "/".join(parts[:-1] + ["show"])
1.194 + admin_path = "/".join(parts[:-1] + [""])
1.195
1.196 # Ensure the presence of a document.
1.197
1.198 if documents.has_key("candidate"):
1.199 candidate = documents["candidate"]
1.200 else:
1.201 - candidate = form.new_instance("candidate")
1.202 + session = trans.get_session(create=0)
1.203 + if session is None or not session.has_key("candidate-%s" % name.encode("utf-8")):
1.204 + candidate = form.new_instance("candidate")
1.205 + else:
1.206 + candidate = libxml2dom.parseString(session["candidate-%s" % name.encode("utf-8")])
1.207
1.208 # Add and remove elements according to the selectors found.
1.209
1.210 @@ -64,23 +227,44 @@
1.211 status_xml = self.prepare_document("status")
1.212 candidate = self.get_result([init_xsl], candidate, references={"status" : status_xml})
1.213
1.214 + # Redirect if the CV is to be shown.
1.215 +
1.216 + if parameters.has_key("show"):
1.217 +
1.218 + # Save the candidate information.
1.219 +
1.220 + session = trans.get_session()
1.221 + session["candidate-%s" % name.encode("utf-8")] = candidate.toString()
1.222 + self._redirect(trans, show_path + "?name=%s" % name.encode(self.path_encoding))
1.223 +
1.224 + # Redirect if the administration interface is to be used.
1.225 +
1.226 + elif parameters.has_key("admin"):
1.227 +
1.228 + # Save the candidate information.
1.229 +
1.230 + session = trans.get_session()
1.231 + session["candidate-%s" % name.encode("utf-8")] = candidate.toString()
1.232 + self._redirect(trans, admin_path)
1.233 +
1.234 # Start the response.
1.235
1.236 trans.set_content_type(WebStack.Generic.ContentType("application/xhtml+xml", self.encoding))
1.237
1.238 # Ensure that an output stylesheet exists.
1.239
1.240 - if in_page_resource in self.in_page_resources.keys():
1.241 - trans_xsl = self.prepare_fragment("candidate", in_page_resource)
1.242 - stylesheet_parameters = self.prepare_parameters(parameters)
1.243 - else:
1.244 - trans_xsl = self.prepare_output("candidate")
1.245 - stylesheet_parameters = {}
1.246 + trans_xsl = self.prepare_output("candidate")
1.247 + stylesheet_parameters = {}
1.248
1.249 # Complete the response.
1.250
1.251 self.send_output(trans, [trans_xsl], candidate, stylesheet_parameters)
1.252
1.253 + def _redirect(self, trans, path):
1.254 + trans.set_response_code(302)
1.255 + trans.set_header_value("Location", path)
1.256 + raise WebStack.Generic.EndOfResponse
1.257 +
1.258 # Site map initialisation.
1.259
1.260 def get_site():
1.261 @@ -90,15 +274,16 @@
1.262 # Get the main resource and the directory used by the application.
1.263
1.264 candidate_resource = CandidateResource()
1.265 + display_resource = DisplayResource()
1.266 + admin_resource = AdminResource()
1.267 directory = candidate_resource.resource_dir
1.268
1.269 # Make a simple Web site.
1.270
1.271 resource = MapResource({
1.272 - # Static resources:
1.273 - #"scripts" : DirectoryResource(os.path.join(directory, "scripts"), {"js" : "text/javascript"}),
1.274 - # Main page and in-page resources:
1.275 - None : candidate_resource
1.276 + "edit" : candidate_resource,
1.277 + "show" : display_resource,
1.278 + "" : admin_resource
1.279 })
1.280
1.281 return resource