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