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@0 | 10 | from MoinMoin import wikiutil |
paul@0 | 11 | from MoinSupport import * |
paul@0 | 12 | import re |
paul@0 | 13 | |
paul@0 | 14 | __version__ = "0.1" |
paul@0 | 15 | |
paul@4 | 16 | form_field_regexp_str = r"<<Form(Field|Message)\((.*?)\)>>" |
paul@1 | 17 | form_field_regexp = re.compile(form_field_regexp_str, re.DOTALL) |
paul@1 | 18 | |
paul@3 | 19 | # Common action functionality. |
paul@3 | 20 | |
paul@3 | 21 | class MoinFormHandlerAction: |
paul@3 | 22 | |
paul@3 | 23 | "A handler action that can be specialised for individual forms." |
paul@3 | 24 | |
paul@3 | 25 | def __init__(self, pagename, request): |
paul@3 | 26 | self.pagename = pagename |
paul@3 | 27 | self.request = request |
paul@3 | 28 | |
paul@3 | 29 | def processForm(self): |
paul@3 | 30 | |
paul@3 | 31 | """ |
paul@3 | 32 | Interpret the request details and modify them according to the structure |
paul@3 | 33 | of the interpreted information. |
paul@3 | 34 | """ |
paul@3 | 35 | |
paul@3 | 36 | # Get the form fields and obtain the hierarchical field structure. |
paul@3 | 37 | |
paul@3 | 38 | form = get_form(self.request) |
paul@3 | 39 | fields = getFields(form, remove=True) |
paul@3 | 40 | |
paul@3 | 41 | # Modify, serialise and show the form. |
paul@3 | 42 | |
paul@3 | 43 | self.modifyFields(fields) |
paul@4 | 44 | self.validateFields(fields) |
paul@3 | 45 | self.serialiseFields(fields, form) |
paul@3 | 46 | do_show(self.pagename, self.request) |
paul@3 | 47 | |
paul@4 | 48 | def validateFields(self, fields): |
paul@4 | 49 | pass |
paul@4 | 50 | |
paul@3 | 51 | def serialiseFields(self, fields, form, path=None): |
paul@3 | 52 | |
paul@3 | 53 | """ |
paul@3 | 54 | Serialise the given 'fields' to the given 'form', using the given 'path' |
paul@3 | 55 | to name the entries. |
paul@3 | 56 | """ |
paul@3 | 57 | |
paul@3 | 58 | for key, value in fields.items(): |
paul@3 | 59 | |
paul@3 | 60 | # Serialise sections. |
paul@3 | 61 | |
paul@3 | 62 | if isinstance(value, dict): |
paul@3 | 63 | for index, element in enumerate(getSectionElements(value)): |
paul@3 | 64 | element_ref = "%s$%s" % (key, index) |
paul@3 | 65 | |
paul@3 | 66 | self.serialiseFields(element, form, |
paul@3 | 67 | path and ("%s/%s" % (path, element_ref)) or element_ref |
paul@3 | 68 | ) |
paul@3 | 69 | |
paul@3 | 70 | # Serialise fields. |
paul@3 | 71 | |
paul@3 | 72 | else: |
paul@3 | 73 | form[path and ("%s/%s" % (path, key)) or key] = value |
paul@3 | 74 | |
paul@3 | 75 | def modifyFields(self, fields): |
paul@3 | 76 | |
paul@3 | 77 | "Modify the given 'fields', removing and adding items." |
paul@3 | 78 | |
paul@3 | 79 | # First, remove fields. |
paul@3 | 80 | |
paul@3 | 81 | for key in fields.keys(): |
paul@3 | 82 | if key.startswith("_remove="): |
paul@3 | 83 | self.removeField(key[8:], fields) |
paul@3 | 84 | |
paul@3 | 85 | # Then, add fields. |
paul@3 | 86 | |
paul@3 | 87 | for key in fields.keys(): |
paul@3 | 88 | if key.startswith("_add="): |
paul@3 | 89 | self.addField(key[5:], fields) |
paul@3 | 90 | |
paul@3 | 91 | def removeField(self, path, fields): |
paul@3 | 92 | |
paul@3 | 93 | """ |
paul@3 | 94 | Remove the section element indicated by the given 'path' from the |
paul@3 | 95 | 'fields'. |
paul@3 | 96 | """ |
paul@3 | 97 | |
paul@3 | 98 | section, (name, index) = getSectionForPath(path, fields) |
paul@3 | 99 | del section[name][index] |
paul@3 | 100 | |
paul@3 | 101 | def addField(self, path, fields): |
paul@3 | 102 | |
paul@3 | 103 | """ |
paul@3 | 104 | Add a section element indicated by the given 'path' to the 'fields'. |
paul@3 | 105 | """ |
paul@3 | 106 | |
paul@3 | 107 | section, (name, index) = getSectionForPath(path, fields) |
paul@3 | 108 | placeholder = {"_new" : ""} |
paul@3 | 109 | |
paul@3 | 110 | if section.has_key(name): |
paul@3 | 111 | indexes = section[name].keys() |
paul@3 | 112 | max_index = max(map(int, indexes)) |
paul@3 | 113 | section[name][max_index + 1] = placeholder |
paul@3 | 114 | else: |
paul@3 | 115 | max_index = -1 |
paul@3 | 116 | section[name] = {0 : placeholder} |
paul@3 | 117 | |
paul@0 | 118 | # Common formatting functions. |
paul@0 | 119 | |
paul@0 | 120 | def formatForm(text, request, fmt, attrs=None, write=None): |
paul@0 | 121 | |
paul@0 | 122 | """ |
paul@0 | 123 | Format the given 'text' using the specified 'request' and formatter 'fmt'. |
paul@0 | 124 | The optional 'attrs' can be used to control the presentation of the form. |
paul@0 | 125 | |
paul@0 | 126 | If the 'write' parameter is specified, use it to write output; otherwise, |
paul@0 | 127 | write output using the request. |
paul@0 | 128 | """ |
paul@0 | 129 | |
paul@0 | 130 | write = write or request.write |
paul@0 | 131 | page = request.page |
paul@0 | 132 | |
paul@0 | 133 | fields = getFields(get_form(request)) |
paul@0 | 134 | |
paul@1 | 135 | queryparams = [] |
paul@1 | 136 | |
paul@1 | 137 | for argname in ["fragment", "action"]: |
paul@1 | 138 | if attrs and attrs.has_key(argname): |
paul@1 | 139 | queryparams.append("%s=%s" % (argname, attrs[argname])) |
paul@1 | 140 | |
paul@1 | 141 | querystr = "&".join(queryparams) |
paul@0 | 142 | |
paul@0 | 143 | write(fmt.rawHTML('<form method="post" action="%s">' % |
paul@0 | 144 | escattr(page.url(request, querystr)) |
paul@0 | 145 | )) |
paul@0 | 146 | |
paul@0 | 147 | output = getFormOutput(text, fields) |
paul@2 | 148 | write(formatText(output, request, fmt, inhibit_p=False)) |
paul@0 | 149 | |
paul@0 | 150 | write(fmt.rawHTML('</form>')) |
paul@0 | 151 | |
paul@0 | 152 | def getFormOutput(text, fields): |
paul@0 | 153 | |
paul@0 | 154 | """ |
paul@0 | 155 | Combine regions found in the given 'text' and then return them as a single |
paul@0 | 156 | block. The reason for doing this, as opposed to just passing each region to |
paul@0 | 157 | a suitable parser for formatting, is that form sections may break up |
paul@0 | 158 | regions, and such sections may not define separate subregions but instead |
paul@0 | 159 | act as a means of conditional inclusion of text into an outer region. |
paul@0 | 160 | |
paul@0 | 161 | The given 'fields' are used to populate fields provided in forms and to |
paul@0 | 162 | control whether sections are populated or not. |
paul@0 | 163 | """ |
paul@0 | 164 | |
paul@0 | 165 | output = [] |
paul@0 | 166 | section = fields |
paul@0 | 167 | |
paul@0 | 168 | for region in getRegions(text, True): |
paul@0 | 169 | format, attributes, body, header, close = getFragmentFromRegion(region) |
paul@0 | 170 | |
paul@0 | 171 | # Include bare regions as they are. |
paul@0 | 172 | |
paul@0 | 173 | if format is None: |
paul@0 | 174 | output.append(region) |
paul@0 | 175 | |
paul@0 | 176 | # Include form sections only if fields exist for those sections. |
paul@0 | 177 | |
paul@0 | 178 | elif format == "form": |
paul@0 | 179 | section_name = attributes.get("section") |
paul@0 | 180 | if section_name and section.has_key(section_name): |
paul@1 | 181 | |
paul@1 | 182 | # Iterate over the section contents ignoring the given indexes. |
paul@1 | 183 | |
paul@1 | 184 | for index, element in enumerate(getSectionElements(section[section_name])): |
paul@1 | 185 | |
paul@1 | 186 | # Adjust FormField macros to use hierarchical names. |
paul@1 | 187 | |
paul@1 | 188 | adjusted_body = adjustFormFields(body, section_name, index) |
paul@1 | 189 | output.append(getFormOutput(adjusted_body, element)) |
paul@0 | 190 | |
paul@0 | 191 | # Inspect and include other regions. |
paul@0 | 192 | |
paul@0 | 193 | else: |
paul@0 | 194 | output.append(header) |
paul@0 | 195 | output.append(getFormOutput(body, section)) |
paul@0 | 196 | output.append(close) |
paul@0 | 197 | |
paul@0 | 198 | return "".join(output) |
paul@0 | 199 | |
paul@1 | 200 | def adjustFormFields(body, section_name, index): |
paul@1 | 201 | |
paul@1 | 202 | """ |
paul@1 | 203 | Return a version of the 'body' with the names in FormField macros updated to |
paul@1 | 204 | incorporate the given 'section_name' and 'index'. |
paul@1 | 205 | """ |
paul@1 | 206 | |
paul@1 | 207 | result = [] |
paul@4 | 208 | state = None |
paul@4 | 209 | type = None |
paul@1 | 210 | |
paul@1 | 211 | for match in form_field_regexp.split(body): |
paul@4 | 212 | |
paul@4 | 213 | # Reproduce normal text as is. |
paul@4 | 214 | |
paul@4 | 215 | if not state: |
paul@1 | 216 | result.append(match) |
paul@4 | 217 | state = "TYPE" |
paul@4 | 218 | |
paul@4 | 219 | # Capture the macro type. |
paul@4 | 220 | |
paul@4 | 221 | elif state == "TYPE": |
paul@4 | 222 | type = match |
paul@4 | 223 | state = "ARGS" |
paul@4 | 224 | |
paul@4 | 225 | # Substitute the macro and modified arguments. |
paul@4 | 226 | |
paul@1 | 227 | else: |
paul@4 | 228 | result.append("<<Form%s(%s)>>" % (type, ",".join( |
paul@1 | 229 | adjustMacroArguments(parseMacroArguments(match), section_name, index) |
paul@4 | 230 | ))) |
paul@4 | 231 | state = None |
paul@1 | 232 | |
paul@1 | 233 | return "".join(result) |
paul@1 | 234 | |
paul@1 | 235 | def adjustMacroArguments(args, section_name, index): |
paul@0 | 236 | |
paul@0 | 237 | """ |
paul@1 | 238 | Adjust the given 'args' so that the path incorporates the given |
paul@1 | 239 | 'section_name' and 'index', returning a new list containing the revised |
paul@1 | 240 | path and remaining arguments. |
paul@1 | 241 | """ |
paul@1 | 242 | |
paul@1 | 243 | result = [] |
paul@1 | 244 | path = None |
paul@1 | 245 | |
paul@1 | 246 | for arg in args: |
paul@1 | 247 | if arg.startswith("path="): |
paul@1 | 248 | path = arg[5:] |
paul@1 | 249 | else: |
paul@1 | 250 | result.append(arg) |
paul@1 | 251 | |
paul@1 | 252 | qualified = "%s%s$%s" % (path and ("%s/" % path) or "", section_name, index) |
paul@1 | 253 | result.append("path=%s" % qualified) |
paul@1 | 254 | |
paul@1 | 255 | return result |
paul@1 | 256 | |
paul@1 | 257 | def parseMacroArguments(args): |
paul@1 | 258 | |
paul@1 | 259 | """ |
paul@1 | 260 | Interpret the arguments. |
paul@1 | 261 | NOTE: The argument parsing should really be more powerful in order to |
paul@1 | 262 | NOTE: support labels. |
paul@1 | 263 | """ |
paul@1 | 264 | |
paul@1 | 265 | try: |
paul@1 | 266 | parsed_args = args and wikiutil.parse_quoted_separated(args, name_value=False) or [] |
paul@1 | 267 | except AttributeError: |
paul@1 | 268 | parsed_args = args.split(",") |
paul@1 | 269 | |
paul@1 | 270 | return [arg for arg in parsed_args if arg] |
paul@1 | 271 | |
paul@1 | 272 | def getFields(d, remove=False): |
paul@1 | 273 | |
paul@1 | 274 | """ |
paul@1 | 275 | Return the form fields hierarchy for the given dictionary 'd'. If the |
paul@1 | 276 | optional 'remove' parameter is set to a true value, remove the entries for |
paul@1 | 277 | the fields from 'd'. |
paul@0 | 278 | """ |
paul@0 | 279 | |
paul@0 | 280 | fields = {} |
paul@0 | 281 | |
paul@0 | 282 | for key, value in d.items(): |
paul@0 | 283 | |
paul@1 | 284 | # Detect modifying fields. |
paul@1 | 285 | |
paul@1 | 286 | if key.find("=") != -1: |
paul@1 | 287 | fields[key] = value |
paul@1 | 288 | if remove: |
paul@1 | 289 | del d[key] |
paul@1 | 290 | continue |
paul@1 | 291 | |
paul@0 | 292 | # Reproduce the original hierarchy of the fields. |
paul@0 | 293 | |
paul@0 | 294 | section = fields |
paul@1 | 295 | parts = getPathDetails(key) |
paul@0 | 296 | |
paul@1 | 297 | for name, index in parts[:-1]: |
paul@1 | 298 | |
paul@1 | 299 | # Add an entry for instances of the section. |
paul@0 | 300 | |
paul@0 | 301 | if not section.has_key(name): |
paul@0 | 302 | section[name] = {} |
paul@0 | 303 | |
paul@1 | 304 | # Add an entry for the specific instance of the section. |
paul@1 | 305 | |
paul@0 | 306 | if not section[name].has_key(index): |
paul@0 | 307 | section[name][index] = {} |
paul@0 | 308 | |
paul@0 | 309 | section = section[name][index] |
paul@0 | 310 | |
paul@1 | 311 | section[parts[-1][0]] = value |
paul@1 | 312 | |
paul@1 | 313 | if remove: |
paul@1 | 314 | del d[key] |
paul@0 | 315 | |
paul@0 | 316 | return fields |
paul@0 | 317 | |
paul@1 | 318 | def getPathDetails(path): |
paul@4 | 319 | |
paul@4 | 320 | """ |
paul@4 | 321 | Return the given 'path' as a list of (name, index) tuples providing details |
paul@4 | 322 | of section instances, with any specific field appearing as the last element |
paul@4 | 323 | and having the form (name, None). |
paul@4 | 324 | """ |
paul@4 | 325 | |
paul@1 | 326 | parts = [] |
paul@1 | 327 | |
paul@1 | 328 | for part in path.split("/"): |
paul@1 | 329 | try: |
paul@1 | 330 | name, index = part.split("$", 1) |
paul@1 | 331 | index = int(index) |
paul@1 | 332 | except ValueError: |
paul@1 | 333 | name, index = part, None |
paul@1 | 334 | |
paul@1 | 335 | parts.append((name, index)) |
paul@1 | 336 | |
paul@1 | 337 | return parts |
paul@1 | 338 | |
paul@1 | 339 | def getSectionForPath(path, fields): |
paul@1 | 340 | |
paul@1 | 341 | """ |
paul@1 | 342 | Obtain the section indicated by the given 'path' from the 'fields', |
paul@1 | 343 | returning a tuple of the form (parent section, (name, index)), where the |
paul@1 | 344 | parent section contains the referenced section, where name is the name of |
paul@1 | 345 | the referenced section, and where index, if not None, is the index of a |
paul@1 | 346 | specific section instance within the named section. |
paul@1 | 347 | """ |
paul@1 | 348 | |
paul@1 | 349 | parts = getPathDetails(path) |
paul@1 | 350 | section = fields |
paul@1 | 351 | |
paul@1 | 352 | for name, index in parts[:-1]: |
paul@1 | 353 | section = fields[name][index] |
paul@1 | 354 | |
paul@1 | 355 | return section, parts[-1] |
paul@1 | 356 | |
paul@1 | 357 | def getSectionElements(section_elements): |
paul@1 | 358 | |
paul@1 | 359 | "Return the given 'section_elements' as an ordered collection." |
paul@1 | 360 | |
paul@1 | 361 | keys = map(int, section_elements.keys()) |
paul@1 | 362 | keys.sort() |
paul@1 | 363 | |
paul@1 | 364 | elements = [] |
paul@1 | 365 | |
paul@1 | 366 | for key in keys: |
paul@1 | 367 | elements.append(section_elements[key]) |
paul@1 | 368 | |
paul@1 | 369 | return elements |
paul@1 | 370 | |
paul@0 | 371 | def formatFormForOutputType(text, request, mimetype, attrs=None, write=None): |
paul@0 | 372 | |
paul@0 | 373 | """ |
paul@0 | 374 | Format the given 'text' using the specified 'request' for the given output |
paul@0 | 375 | 'mimetype'. |
paul@0 | 376 | |
paul@0 | 377 | The optional 'attrs' can be used to control the presentation of the form. |
paul@0 | 378 | |
paul@0 | 379 | If the 'write' parameter is specified, use it to write output; otherwise, |
paul@0 | 380 | write output using the request. |
paul@0 | 381 | """ |
paul@0 | 382 | |
paul@0 | 383 | write = write or request.write |
paul@0 | 384 | |
paul@0 | 385 | if mimetype == "text/html": |
paul@0 | 386 | write('<html>') |
paul@0 | 387 | write('<body>') |
paul@0 | 388 | fmt = request.html_formatter |
paul@0 | 389 | fmt.setPage(request.page) |
paul@0 | 390 | formatForm(text, request, fmt, attrs, write) |
paul@0 | 391 | write('</body>') |
paul@0 | 392 | write('</html>') |
paul@0 | 393 | |
paul@0 | 394 | # vim: tabstop=4 expandtab shiftwidth=4 |