paulb@235 | 1 | #!/usr/bin/env python |
paulb@235 | 2 | |
paulb@235 | 3 | "A job candidate editing application." |
paulb@235 | 4 | |
paulb@235 | 5 | import WebStack.Generic |
paulb@353 | 6 | import XSLForms.Resources.WebResources |
paulb@235 | 7 | import XSLForms.Utils |
paulb@235 | 8 | import os |
paulb@402 | 9 | import libxml2dom |
paulb@235 | 10 | |
paulb@235 | 11 | # Site map imports. |
paulb@235 | 12 | |
paulb@235 | 13 | from WebStack.Resources.ResourceMap import MapResource |
paulb@235 | 14 | from WebStack.Resources.Static import DirectoryResource |
paulb@235 | 15 | |
paulb@235 | 16 | # Resource classes. |
paulb@235 | 17 | |
paulb@402 | 18 | class AdminResource(XSLForms.Resources.WebResources.XSLFormsResource): |
paulb@402 | 19 | |
paulb@402 | 20 | "A resource providing administration facilities for job candidate profiles." |
paulb@402 | 21 | |
paulb@402 | 22 | resource_dir = os.path.join(os.path.split(__file__)[0], "Resources") |
paulb@402 | 23 | encoding = "utf-8" |
paulb@402 | 24 | template_resources = { |
paulb@402 | 25 | "admin" : ("admin_template.xhtml", "admin_output.xsl") |
paulb@402 | 26 | } |
paulb@402 | 27 | init_resources = { |
paulb@402 | 28 | "admin" : ("admin_template.xhtml", "admin_input.xsl") |
paulb@402 | 29 | } |
paulb@402 | 30 | |
paulb@402 | 31 | def respond_to_form(self, trans, form): |
paulb@402 | 32 | |
paulb@402 | 33 | """ |
paulb@402 | 34 | Respond to a request having the given transaction 'trans' and the given |
paulb@402 | 35 | 'form' information. |
paulb@402 | 36 | """ |
paulb@402 | 37 | |
paulb@402 | 38 | parameters = form.get_parameters() |
paulb@402 | 39 | documents = form.get_documents() |
paulb@402 | 40 | |
paulb@402 | 41 | # Get the "show" and "edit" resource paths. |
paulb@402 | 42 | # NOTE: These should be obtained from the site map. |
paulb@402 | 43 | |
paulb@402 | 44 | parts = trans.get_processed_virtual_path_info(self.path_encoding).split("/") |
paulb@402 | 45 | show_path = "/".join(parts[:-1] + ["show"]) |
paulb@402 | 46 | edit_path = "/".join(parts[:-1] + ["edit"]) |
paulb@402 | 47 | |
paulb@402 | 48 | # Ensure the presence of a document. |
paulb@402 | 49 | |
paulb@402 | 50 | form_is_new = 0 |
paulb@402 | 51 | if documents.has_key("admin"): |
paulb@402 | 52 | admin = documents["admin"] |
paulb@402 | 53 | else: |
paulb@402 | 54 | admin = form.new_instance("admin") |
paulb@402 | 55 | form_is_new = 1 |
paulb@402 | 56 | |
paulb@402 | 57 | # Redirect if one of the CVs is to be shown or edited. |
paulb@402 | 58 | |
paulb@402 | 59 | selectors = form.get_selectors() |
paulb@402 | 60 | if selectors.has_key("show"): |
paulb@402 | 61 | name = selectors["show"][0].getAttribute("name") |
paulb@402 | 62 | self._redirect(trans, show_path + "?name=%s" % name.encode(self.path_encoding)) |
paulb@402 | 63 | elif selectors.has_key("edit"): |
paulb@402 | 64 | name = selectors["edit"][0].getAttribute("name") |
paulb@402 | 65 | self._redirect(trans, edit_path + "?name=%s" % name.encode(self.path_encoding)) |
paulb@402 | 66 | |
paulb@402 | 67 | # Add and remove elements according to the selectors found. |
paulb@402 | 68 | |
paulb@402 | 69 | XSLForms.Utils.remove_elements(selectors.get("remove")) |
paulb@402 | 70 | XSLForms.Utils.add_elements(selectors.get("new"), "cv", "cvs") |
paulb@402 | 71 | |
paulb@402 | 72 | # Transform, adding enumerations/ranges. |
paulb@402 | 73 | |
paulb@402 | 74 | init_xsl = self.prepare_initialiser("admin") |
paulb@402 | 75 | admin = self.get_result([init_xsl], admin) |
paulb@402 | 76 | |
paulb@402 | 77 | # Synchronise the session with the CVs found. |
paulb@402 | 78 | |
paulb@402 | 79 | cvs = admin.xpath("/admin/cvs")[0] |
paulb@402 | 80 | session = trans.get_session() |
paulb@402 | 81 | for key in session.keys(): |
paulb@402 | 82 | if key.startswith("candidate-"): |
paulb@402 | 83 | name = key[len("candidate-"):] |
paulb@402 | 84 | # NOTE: Apostrophes not quoted. |
paulb@402 | 85 | if not cvs.xpath("cv[@name = '%s']" % name): |
paulb@402 | 86 | if form_is_new: |
paulb@402 | 87 | cv = admin.createElement("cv") |
paulb@402 | 88 | cv.setAttribute("name", name) |
paulb@402 | 89 | cvs.appendChild(cv) |
paulb@402 | 90 | else: |
paulb@402 | 91 | del session[key] |
paulb@402 | 92 | |
paulb@402 | 93 | # Start the response. |
paulb@402 | 94 | |
paulb@402 | 95 | trans.set_content_type(WebStack.Generic.ContentType("application/xhtml+xml", self.encoding)) |
paulb@402 | 96 | |
paulb@402 | 97 | # Ensure that an output stylesheet exists. |
paulb@402 | 98 | |
paulb@402 | 99 | trans_xsl = self.prepare_output("admin") |
paulb@402 | 100 | stylesheet_parameters = {} |
paulb@402 | 101 | |
paulb@402 | 102 | # Complete the response. |
paulb@402 | 103 | |
paulb@402 | 104 | self.send_output(trans, [trans_xsl], admin, stylesheet_parameters) |
paulb@402 | 105 | |
paulb@402 | 106 | def _redirect(self, trans, path): |
paulb@402 | 107 | trans.set_response_code(302) |
paulb@402 | 108 | trans.set_header_value("Location", path) |
paulb@402 | 109 | raise WebStack.Generic.EndOfResponse |
paulb@402 | 110 | |
paulb@402 | 111 | class DisplayResource(XSLForms.Resources.WebResources.XSLFormsResource): |
paulb@402 | 112 | |
paulb@402 | 113 | "A resource providing editing facilities for a job candidate profile." |
paulb@402 | 114 | |
paulb@402 | 115 | resource_dir = os.path.join(os.path.split(__file__)[0], "Resources") |
paulb@402 | 116 | encoding = "utf-8" |
paulb@402 | 117 | template_resources = { |
paulb@402 | 118 | "candidate_display" : ("candidate_display_template.xhtml", "candidate_display_output.xsl") |
paulb@402 | 119 | } |
paulb@402 | 120 | init_resources = { |
paulb@402 | 121 | "candidate" : ("candidate_template.xhtml", "candidate_input.xsl") |
paulb@402 | 122 | } |
paulb@402 | 123 | document_resources = { |
paulb@402 | 124 | "status" : "candidate_status.xml" |
paulb@402 | 125 | } |
paulb@402 | 126 | |
paulb@402 | 127 | def respond_to_form(self, trans, form): |
paulb@402 | 128 | |
paulb@402 | 129 | """ |
paulb@402 | 130 | Respond to a request having the given transaction 'trans' and the given |
paulb@402 | 131 | 'form' information. |
paulb@402 | 132 | """ |
paulb@402 | 133 | |
paulb@402 | 134 | parameters = form.get_parameters() |
paulb@402 | 135 | documents = form.get_documents() |
paulb@402 | 136 | fields = trans.get_fields_from_path() |
paulb@402 | 137 | name = fields.get("name", [u"None"])[0] |
paulb@402 | 138 | |
paulb@402 | 139 | # Ensure the presence of a document. |
paulb@402 | 140 | |
paulb@402 | 141 | if documents.has_key("candidate"): |
paulb@402 | 142 | candidate = documents["candidate"] |
paulb@402 | 143 | else: |
paulb@402 | 144 | session = trans.get_session(create=0) |
paulb@402 | 145 | if session is None or not session.has_key("candidate-%s" % name.encode("utf-8")): |
paulb@402 | 146 | candidate = form.new_instance("candidate") |
paulb@402 | 147 | else: |
paulb@402 | 148 | candidate = libxml2dom.parseString(session["candidate-%s" % name.encode("utf-8")]) |
paulb@402 | 149 | |
paulb@402 | 150 | # Transform, adding enumerations/ranges. |
paulb@402 | 151 | |
paulb@402 | 152 | init_xsl = self.prepare_initialiser("candidate") |
paulb@402 | 153 | status_xml = self.prepare_document("status") |
paulb@402 | 154 | candidate = self.get_result([init_xsl], candidate, references={"status" : status_xml}) |
paulb@402 | 155 | |
paulb@402 | 156 | # Start the response. |
paulb@402 | 157 | |
paulb@402 | 158 | trans.set_content_type(WebStack.Generic.ContentType("application/xhtml+xml", self.encoding)) |
paulb@402 | 159 | |
paulb@402 | 160 | # Ensure that an output stylesheet exists. |
paulb@402 | 161 | # Handle the "show" operation. |
paulb@402 | 162 | |
paulb@402 | 163 | trans_xsl = self.prepare_output("candidate_display") |
paulb@402 | 164 | stylesheet_parameters = {} |
paulb@402 | 165 | |
paulb@402 | 166 | # Complete the response. |
paulb@402 | 167 | |
paulb@402 | 168 | self.send_output(trans, [trans_xsl], candidate, stylesheet_parameters) |
paulb@402 | 169 | |
paulb@353 | 170 | class CandidateResource(XSLForms.Resources.WebResources.XSLFormsResource): |
paulb@235 | 171 | |
paulb@235 | 172 | "A resource providing editing facilities for a job candidate profile." |
paulb@235 | 173 | |
paulb@235 | 174 | resource_dir = os.path.join(os.path.split(__file__)[0], "Resources") |
paulb@235 | 175 | encoding = "utf-8" |
paulb@235 | 176 | template_resources = { |
paulb@249 | 177 | "candidate" : ("candidate_template.xhtml", "candidate_output.xsl") |
paulb@235 | 178 | } |
paulb@249 | 179 | init_resources = { |
paulb@249 | 180 | "candidate" : ("candidate_template.xhtml", "candidate_input.xsl") |
paulb@235 | 181 | } |
paulb@235 | 182 | document_resources = { |
paulb@235 | 183 | "status" : "candidate_status.xml" |
paulb@235 | 184 | } |
paulb@235 | 185 | |
paulb@235 | 186 | def respond_to_form(self, trans, form): |
paulb@235 | 187 | |
paulb@235 | 188 | """ |
paulb@235 | 189 | Respond to a request having the given transaction 'trans' and the given |
paulb@235 | 190 | 'form' information. |
paulb@235 | 191 | """ |
paulb@235 | 192 | |
paulb@235 | 193 | parameters = form.get_parameters() |
paulb@235 | 194 | documents = form.get_documents() |
paulb@402 | 195 | fields = trans.get_fields_from_path() |
paulb@402 | 196 | name = fields.get("name", [u"None"])[0] |
paulb@402 | 197 | |
paulb@402 | 198 | # Get the "show" resource path. |
paulb@402 | 199 | # NOTE: This should be obtained from the site map. |
paulb@402 | 200 | |
paulb@402 | 201 | parts = trans.get_processed_virtual_path_info(self.path_encoding).split("/") |
paulb@402 | 202 | show_path = "/".join(parts[:-1] + ["show"]) |
paulb@402 | 203 | admin_path = "/".join(parts[:-1] + [""]) |
paulb@235 | 204 | |
paulb@235 | 205 | # Ensure the presence of a document. |
paulb@235 | 206 | |
paulb@235 | 207 | if documents.has_key("candidate"): |
paulb@235 | 208 | candidate = documents["candidate"] |
paulb@235 | 209 | else: |
paulb@402 | 210 | session = trans.get_session(create=0) |
paulb@402 | 211 | if session is None or not session.has_key("candidate-%s" % name.encode("utf-8")): |
paulb@402 | 212 | candidate = form.new_instance("candidate") |
paulb@402 | 213 | else: |
paulb@402 | 214 | candidate = libxml2dom.parseString(session["candidate-%s" % name.encode("utf-8")]) |
paulb@235 | 215 | |
paulb@235 | 216 | # Add and remove elements according to the selectors found. |
paulb@235 | 217 | |
paulb@235 | 218 | selectors = form.get_selectors() |
paulb@235 | 219 | XSLForms.Utils.remove_elements(selectors.get("remove")) |
paulb@235 | 220 | XSLForms.Utils.add_elements(selectors.get("add-skill"), "skill", "skills") |
paulb@235 | 221 | XSLForms.Utils.add_elements(selectors.get("add-qualification"), "qualification", "qualifications") |
paulb@235 | 222 | XSLForms.Utils.add_elements(selectors.get("add-employment"), "employment", "experience") |
paulb@235 | 223 | |
paulb@235 | 224 | # Transform, adding enumerations/ranges. |
paulb@235 | 225 | |
paulb@249 | 226 | init_xsl = self.prepare_initialiser("candidate") |
paulb@235 | 227 | status_xml = self.prepare_document("status") |
paulb@249 | 228 | candidate = self.get_result([init_xsl], candidate, references={"status" : status_xml}) |
paulb@235 | 229 | |
paulb@402 | 230 | # Redirect if the CV is to be shown. |
paulb@402 | 231 | |
paulb@402 | 232 | if parameters.has_key("show"): |
paulb@402 | 233 | |
paulb@402 | 234 | # Save the candidate information. |
paulb@402 | 235 | |
paulb@402 | 236 | session = trans.get_session() |
paulb@402 | 237 | session["candidate-%s" % name.encode("utf-8")] = candidate.toString() |
paulb@402 | 238 | self._redirect(trans, show_path + "?name=%s" % name.encode(self.path_encoding)) |
paulb@402 | 239 | |
paulb@402 | 240 | # Redirect if the administration interface is to be used. |
paulb@402 | 241 | |
paulb@402 | 242 | elif parameters.has_key("admin"): |
paulb@402 | 243 | |
paulb@402 | 244 | # Save the candidate information. |
paulb@402 | 245 | |
paulb@402 | 246 | session = trans.get_session() |
paulb@402 | 247 | session["candidate-%s" % name.encode("utf-8")] = candidate.toString() |
paulb@402 | 248 | self._redirect(trans, admin_path) |
paulb@402 | 249 | |
paulb@235 | 250 | # Start the response. |
paulb@235 | 251 | |
paulb@235 | 252 | trans.set_content_type(WebStack.Generic.ContentType("application/xhtml+xml", self.encoding)) |
paulb@235 | 253 | |
paulb@235 | 254 | # Ensure that an output stylesheet exists. |
paulb@235 | 255 | |
paulb@402 | 256 | trans_xsl = self.prepare_output("candidate") |
paulb@402 | 257 | stylesheet_parameters = {} |
paulb@235 | 258 | |
paulb@235 | 259 | # Complete the response. |
paulb@235 | 260 | |
paulb@235 | 261 | self.send_output(trans, [trans_xsl], candidate, stylesheet_parameters) |
paulb@235 | 262 | |
paulb@402 | 263 | def _redirect(self, trans, path): |
paulb@402 | 264 | trans.set_response_code(302) |
paulb@402 | 265 | trans.set_header_value("Location", path) |
paulb@402 | 266 | raise WebStack.Generic.EndOfResponse |
paulb@402 | 267 | |
paulb@235 | 268 | # Site map initialisation. |
paulb@235 | 269 | |
paulb@235 | 270 | def get_site(): |
paulb@235 | 271 | |
paulb@235 | 272 | "Return a simple Web site resource." |
paulb@235 | 273 | |
paulb@235 | 274 | # Get the main resource and the directory used by the application. |
paulb@235 | 275 | |
paulb@235 | 276 | candidate_resource = CandidateResource() |
paulb@402 | 277 | display_resource = DisplayResource() |
paulb@402 | 278 | admin_resource = AdminResource() |
paulb@235 | 279 | directory = candidate_resource.resource_dir |
paulb@235 | 280 | |
paulb@235 | 281 | # Make a simple Web site. |
paulb@235 | 282 | |
paulb@235 | 283 | resource = MapResource({ |
paulb@402 | 284 | "edit" : candidate_resource, |
paulb@402 | 285 | "show" : display_resource, |
paulb@402 | 286 | "" : admin_resource |
paulb@235 | 287 | }) |
paulb@235 | 288 | |
paulb@235 | 289 | return resource |
paulb@235 | 290 | |
paulb@235 | 291 | # vim: tabstop=4 expandtab shiftwidth=4 |