# HG changeset patch
# User paulb
# Date 1132014351 0
# Node ID f672b5def234c6be63d48859a356d527876eec3f
# Parent 0ff68f7a95ebf11ed4ea093047e46cb8f61d3e5d
[project @ 2005-11-15 00:25:40 by paulb]
Added administration and display resources.
Introduced the "name" request parameter to choose the active CV.
Introduced session usage to remember which CVs have been created.
diff -r 0ff68f7a95eb -r f672b5def234 examples/Common/Candidate/Resources/admin_template.xhtml
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/Common/Candidate/Resources/admin_template.xhtml Tue Nov 15 00:25:51 2005 +0000
@@ -0,0 +1,40 @@
+
+
+
+
+ Administration
+
+
+
+
+
diff -r 0ff68f7a95eb -r f672b5def234 examples/Common/Candidate/Resources/candidate_display_template.xhtml
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/Common/Candidate/Resources/candidate_display_template.xhtml Tue Nov 15 00:25:51 2005 +0000
@@ -0,0 +1,59 @@
+
+
+
+
+ Candidate
+
+
+
+Candidate Details
+
+
+
+
+ Name |
+
+ ...
+ Candidate name withheld
+ |
+
+
+ Work status |
+
+ ...
+ |
+
+
+ Skills |
+
+
+ ...
+
+ |
+
+
+ Qualifications |
+
+
+ ...
+ ...
+ ...
+
+ |
+
+
+
+ From ...
+ to ...
+ |
+
+ ...
+ |
+
+
+
+
+
+
diff -r 0ff68f7a95eb -r f672b5def234 examples/Common/Candidate/Resources/candidate_template.xhtml
--- a/examples/Common/Candidate/Resources/candidate_template.xhtml Tue Nov 15 00:24:15 2005 +0000
+++ b/examples/Common/Candidate/Resources/candidate_template.xhtml Tue Nov 15 00:25:51 2005 +0000
@@ -11,22 +11,22 @@
Candidate Details
-
+
diff -r 0ff68f7a95eb -r f672b5def234 examples/Common/Candidate/__init__.py
--- a/examples/Common/Candidate/__init__.py Tue Nov 15 00:24:15 2005 +0000
+++ b/examples/Common/Candidate/__init__.py Tue Nov 15 00:25:51 2005 +0000
@@ -6,6 +6,7 @@
import XSLForms.Resources.WebResources
import XSLForms.Utils
import os
+import libxml2dom
# Site map imports.
@@ -14,6 +15,158 @@
# Resource classes.
+class AdminResource(XSLForms.Resources.WebResources.XSLFormsResource):
+
+ "A resource providing administration facilities for job candidate profiles."
+
+ resource_dir = os.path.join(os.path.split(__file__)[0], "Resources")
+ encoding = "utf-8"
+ template_resources = {
+ "admin" : ("admin_template.xhtml", "admin_output.xsl")
+ }
+ init_resources = {
+ "admin" : ("admin_template.xhtml", "admin_input.xsl")
+ }
+
+ def respond_to_form(self, trans, form):
+
+ """
+ Respond to a request having the given transaction 'trans' and the given
+ 'form' information.
+ """
+
+ parameters = form.get_parameters()
+ documents = form.get_documents()
+
+ # Get the "show" and "edit" resource paths.
+ # NOTE: These should be obtained from the site map.
+
+ parts = trans.get_processed_virtual_path_info(self.path_encoding).split("/")
+ show_path = "/".join(parts[:-1] + ["show"])
+ edit_path = "/".join(parts[:-1] + ["edit"])
+
+ # Ensure the presence of a document.
+
+ form_is_new = 0
+ if documents.has_key("admin"):
+ admin = documents["admin"]
+ else:
+ admin = form.new_instance("admin")
+ form_is_new = 1
+
+ # Redirect if one of the CVs is to be shown or edited.
+
+ selectors = form.get_selectors()
+ if selectors.has_key("show"):
+ name = selectors["show"][0].getAttribute("name")
+ self._redirect(trans, show_path + "?name=%s" % name.encode(self.path_encoding))
+ elif selectors.has_key("edit"):
+ name = selectors["edit"][0].getAttribute("name")
+ self._redirect(trans, edit_path + "?name=%s" % name.encode(self.path_encoding))
+
+ # Add and remove elements according to the selectors found.
+
+ XSLForms.Utils.remove_elements(selectors.get("remove"))
+ XSLForms.Utils.add_elements(selectors.get("new"), "cv", "cvs")
+
+ # Transform, adding enumerations/ranges.
+
+ init_xsl = self.prepare_initialiser("admin")
+ admin = self.get_result([init_xsl], admin)
+
+ # Synchronise the session with the CVs found.
+
+ cvs = admin.xpath("/admin/cvs")[0]
+ session = trans.get_session()
+ for key in session.keys():
+ if key.startswith("candidate-"):
+ name = key[len("candidate-"):]
+ # NOTE: Apostrophes not quoted.
+ if not cvs.xpath("cv[@name = '%s']" % name):
+ if form_is_new:
+ cv = admin.createElement("cv")
+ cv.setAttribute("name", name)
+ cvs.appendChild(cv)
+ else:
+ del session[key]
+
+ # Start the response.
+
+ trans.set_content_type(WebStack.Generic.ContentType("application/xhtml+xml", self.encoding))
+
+ # Ensure that an output stylesheet exists.
+
+ trans_xsl = self.prepare_output("admin")
+ stylesheet_parameters = {}
+
+ # Complete the response.
+
+ self.send_output(trans, [trans_xsl], admin, stylesheet_parameters)
+
+ def _redirect(self, trans, path):
+ trans.set_response_code(302)
+ trans.set_header_value("Location", path)
+ raise WebStack.Generic.EndOfResponse
+
+class DisplayResource(XSLForms.Resources.WebResources.XSLFormsResource):
+
+ "A resource providing editing facilities for a job candidate profile."
+
+ resource_dir = os.path.join(os.path.split(__file__)[0], "Resources")
+ encoding = "utf-8"
+ template_resources = {
+ "candidate_display" : ("candidate_display_template.xhtml", "candidate_display_output.xsl")
+ }
+ init_resources = {
+ "candidate" : ("candidate_template.xhtml", "candidate_input.xsl")
+ }
+ document_resources = {
+ "status" : "candidate_status.xml"
+ }
+
+ def respond_to_form(self, trans, form):
+
+ """
+ Respond to a request having the given transaction 'trans' and the given
+ 'form' information.
+ """
+
+ parameters = form.get_parameters()
+ documents = form.get_documents()
+ fields = trans.get_fields_from_path()
+ name = fields.get("name", [u"None"])[0]
+
+ # Ensure the presence of a document.
+
+ if documents.has_key("candidate"):
+ candidate = documents["candidate"]
+ else:
+ session = trans.get_session(create=0)
+ if session is None or not session.has_key("candidate-%s" % name.encode("utf-8")):
+ candidate = form.new_instance("candidate")
+ else:
+ candidate = libxml2dom.parseString(session["candidate-%s" % name.encode("utf-8")])
+
+ # Transform, adding enumerations/ranges.
+
+ init_xsl = self.prepare_initialiser("candidate")
+ status_xml = self.prepare_document("status")
+ candidate = self.get_result([init_xsl], candidate, references={"status" : status_xml})
+
+ # Start the response.
+
+ trans.set_content_type(WebStack.Generic.ContentType("application/xhtml+xml", self.encoding))
+
+ # Ensure that an output stylesheet exists.
+ # Handle the "show" operation.
+
+ trans_xsl = self.prepare_output("candidate_display")
+ stylesheet_parameters = {}
+
+ # Complete the response.
+
+ self.send_output(trans, [trans_xsl], candidate, stylesheet_parameters)
+
class CandidateResource(XSLForms.Resources.WebResources.XSLFormsResource):
"A resource providing editing facilities for a job candidate profile."
@@ -29,8 +182,6 @@
document_resources = {
"status" : "candidate_status.xml"
}
- in_page_resources = {
- }
def respond_to_form(self, trans, form):
@@ -39,16 +190,28 @@
'form' information.
"""
- in_page_resource = self.get_in_page_resource(trans)
parameters = form.get_parameters()
documents = form.get_documents()
+ fields = trans.get_fields_from_path()
+ name = fields.get("name", [u"None"])[0]
+
+ # Get the "show" resource path.
+ # NOTE: This should be obtained from the site map.
+
+ parts = trans.get_processed_virtual_path_info(self.path_encoding).split("/")
+ show_path = "/".join(parts[:-1] + ["show"])
+ admin_path = "/".join(parts[:-1] + [""])
# Ensure the presence of a document.
if documents.has_key("candidate"):
candidate = documents["candidate"]
else:
- candidate = form.new_instance("candidate")
+ session = trans.get_session(create=0)
+ if session is None or not session.has_key("candidate-%s" % name.encode("utf-8")):
+ candidate = form.new_instance("candidate")
+ else:
+ candidate = libxml2dom.parseString(session["candidate-%s" % name.encode("utf-8")])
# Add and remove elements according to the selectors found.
@@ -64,23 +227,44 @@
status_xml = self.prepare_document("status")
candidate = self.get_result([init_xsl], candidate, references={"status" : status_xml})
+ # Redirect if the CV is to be shown.
+
+ if parameters.has_key("show"):
+
+ # Save the candidate information.
+
+ session = trans.get_session()
+ session["candidate-%s" % name.encode("utf-8")] = candidate.toString()
+ self._redirect(trans, show_path + "?name=%s" % name.encode(self.path_encoding))
+
+ # Redirect if the administration interface is to be used.
+
+ elif parameters.has_key("admin"):
+
+ # Save the candidate information.
+
+ session = trans.get_session()
+ session["candidate-%s" % name.encode("utf-8")] = candidate.toString()
+ self._redirect(trans, admin_path)
+
# Start the response.
trans.set_content_type(WebStack.Generic.ContentType("application/xhtml+xml", self.encoding))
# Ensure that an output stylesheet exists.
- if in_page_resource in self.in_page_resources.keys():
- trans_xsl = self.prepare_fragment("candidate", in_page_resource)
- stylesheet_parameters = self.prepare_parameters(parameters)
- else:
- trans_xsl = self.prepare_output("candidate")
- stylesheet_parameters = {}
+ trans_xsl = self.prepare_output("candidate")
+ stylesheet_parameters = {}
# Complete the response.
self.send_output(trans, [trans_xsl], candidate, stylesheet_parameters)
+ def _redirect(self, trans, path):
+ trans.set_response_code(302)
+ trans.set_header_value("Location", path)
+ raise WebStack.Generic.EndOfResponse
+
# Site map initialisation.
def get_site():
@@ -90,15 +274,16 @@
# Get the main resource and the directory used by the application.
candidate_resource = CandidateResource()
+ display_resource = DisplayResource()
+ admin_resource = AdminResource()
directory = candidate_resource.resource_dir
# Make a simple Web site.
resource = MapResource({
- # Static resources:
- #"scripts" : DirectoryResource(os.path.join(directory, "scripts"), {"js" : "text/javascript"}),
- # Main page and in-page resources:
- None : candidate_resource
+ "edit" : candidate_resource,
+ "show" : display_resource,
+ "" : admin_resource
})
return resource