# HG changeset patch # User Paul Boddie # Date 1355269064 -3600 # Node ID bec2e385d5047b095106173e2efd4364cf298bbc # Parent 348ab4b571b1735eeefc0171f19e326632432a45 Added support for repeating message sections and showing all message values. diff -r 348ab4b571b1 -r bec2e385d504 MoinForms.py --- a/MoinForms.py Mon Dec 10 00:13:17 2012 +0100 +++ b/MoinForms.py Wed Dec 12 00:37:44 2012 +0100 @@ -300,7 +300,7 @@ # Common formatting functions. -def getFormOutput(text, fields, path=None): +def getFormOutput(text, fields, path=None, repeating=None, index=None): """ Combine regions found in the given 'text' and then return them as a single @@ -311,6 +311,12 @@ The given 'fields' are used to populate fields provided in forms and to control whether sections are populated or not. + + The optional 'path' is used to adjust form fields to refer to the correct + part of the form hierarchy. + + The optional 'repeating' and 'index' is used to refer to individual values + of a designated field. """ output = [] @@ -322,7 +328,8 @@ # Adjust any FormField macros to use hierarchical names. if format is None: - output.append(path and adjustFormFields(body, path) or body) + output.append((path or repeating) and + adjustFormFields(body, path, repeating, index) or body) # Include form sections only if fields exist for those sections. @@ -349,7 +356,12 @@ # reference the current namespace. elif message_name and section.has_key(message_name): - output.append(getFormOutput(body, section, path)) + + if attributes.get("repeating"): + for index in range(0, len(section[message_name])): + output.append(getFormOutput(body, section, path, message_name, index)) + else: + output.append(getFormOutput(body, section, path)) # Not-message regions are conditional on a particular field being # absent. They reference the current namespace. @@ -361,7 +373,7 @@ else: output.append(header) - output.append(getFormOutput(body, section, path)) + output.append(getFormOutput(body, section, path, repeating, index)) output.append(close) return "".join(output) @@ -419,11 +431,12 @@ return fields -def adjustFormFields(body, path): +def adjustFormFields(body, path, repeating=None, index=None): """ Return a version of the 'body' with the names in FormField macros updated to - incorporate the given 'path'. + incorporate the given 'path'. If 'repeating' is specified, any field with + such a name will be adjusted to reference the value with the given 'index'. """ result = [] @@ -446,33 +459,43 @@ else: result.append("<>" % (type, ",".join( - adjustMacroArguments(parseMacroArguments(match), path) + adjustMacroArguments(parseMacroArguments(match), path, repeating, index) ))) return "".join(result) -def adjustMacroArguments(args, path): +def adjustMacroArguments(args, path, repeating=None, index=None): """ Adjust the given 'args' so that the path incorporates the given 'path', returning a new list containing the revised path and remaining - arguments. + arguments. If 'repeating' is specified, any field with such a name will be + adjusted to reference the value with the given 'index'. """ - if not path: + if not path and not repeating: return args result = [] old_path = None + found_name = None for arg in args: if arg.startswith("path="): old_path = arg[5:] else: result.append(arg) + if arg.startswith("name="): + found_name = arg[5:] + elif found_name is None: + found_name = arg - qualified = old_path and ("%s/%s" % (old_path, path)) or path - result.append("path=%s" % qualified) + if path: + qualified = old_path and ("%s/%s" % (old_path, path)) or path + result.append("path=%s" % qualified) + + if repeating and repeating == found_name: + result.append("index=%s" % index) return result diff -r 348ab4b571b1 -r bec2e385d504 macros/FormMessage.py --- a/macros/FormMessage.py Mon Dec 10 00:13:17 2012 +0100 +++ b/macros/FormMessage.py Wed Dec 12 00:37:44 2012 +0100 @@ -23,6 +23,8 @@ The following optional named arguments are also supported: path=PATH The location of the field in the form section hierarchy + index=INDEX The index of the value to be displayed (instead of the first + value) """ request = macro.request @@ -38,6 +40,7 @@ name = None path = None + index = None for arg in parsed_args: if arg.startswith("name="): @@ -46,6 +49,9 @@ elif arg.startswith("path="): path = arg[5:] + elif arg.startswith("index="): + index = arg[6:] + elif name is None: name = arg @@ -59,7 +65,7 @@ # Obtain any request parameters corresponding to the field. form = get_form(request) - value = form.get(ref, [""])[0] + value = form.get(ref, [""])[index and int(index) or 0] # Render the message.