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