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