1.1 --- a/MoinForms.py Mon Dec 10 00:13:17 2012 +0100
1.2 +++ b/MoinForms.py Wed Dec 12 00:37:44 2012 +0100
1.3 @@ -300,7 +300,7 @@
1.4
1.5 # Common formatting functions.
1.6
1.7 -def getFormOutput(text, fields, path=None):
1.8 +def getFormOutput(text, fields, path=None, repeating=None, index=None):
1.9
1.10 """
1.11 Combine regions found in the given 'text' and then return them as a single
1.12 @@ -311,6 +311,12 @@
1.13
1.14 The given 'fields' are used to populate fields provided in forms and to
1.15 control whether sections are populated or not.
1.16 +
1.17 + The optional 'path' is used to adjust form fields to refer to the correct
1.18 + part of the form hierarchy.
1.19 +
1.20 + The optional 'repeating' and 'index' is used to refer to individual values
1.21 + of a designated field.
1.22 """
1.23
1.24 output = []
1.25 @@ -322,7 +328,8 @@
1.26 # Adjust any FormField macros to use hierarchical names.
1.27
1.28 if format is None:
1.29 - output.append(path and adjustFormFields(body, path) or body)
1.30 + output.append((path or repeating) and
1.31 + adjustFormFields(body, path, repeating, index) or body)
1.32
1.33 # Include form sections only if fields exist for those sections.
1.34
1.35 @@ -349,7 +356,12 @@
1.36 # reference the current namespace.
1.37
1.38 elif message_name and section.has_key(message_name):
1.39 - output.append(getFormOutput(body, section, path))
1.40 +
1.41 + if attributes.get("repeating"):
1.42 + for index in range(0, len(section[message_name])):
1.43 + output.append(getFormOutput(body, section, path, message_name, index))
1.44 + else:
1.45 + output.append(getFormOutput(body, section, path))
1.46
1.47 # Not-message regions are conditional on a particular field being
1.48 # absent. They reference the current namespace.
1.49 @@ -361,7 +373,7 @@
1.50
1.51 else:
1.52 output.append(header)
1.53 - output.append(getFormOutput(body, section, path))
1.54 + output.append(getFormOutput(body, section, path, repeating, index))
1.55 output.append(close)
1.56
1.57 return "".join(output)
1.58 @@ -419,11 +431,12 @@
1.59
1.60 return fields
1.61
1.62 -def adjustFormFields(body, path):
1.63 +def adjustFormFields(body, path, repeating=None, index=None):
1.64
1.65 """
1.66 Return a version of the 'body' with the names in FormField macros updated to
1.67 - incorporate the given 'path'.
1.68 + incorporate the given 'path'. If 'repeating' is specified, any field with
1.69 + such a name will be adjusted to reference the value with the given 'index'.
1.70 """
1.71
1.72 result = []
1.73 @@ -446,33 +459,43 @@
1.74
1.75 else:
1.76 result.append("<<Form%s(%s)>>" % (type, ",".join(
1.77 - adjustMacroArguments(parseMacroArguments(match), path)
1.78 + adjustMacroArguments(parseMacroArguments(match), path, repeating, index)
1.79 )))
1.80
1.81 return "".join(result)
1.82
1.83 -def adjustMacroArguments(args, path):
1.84 +def adjustMacroArguments(args, path, repeating=None, index=None):
1.85
1.86 """
1.87 Adjust the given 'args' so that the path incorporates the given
1.88 'path', returning a new list containing the revised path and remaining
1.89 - arguments.
1.90 + arguments. If 'repeating' is specified, any field with such a name will be
1.91 + adjusted to reference the value with the given 'index'.
1.92 """
1.93
1.94 - if not path:
1.95 + if not path and not repeating:
1.96 return args
1.97
1.98 result = []
1.99 old_path = None
1.100 + found_name = None
1.101
1.102 for arg in args:
1.103 if arg.startswith("path="):
1.104 old_path = arg[5:]
1.105 else:
1.106 result.append(arg)
1.107 + if arg.startswith("name="):
1.108 + found_name = arg[5:]
1.109 + elif found_name is None:
1.110 + found_name = arg
1.111
1.112 - qualified = old_path and ("%s/%s" % (old_path, path)) or path
1.113 - result.append("path=%s" % qualified)
1.114 + if path:
1.115 + qualified = old_path and ("%s/%s" % (old_path, path)) or path
1.116 + result.append("path=%s" % qualified)
1.117 +
1.118 + if repeating and repeating == found_name:
1.119 + result.append("index=%s" % index)
1.120
1.121 return result
1.122
2.1 --- a/macros/FormMessage.py Mon Dec 10 00:13:17 2012 +0100
2.2 +++ b/macros/FormMessage.py Wed Dec 12 00:37:44 2012 +0100
2.3 @@ -23,6 +23,8 @@
2.4 The following optional named arguments are also supported:
2.5
2.6 path=PATH The location of the field in the form section hierarchy
2.7 + index=INDEX The index of the value to be displayed (instead of the first
2.8 + value)
2.9 """
2.10
2.11 request = macro.request
2.12 @@ -38,6 +40,7 @@
2.13
2.14 name = None
2.15 path = None
2.16 + index = None
2.17
2.18 for arg in parsed_args:
2.19 if arg.startswith("name="):
2.20 @@ -46,6 +49,9 @@
2.21 elif arg.startswith("path="):
2.22 path = arg[5:]
2.23
2.24 + elif arg.startswith("index="):
2.25 + index = arg[6:]
2.26 +
2.27 elif name is None:
2.28 name = arg
2.29
2.30 @@ -59,7 +65,7 @@
2.31 # Obtain any request parameters corresponding to the field.
2.32
2.33 form = get_form(request)
2.34 - value = form.get(ref, [""])[0]
2.35 + value = form.get(ref, [""])[index and int(index) or 0]
2.36
2.37 # Render the message.
2.38