paul@0 | 1 | # -*- coding: iso-8859-1 -*- |
paul@0 | 2 | """ |
paul@0 | 3 | MoinMoin - MoinForms library |
paul@0 | 4 | |
paul@0 | 5 | @copyright: 2012 by Paul Boddie <paul@boddie.org.uk> |
paul@0 | 6 | @license: GNU GPL (v2 or later), see COPYING.txt for details. |
paul@0 | 7 | """ |
paul@0 | 8 | |
paul@3 | 9 | from MoinMoin.action import do_show |
paul@8 | 10 | from MoinMoin.Page import Page |
paul@0 | 11 | from MoinMoin import wikiutil |
paul@0 | 12 | from MoinSupport import * |
paul@0 | 13 | import re |
paul@0 | 14 | |
paul@0 | 15 | __version__ = "0.1" |
paul@0 | 16 | |
paul@4 | 17 | form_field_regexp_str = r"<<Form(Field|Message)\((.*?)\)>>" |
paul@1 | 18 | form_field_regexp = re.compile(form_field_regexp_str, re.DOTALL) |
paul@1 | 19 | |
paul@3 | 20 | # Common action functionality. |
paul@3 | 21 | |
paul@3 | 22 | class MoinFormHandlerAction: |
paul@3 | 23 | |
paul@3 | 24 | "A handler action that can be specialised for individual forms." |
paul@3 | 25 | |
paul@3 | 26 | def __init__(self, pagename, request): |
paul@3 | 27 | self.pagename = pagename |
paul@3 | 28 | self.request = request |
paul@3 | 29 | |
paul@3 | 30 | def processForm(self): |
paul@3 | 31 | |
paul@3 | 32 | """ |
paul@3 | 33 | Interpret the request details and modify them according to the structure |
paul@3 | 34 | of the interpreted information. |
paul@3 | 35 | """ |
paul@3 | 36 | |
paul@3 | 37 | # Get the form fields and obtain the hierarchical field structure. |
paul@3 | 38 | |
paul@3 | 39 | form = get_form(self.request) |
paul@3 | 40 | fields = getFields(form, remove=True) |
paul@3 | 41 | |
paul@8 | 42 | # Modify and validate the form. |
paul@3 | 43 | |
paul@3 | 44 | self.modifyFields(fields) |
paul@8 | 45 | |
paul@8 | 46 | if self.validateFields(fields): |
paul@8 | 47 | self.finished(fields, form) |
paul@8 | 48 | else: |
paul@8 | 49 | self.unfinished(fields, form) |
paul@8 | 50 | |
paul@8 | 51 | def finished(self, fields, form): |
paul@8 | 52 | |
paul@8 | 53 | "Handle the finished 'fields' and 'form'." |
paul@8 | 54 | |
paul@8 | 55 | self.unfinished(fields, form) |
paul@8 | 56 | |
paul@8 | 57 | def unfinished(self, fields, form): |
paul@8 | 58 | |
paul@8 | 59 | "Handle the unfinished 'fields' and 'form'." |
paul@8 | 60 | |
paul@8 | 61 | # Serialise and show the form. |
paul@8 | 62 | |
paul@3 | 63 | self.serialiseFields(fields, form) |
paul@3 | 64 | do_show(self.pagename, self.request) |
paul@3 | 65 | |
paul@4 | 66 | def validateFields(self, fields): |
paul@8 | 67 | |
paul@8 | 68 | """ |
paul@8 | 69 | Validate the given 'fields', introducing error fields where the |
paul@8 | 70 | individual fields do not conform to their descriptions. |
paul@8 | 71 | """ |
paul@8 | 72 | |
paul@8 | 73 | form = get_form(self.request) |
paul@8 | 74 | text = Page(self.request, self.pagename).get_raw_body() |
paul@8 | 75 | text = getFormForFragment(text, form.get("fragment", [None])[0]) |
paul@8 | 76 | structure = getFormStructure(text, self.request) |
paul@8 | 77 | |
paul@8 | 78 | return self.validateFieldsUsingStructure(fields, structure) |
paul@8 | 79 | |
paul@8 | 80 | def validateFieldsUsingStructure(self, fields, structure): |
paul@8 | 81 | |
paul@8 | 82 | "Validate the given 'fields' using the given 'structure'." |
paul@8 | 83 | |
paul@8 | 84 | _ = self.request.getText |
paul@8 | 85 | valid = True |
paul@8 | 86 | |
paul@8 | 87 | for key, definition in structure.items(): |
paul@8 | 88 | value = fields.get(key) |
paul@8 | 89 | |
paul@8 | 90 | # Enter form sections and validate them. |
paul@8 | 91 | |
paul@8 | 92 | if isinstance(definition, dict): |
paul@8 | 93 | if value: |
paul@8 | 94 | for element in getSectionElements(value): |
paul@11 | 95 | valid = self.validateFieldsUsingStructure(element, structure[key]) and valid |
paul@8 | 96 | |
paul@8 | 97 | # Validate individual fields. |
paul@8 | 98 | |
paul@8 | 99 | elif structure.has_key(key): |
paul@8 | 100 | path, dictpage, label, section, field_args, allowed_values = definition |
paul@8 | 101 | errors = [] |
paul@8 | 102 | |
paul@8 | 103 | # Test for obligatory values. |
paul@8 | 104 | |
paul@10 | 105 | if not value or not value[0]: |
paul@8 | 106 | if field_args.get("required"): |
paul@11 | 107 | |
paul@11 | 108 | # Detect new parts of the structure and avoid producing |
paul@11 | 109 | # premature error messages. |
paul@11 | 110 | |
paul@11 | 111 | if not fields.has_key("_new"): |
paul@11 | 112 | errors.append(_("This field must be filled out.")) |
paul@11 | 113 | else: |
paul@11 | 114 | valid = False |
paul@8 | 115 | else: |
paul@8 | 116 | # Test for unacceptable values. |
paul@8 | 117 | |
paul@8 | 118 | if allowed_values and set(value).difference(allowed_values): |
paul@8 | 119 | errors.append(_("At least one of the choices is not acceptable.")) |
paul@8 | 120 | |
paul@8 | 121 | # Test the number of values. |
paul@8 | 122 | |
paul@8 | 123 | if field_args.get("type") == "select": |
paul@8 | 124 | if field_args.has_key("maxselected"): |
paul@8 | 125 | if len(value) > int(field_args["maxselected"]): |
paul@8 | 126 | errors.append(_("Incorrect number of choices given: need %s.") % field_args["maxselected"]) |
paul@8 | 127 | |
paul@8 | 128 | if errors: |
paul@8 | 129 | fields["%s-error" % key] = errors |
paul@8 | 130 | valid = False |
paul@8 | 131 | |
paul@8 | 132 | return valid |
paul@4 | 133 | |
paul@3 | 134 | def serialiseFields(self, fields, form, path=None): |
paul@3 | 135 | |
paul@3 | 136 | """ |
paul@3 | 137 | Serialise the given 'fields' to the given 'form', using the given 'path' |
paul@3 | 138 | to name the entries. |
paul@3 | 139 | """ |
paul@3 | 140 | |
paul@3 | 141 | for key, value in fields.items(): |
paul@3 | 142 | |
paul@3 | 143 | # Serialise sections. |
paul@3 | 144 | |
paul@3 | 145 | if isinstance(value, dict): |
paul@3 | 146 | for index, element in enumerate(getSectionElements(value)): |
paul@3 | 147 | element_ref = "%s$%s" % (key, index) |
paul@3 | 148 | |
paul@3 | 149 | self.serialiseFields(element, form, |
paul@3 | 150 | path and ("%s/%s" % (path, element_ref)) or element_ref |
paul@3 | 151 | ) |
paul@3 | 152 | |
paul@3 | 153 | # Serialise fields. |
paul@3 | 154 | |
paul@3 | 155 | else: |
paul@3 | 156 | form[path and ("%s/%s" % (path, key)) or key] = value |
paul@3 | 157 | |
paul@3 | 158 | def modifyFields(self, fields): |
paul@3 | 159 | |
paul@3 | 160 | "Modify the given 'fields', removing and adding items." |
paul@3 | 161 | |
paul@3 | 162 | # First, remove fields. |
paul@3 | 163 | |
paul@3 | 164 | for key in fields.keys(): |
paul@3 | 165 | if key.startswith("_remove="): |
paul@3 | 166 | self.removeField(key[8:], fields) |
paul@3 | 167 | |
paul@3 | 168 | # Then, add fields. |
paul@3 | 169 | |
paul@3 | 170 | for key in fields.keys(): |
paul@3 | 171 | if key.startswith("_add="): |
paul@3 | 172 | self.addField(key[5:], fields) |
paul@3 | 173 | |
paul@3 | 174 | def removeField(self, path, fields): |
paul@3 | 175 | |
paul@3 | 176 | """ |
paul@3 | 177 | Remove the section element indicated by the given 'path' from the |
paul@3 | 178 | 'fields'. |
paul@3 | 179 | """ |
paul@3 | 180 | |
paul@3 | 181 | section, (name, index) = getSectionForPath(path, fields) |
paul@3 | 182 | del section[name][index] |
paul@3 | 183 | |
paul@3 | 184 | def addField(self, path, fields): |
paul@3 | 185 | |
paul@3 | 186 | """ |
paul@3 | 187 | Add a section element indicated by the given 'path' to the 'fields'. |
paul@3 | 188 | """ |
paul@3 | 189 | |
paul@3 | 190 | section, (name, index) = getSectionForPath(path, fields) |
paul@3 | 191 | placeholder = {"_new" : ""} |
paul@3 | 192 | |
paul@3 | 193 | if section.has_key(name): |
paul@3 | 194 | indexes = section[name].keys() |
paul@3 | 195 | max_index = max(map(int, indexes)) |
paul@3 | 196 | section[name][max_index + 1] = placeholder |
paul@3 | 197 | else: |
paul@3 | 198 | max_index = -1 |
paul@3 | 199 | section[name] = {0 : placeholder} |
paul@3 | 200 | |
paul@5 | 201 | # Form and field information. |
paul@5 | 202 | |
paul@8 | 203 | def getFormStructure(text, request, path=None, structure=None): |
paul@8 | 204 | |
paul@8 | 205 | """ |
paul@8 | 206 | For the given form 'text' and using the 'request', return details of the |
paul@8 | 207 | form for the section at the given 'path' (or the entire form if 'path' is |
paul@8 | 208 | omitted), populating the given 'structure' (or populating a new structure if |
paul@8 | 209 | 'structure' is omitted). |
paul@8 | 210 | """ |
paul@8 | 211 | |
paul@8 | 212 | if structure is None: |
paul@8 | 213 | structure = {} |
paul@8 | 214 | |
paul@8 | 215 | for format, attributes, body in getFragments(text, True): |
paul@8 | 216 | |
paul@8 | 217 | # Get field details at the current level. |
paul@8 | 218 | |
paul@8 | 219 | if format is None: |
paul@8 | 220 | structure.update(getFormFields(body, path, request)) |
paul@8 | 221 | |
paul@8 | 222 | # Where a section is found, get details from within the section. |
paul@8 | 223 | |
paul@11 | 224 | elif format == "form": |
paul@11 | 225 | if attributes.has_key("section"): |
paul@11 | 226 | section_name = attributes["section"] |
paul@11 | 227 | section = structure[section_name] = {} |
paul@11 | 228 | getFormStructure(body, request, path and ("%s/%s" % (path, section_name)) or section_name, section) |
paul@11 | 229 | elif attributes.has_key("message"): |
paul@11 | 230 | getFormStructure(body, request, path, structure) |
paul@8 | 231 | |
paul@8 | 232 | # Get field details from other kinds of region. |
paul@8 | 233 | |
paul@8 | 234 | elif format != "form": |
paul@8 | 235 | getFormStructure(body, request, path, structure) |
paul@8 | 236 | |
paul@8 | 237 | return structure |
paul@8 | 238 | |
paul@8 | 239 | def getFormForFragment(text, fragment=None): |
paul@8 | 240 | |
paul@8 | 241 | """ |
paul@8 | 242 | Return the form region from the given 'text' for the specified 'fragment'. |
paul@8 | 243 | If no fragment is specified, the first form region is returned. |
paul@8 | 244 | """ |
paul@8 | 245 | |
paul@8 | 246 | for format, attributes, body in getFragments(text): |
paul@8 | 247 | if not fragment or attributes.get("fragment") == fragment: |
paul@8 | 248 | return body |
paul@8 | 249 | |
paul@8 | 250 | return None |
paul@8 | 251 | |
paul@5 | 252 | def getFieldArguments(field_definition): |
paul@5 | 253 | |
paul@5 | 254 | "Return the parsed arguments from the given 'field_definition' string." |
paul@5 | 255 | |
paul@5 | 256 | field_args = {} |
paul@5 | 257 | |
paul@5 | 258 | for field_arg in field_definition.split(): |
paul@8 | 259 | if field_arg == "required": |
paul@8 | 260 | field_args[field_arg] = True |
paul@8 | 261 | continue |
paul@5 | 262 | |
paul@5 | 263 | # Record the key-value details. |
paul@5 | 264 | |
paul@5 | 265 | try: |
paul@5 | 266 | argname, argvalue = field_arg.split("=", 1) |
paul@5 | 267 | field_args[argname] = argvalue |
paul@5 | 268 | |
paul@5 | 269 | # Single keywords are interpreted as type descriptions. |
paul@5 | 270 | |
paul@5 | 271 | except ValueError: |
paul@5 | 272 | if not field_args.has_key("type"): |
paul@5 | 273 | field_args["type"] = field_arg |
paul@5 | 274 | |
paul@5 | 275 | return field_args |
paul@5 | 276 | |
paul@0 | 277 | # Common formatting functions. |
paul@0 | 278 | |
paul@6 | 279 | def getFormOutput(text, fields, path=None): |
paul@0 | 280 | |
paul@0 | 281 | """ |
paul@0 | 282 | Combine regions found in the given 'text' and then return them as a single |
paul@0 | 283 | block. The reason for doing this, as opposed to just passing each region to |
paul@0 | 284 | a suitable parser for formatting, is that form sections may break up |
paul@0 | 285 | regions, and such sections may not define separate subregions but instead |
paul@0 | 286 | act as a means of conditional inclusion of text into an outer region. |
paul@0 | 287 | |
paul@0 | 288 | The given 'fields' are used to populate fields provided in forms and to |
paul@0 | 289 | control whether sections are populated or not. |
paul@0 | 290 | """ |
paul@0 | 291 | |
paul@0 | 292 | output = [] |
paul@0 | 293 | section = fields |
paul@0 | 294 | |
paul@0 | 295 | for region in getRegions(text, True): |
paul@0 | 296 | format, attributes, body, header, close = getFragmentFromRegion(region) |
paul@0 | 297 | |
paul@6 | 298 | # Adjust any FormField macros to use hierarchical names. |
paul@0 | 299 | |
paul@0 | 300 | if format is None: |
paul@8 | 301 | output.append(path and adjustFormFields(body, path) or body) |
paul@0 | 302 | |
paul@0 | 303 | # Include form sections only if fields exist for those sections. |
paul@0 | 304 | |
paul@0 | 305 | elif format == "form": |
paul@0 | 306 | section_name = attributes.get("section") |
paul@10 | 307 | message_name = attributes.get("message") |
paul@10 | 308 | |
paul@10 | 309 | # Sections are groups of fields in their own namespace. |
paul@10 | 310 | |
paul@0 | 311 | if section_name and section.has_key(section_name): |
paul@1 | 312 | |
paul@1 | 313 | # Iterate over the section contents ignoring the given indexes. |
paul@1 | 314 | |
paul@1 | 315 | for index, element in enumerate(getSectionElements(section[section_name])): |
paul@6 | 316 | element_ref = "%s$%s" % (section_name, index) |
paul@1 | 317 | |
paul@6 | 318 | # Get the output for the section. |
paul@6 | 319 | |
paul@6 | 320 | output.append(getFormOutput(body, element, |
paul@6 | 321 | path and ("%s/%s" % (path, element_ref)) or element_ref)) |
paul@0 | 322 | |
paul@10 | 323 | # Message regions are conditional on a particular field and |
paul@10 | 324 | # reference the current namespace. |
paul@10 | 325 | |
paul@10 | 326 | elif message_name and section.has_key(message_name): |
paul@10 | 327 | output.append(getFormOutput(body, section, path)) |
paul@10 | 328 | |
paul@0 | 329 | # Inspect and include other regions. |
paul@0 | 330 | |
paul@0 | 331 | else: |
paul@0 | 332 | output.append(header) |
paul@6 | 333 | output.append(getFormOutput(body, section, path)) |
paul@0 | 334 | output.append(close) |
paul@0 | 335 | |
paul@0 | 336 | return "".join(output) |
paul@0 | 337 | |
paul@8 | 338 | def getFormFields(body, path, request): |
paul@8 | 339 | |
paul@8 | 340 | "Return a dictionary of fields from the given 'body' at the given 'path'." |
paul@8 | 341 | |
paul@8 | 342 | fields = {} |
paul@8 | 343 | cache = {} |
paul@8 | 344 | type = None |
paul@8 | 345 | |
paul@8 | 346 | for i, match in enumerate(form_field_regexp.split(body)): |
paul@8 | 347 | state = i % 3 |
paul@8 | 348 | |
paul@8 | 349 | if state == 1: |
paul@8 | 350 | type = match |
paul@8 | 351 | elif state == 2 and type == "Field": |
paul@8 | 352 | args = {} |
paul@8 | 353 | |
paul@8 | 354 | # Obtain the macro arguments, adjusted to consider the path. |
paul@8 | 355 | |
paul@8 | 356 | name, path, dictpage, label, section = \ |
paul@8 | 357 | getMacroArguments(adjustMacroArguments(parseMacroArguments(match), path)) |
paul@8 | 358 | |
paul@8 | 359 | # Obtain field information from the cache, if possible. |
paul@8 | 360 | |
paul@8 | 361 | cache_key = (name, dictpage) |
paul@8 | 362 | |
paul@8 | 363 | if cache.has_key(cache_key): |
paul@8 | 364 | field_args, allowed_values = cache[cache_key] |
paul@8 | 365 | |
paul@8 | 366 | # Otherwise, obtain field information from any WikiDict. |
paul@8 | 367 | |
paul@8 | 368 | else: |
paul@8 | 369 | field_args = {} |
paul@8 | 370 | allowed_values = None |
paul@8 | 371 | |
paul@8 | 372 | if dictpage: |
paul@8 | 373 | wikidict = getWikiDict(dictpage, request) |
paul@8 | 374 | if wikidict: |
paul@8 | 375 | field_definition = wikidict.get(name) |
paul@8 | 376 | if field_definition: |
paul@8 | 377 | field_args = getFieldArguments(field_definition) |
paul@8 | 378 | if field_args.has_key("source"): |
paul@8 | 379 | sourcedict = getWikiDict(field_args["source"], request) |
paul@8 | 380 | if sourcedict: |
paul@8 | 381 | allowed_values = sourcedict.keys() |
paul@8 | 382 | |
paul@8 | 383 | cache[cache_key] = field_args, allowed_values |
paul@8 | 384 | |
paul@8 | 385 | # Store the field information. |
paul@8 | 386 | |
paul@8 | 387 | fields[name] = path, dictpage, label, section, field_args, allowed_values |
paul@8 | 388 | |
paul@8 | 389 | return fields |
paul@8 | 390 | |
paul@6 | 391 | def adjustFormFields(body, path): |
paul@1 | 392 | |
paul@1 | 393 | """ |
paul@1 | 394 | Return a version of the 'body' with the names in FormField macros updated to |
paul@6 | 395 | incorporate the given 'path'. |
paul@1 | 396 | """ |
paul@1 | 397 | |
paul@1 | 398 | result = [] |
paul@4 | 399 | type = None |
paul@1 | 400 | |
paul@7 | 401 | for i, match in enumerate(form_field_regexp.split(body)): |
paul@7 | 402 | state = i % 3 |
paul@4 | 403 | |
paul@4 | 404 | # Reproduce normal text as is. |
paul@4 | 405 | |
paul@7 | 406 | if state == 0: |
paul@1 | 407 | result.append(match) |
paul@4 | 408 | |
paul@4 | 409 | # Capture the macro type. |
paul@4 | 410 | |
paul@7 | 411 | elif state == 1: |
paul@4 | 412 | type = match |
paul@4 | 413 | |
paul@4 | 414 | # Substitute the macro and modified arguments. |
paul@4 | 415 | |
paul@1 | 416 | else: |
paul@4 | 417 | result.append("<<Form%s(%s)>>" % (type, ",".join( |
paul@6 | 418 | adjustMacroArguments(parseMacroArguments(match), path) |
paul@4 | 419 | ))) |
paul@1 | 420 | |
paul@1 | 421 | return "".join(result) |
paul@1 | 422 | |
paul@6 | 423 | def adjustMacroArguments(args, path): |
paul@0 | 424 | |
paul@0 | 425 | """ |
paul@1 | 426 | Adjust the given 'args' so that the path incorporates the given |
paul@6 | 427 | 'path', returning a new list containing the revised path and remaining |
paul@6 | 428 | arguments. |
paul@1 | 429 | """ |
paul@1 | 430 | |
paul@8 | 431 | if not path: |
paul@8 | 432 | return args |
paul@8 | 433 | |
paul@1 | 434 | result = [] |
paul@6 | 435 | old_path = None |
paul@1 | 436 | |
paul@1 | 437 | for arg in args: |
paul@1 | 438 | if arg.startswith("path="): |
paul@6 | 439 | old_path = arg[5:] |
paul@1 | 440 | else: |
paul@1 | 441 | result.append(arg) |
paul@1 | 442 | |
paul@6 | 443 | qualified = old_path and ("%s/%s" % (old_path, path)) or path |
paul@1 | 444 | result.append("path=%s" % qualified) |
paul@1 | 445 | |
paul@1 | 446 | return result |
paul@1 | 447 | |
paul@1 | 448 | def parseMacroArguments(args): |
paul@1 | 449 | |
paul@1 | 450 | """ |
paul@1 | 451 | Interpret the arguments. |
paul@1 | 452 | NOTE: The argument parsing should really be more powerful in order to |
paul@1 | 453 | NOTE: support labels. |
paul@1 | 454 | """ |
paul@1 | 455 | |
paul@1 | 456 | try: |
paul@1 | 457 | parsed_args = args and wikiutil.parse_quoted_separated(args, name_value=False) or [] |
paul@1 | 458 | except AttributeError: |
paul@1 | 459 | parsed_args = args.split(",") |
paul@1 | 460 | |
paul@1 | 461 | return [arg for arg in parsed_args if arg] |
paul@1 | 462 | |
paul@8 | 463 | def getMacroArguments(parsed_args): |
paul@8 | 464 | |
paul@8 | 465 | "Return the macro arguments decoded from 'parsed_args'." |
paul@8 | 466 | |
paul@8 | 467 | name = None |
paul@8 | 468 | path = None |
paul@8 | 469 | dictpage = None |
paul@8 | 470 | label = None |
paul@8 | 471 | section = None |
paul@8 | 472 | |
paul@8 | 473 | for arg in parsed_args: |
paul@8 | 474 | if arg.startswith("name="): |
paul@8 | 475 | name = arg[5:] |
paul@8 | 476 | |
paul@8 | 477 | elif arg.startswith("path="): |
paul@8 | 478 | path = arg[5:] |
paul@8 | 479 | |
paul@8 | 480 | elif arg.startswith("dict="): |
paul@8 | 481 | dictpage = arg[5:] |
paul@8 | 482 | |
paul@8 | 483 | elif arg.startswith("label="): |
paul@8 | 484 | label = arg[6:] |
paul@8 | 485 | |
paul@8 | 486 | elif arg.startswith("section="): |
paul@8 | 487 | section = arg[8:] |
paul@8 | 488 | |
paul@8 | 489 | elif name is None: |
paul@8 | 490 | name = arg |
paul@8 | 491 | |
paul@8 | 492 | elif dictpage is None: |
paul@8 | 493 | dictpage = arg |
paul@8 | 494 | |
paul@8 | 495 | return name, path, dictpage, label, section |
paul@8 | 496 | |
paul@1 | 497 | def getFields(d, remove=False): |
paul@1 | 498 | |
paul@1 | 499 | """ |
paul@1 | 500 | Return the form fields hierarchy for the given dictionary 'd'. If the |
paul@1 | 501 | optional 'remove' parameter is set to a true value, remove the entries for |
paul@1 | 502 | the fields from 'd'. |
paul@0 | 503 | """ |
paul@0 | 504 | |
paul@0 | 505 | fields = {} |
paul@0 | 506 | |
paul@0 | 507 | for key, value in d.items(): |
paul@0 | 508 | |
paul@1 | 509 | # Detect modifying fields. |
paul@1 | 510 | |
paul@1 | 511 | if key.find("=") != -1: |
paul@1 | 512 | fields[key] = value |
paul@1 | 513 | if remove: |
paul@1 | 514 | del d[key] |
paul@1 | 515 | continue |
paul@1 | 516 | |
paul@0 | 517 | # Reproduce the original hierarchy of the fields. |
paul@0 | 518 | |
paul@0 | 519 | section = fields |
paul@1 | 520 | parts = getPathDetails(key) |
paul@0 | 521 | |
paul@1 | 522 | for name, index in parts[:-1]: |
paul@1 | 523 | |
paul@1 | 524 | # Add an entry for instances of the section. |
paul@0 | 525 | |
paul@0 | 526 | if not section.has_key(name): |
paul@0 | 527 | section[name] = {} |
paul@0 | 528 | |
paul@1 | 529 | # Add an entry for the specific instance of the section. |
paul@1 | 530 | |
paul@0 | 531 | if not section[name].has_key(index): |
paul@0 | 532 | section[name][index] = {} |
paul@0 | 533 | |
paul@0 | 534 | section = section[name][index] |
paul@0 | 535 | |
paul@1 | 536 | section[parts[-1][0]] = value |
paul@1 | 537 | |
paul@1 | 538 | if remove: |
paul@1 | 539 | del d[key] |
paul@0 | 540 | |
paul@0 | 541 | return fields |
paul@0 | 542 | |
paul@1 | 543 | def getPathDetails(path): |
paul@4 | 544 | |
paul@4 | 545 | """ |
paul@4 | 546 | Return the given 'path' as a list of (name, index) tuples providing details |
paul@4 | 547 | of section instances, with any specific field appearing as the last element |
paul@4 | 548 | and having the form (name, None). |
paul@4 | 549 | """ |
paul@4 | 550 | |
paul@1 | 551 | parts = [] |
paul@1 | 552 | |
paul@1 | 553 | for part in path.split("/"): |
paul@1 | 554 | try: |
paul@1 | 555 | name, index = part.split("$", 1) |
paul@1 | 556 | index = int(index) |
paul@1 | 557 | except ValueError: |
paul@1 | 558 | name, index = part, None |
paul@1 | 559 | |
paul@1 | 560 | parts.append((name, index)) |
paul@1 | 561 | |
paul@1 | 562 | return parts |
paul@1 | 563 | |
paul@1 | 564 | def getSectionForPath(path, fields): |
paul@1 | 565 | |
paul@1 | 566 | """ |
paul@1 | 567 | Obtain the section indicated by the given 'path' from the 'fields', |
paul@1 | 568 | returning a tuple of the form (parent section, (name, index)), where the |
paul@1 | 569 | parent section contains the referenced section, where name is the name of |
paul@1 | 570 | the referenced section, and where index, if not None, is the index of a |
paul@1 | 571 | specific section instance within the named section. |
paul@1 | 572 | """ |
paul@1 | 573 | |
paul@1 | 574 | parts = getPathDetails(path) |
paul@1 | 575 | section = fields |
paul@1 | 576 | |
paul@1 | 577 | for name, index in parts[:-1]: |
paul@1 | 578 | section = fields[name][index] |
paul@1 | 579 | |
paul@1 | 580 | return section, parts[-1] |
paul@1 | 581 | |
paul@1 | 582 | def getSectionElements(section_elements): |
paul@1 | 583 | |
paul@1 | 584 | "Return the given 'section_elements' as an ordered collection." |
paul@1 | 585 | |
paul@1 | 586 | keys = map(int, section_elements.keys()) |
paul@1 | 587 | keys.sort() |
paul@1 | 588 | |
paul@1 | 589 | elements = [] |
paul@1 | 590 | |
paul@1 | 591 | for key in keys: |
paul@1 | 592 | elements.append(section_elements[key]) |
paul@1 | 593 | |
paul@1 | 594 | return elements |
paul@1 | 595 | |
paul@9 | 596 | # Parser-related formatting functions. |
paul@9 | 597 | |
paul@9 | 598 | def formatForm(text, request, fmt, attrs=None, write=None): |
paul@9 | 599 | |
paul@9 | 600 | """ |
paul@9 | 601 | Format the given 'text' using the specified 'request' and formatter 'fmt'. |
paul@9 | 602 | The optional 'attrs' can be used to control the presentation of the form. |
paul@9 | 603 | |
paul@9 | 604 | If the 'write' parameter is specified, use it to write output; otherwise, |
paul@9 | 605 | write output using the request. |
paul@9 | 606 | """ |
paul@9 | 607 | |
paul@9 | 608 | write = write or request.write |
paul@9 | 609 | page = request.page |
paul@9 | 610 | |
paul@9 | 611 | fields = getFields(get_form(request)) |
paul@9 | 612 | |
paul@9 | 613 | queryparams = [] |
paul@9 | 614 | |
paul@9 | 615 | for argname in ["fragment", "action"]: |
paul@9 | 616 | if attrs and attrs.has_key(argname): |
paul@9 | 617 | queryparams.append("%s=%s" % (argname, attrs[argname])) |
paul@9 | 618 | |
paul@9 | 619 | querystr = "&".join(queryparams) |
paul@9 | 620 | |
paul@9 | 621 | write(fmt.rawHTML('<form method="post" action="%s">' % |
paul@9 | 622 | escattr(page.url(request, querystr)) |
paul@9 | 623 | )) |
paul@9 | 624 | |
paul@9 | 625 | # Obtain page text for the form, incorporating subregions and applicable |
paul@9 | 626 | # sections. |
paul@9 | 627 | |
paul@9 | 628 | output = getFormOutput(text, fields) |
paul@9 | 629 | write(formatText(output, request, fmt, inhibit_p=False)) |
paul@9 | 630 | |
paul@9 | 631 | write(fmt.rawHTML('</form>')) |
paul@9 | 632 | |
paul@0 | 633 | def formatFormForOutputType(text, request, mimetype, attrs=None, write=None): |
paul@0 | 634 | |
paul@0 | 635 | """ |
paul@0 | 636 | Format the given 'text' using the specified 'request' for the given output |
paul@0 | 637 | 'mimetype'. |
paul@0 | 638 | |
paul@0 | 639 | The optional 'attrs' can be used to control the presentation of the form. |
paul@0 | 640 | |
paul@0 | 641 | If the 'write' parameter is specified, use it to write output; otherwise, |
paul@0 | 642 | write output using the request. |
paul@0 | 643 | """ |
paul@0 | 644 | |
paul@0 | 645 | write = write or request.write |
paul@0 | 646 | |
paul@0 | 647 | if mimetype == "text/html": |
paul@0 | 648 | write('<html>') |
paul@0 | 649 | write('<body>') |
paul@0 | 650 | fmt = request.html_formatter |
paul@0 | 651 | fmt.setPage(request.page) |
paul@0 | 652 | formatForm(text, request, fmt, attrs, write) |
paul@0 | 653 | write('</body>') |
paul@0 | 654 | write('</html>') |
paul@0 | 655 | |
paul@0 | 656 | # vim: tabstop=4 expandtab shiftwidth=4 |