1.1 --- a/MoinForms.py Sat Jan 12 00:51:50 2013 +0100
1.2 +++ b/MoinForms.py Sat Jan 19 22:50:29 2013 +0100
1.3 @@ -82,8 +82,9 @@
1.4
1.5 "Return the structure of the form being handled."
1.6
1.7 + fragment = fields.get("fragment", [None])[0]
1.8 text = Page(self.request, self.pagename).get_raw_body()
1.9 - text = getFormForFragment(text, fields.get("fragment", [None])[0])
1.10 + text = getFormForFragment(text, fragment)
1.11 return getFormStructure(text, self.request)
1.12
1.13 def validateFields(self, fields, structure):
1.14 @@ -300,7 +301,7 @@
1.15
1.16 # Common formatting functions.
1.17
1.18 -def getFormOutput(text, fields, path=None, repeating=None, index=None):
1.19 +def getFormOutput(text, fields, path=None, fragment=None, repeating=None, index=None):
1.20
1.21 """
1.22 Combine regions found in the given 'text' and then return them as a single
1.23 @@ -315,6 +316,9 @@
1.24 The optional 'path' is used to adjust form fields to refer to the correct
1.25 part of the form hierarchy.
1.26
1.27 + The optional 'fragment' is used to indicate the form to which the fields
1.28 + belong.
1.29 +
1.30 The optional 'repeating' and 'index' is used to refer to individual values
1.31 of a designated field.
1.32 """
1.33 @@ -328,8 +332,8 @@
1.34 # Adjust any FormField macros to use hierarchical names.
1.35
1.36 if format is None:
1.37 - output.append((path or repeating) and
1.38 - adjustFormFields(body, path, repeating, index) or body)
1.39 + output.append((path or fragment or repeating) and
1.40 + adjustFormFields(body, path, fragment, repeating, index) or body)
1.41
1.42 # Include form sections only if fields exist for those sections.
1.43
1.44 @@ -350,7 +354,7 @@
1.45 # Get the output for the section.
1.46
1.47 output.append(getFormOutput(body, element,
1.48 - path and ("%s/%s" % (path, element_ref)) or element_ref))
1.49 + path and ("%s/%s" % (path, element_ref)) or element_ref, fragment))
1.50
1.51 # Message regions are conditional on a particular field and
1.52 # reference the current namespace.
1.53 @@ -359,21 +363,21 @@
1.54
1.55 if attributes.get("repeating"):
1.56 for index in range(0, len(section[message_name])):
1.57 - output.append(getFormOutput(body, section, path, message_name, index))
1.58 + output.append(getFormOutput(body, section, path, fragment, message_name, index))
1.59 else:
1.60 - output.append(getFormOutput(body, section, path))
1.61 + output.append(getFormOutput(body, section, path, fragment))
1.62
1.63 # Not-message regions are conditional on a particular field being
1.64 # absent. They reference the current namespace.
1.65
1.66 elif absent_message_name and not section.has_key(absent_message_name):
1.67 - output.append(getFormOutput(body, section, path))
1.68 + output.append(getFormOutput(body, section, path, fragment))
1.69
1.70 # Inspect and include other regions.
1.71
1.72 else:
1.73 output.append(header)
1.74 - output.append(getFormOutput(body, section, path, repeating, index))
1.75 + output.append(getFormOutput(body, section, path, fragment, repeating, index))
1.76 output.append(close)
1.77
1.78 return "".join(output)
1.79 @@ -396,7 +400,7 @@
1.80
1.81 # Obtain the macro arguments, adjusted to consider the path.
1.82
1.83 - name, path, dictpage, label, section = \
1.84 + name, path, dictpage, label, section, fragment = \
1.85 getMacroArguments(adjustMacroArguments(parseMacroArguments(match), path))
1.86
1.87 # Obtain field information from the cache, if possible.
1.88 @@ -431,12 +435,13 @@
1.89
1.90 return fields
1.91
1.92 -def adjustFormFields(body, path, repeating=None, index=None):
1.93 +def adjustFormFields(body, path, fragment, repeating=None, index=None):
1.94
1.95 """
1.96 Return a version of the 'body' with the names in FormField macros updated to
1.97 - incorporate the given 'path'. If 'repeating' is specified, any field with
1.98 - such a name will be adjusted to reference the value with the given 'index'.
1.99 + incorporate the given 'path' and 'fragment'. If 'repeating' is specified,
1.100 + any field with such a name will be adjusted to reference the value with the
1.101 + given 'index'.
1.102 """
1.103
1.104 result = []
1.105 @@ -459,21 +464,22 @@
1.106
1.107 else:
1.108 result.append("<<Form%s(%s)>>" % (type, ",".join(
1.109 - adjustMacroArguments(parseMacroArguments(match), path, repeating, index)
1.110 + adjustMacroArguments(parseMacroArguments(match), path, fragment, repeating, index)
1.111 )))
1.112
1.113 return "".join(result)
1.114
1.115 -def adjustMacroArguments(args, path, repeating=None, index=None):
1.116 +def adjustMacroArguments(args, path, fragment=None, repeating=None, index=None):
1.117
1.118 """
1.119 Adjust the given 'args' so that the path incorporates the given
1.120 - 'path', returning a new list containing the revised path and remaining
1.121 - arguments. If 'repeating' is specified, any field with such a name will be
1.122 - adjusted to reference the value with the given 'index'.
1.123 + 'path' and 'fragment', returning a new list containing the revised path,
1.124 + fragment and remaining arguments. If 'repeating' is specified, any field
1.125 + with such a name will be adjusted to reference the value with the given
1.126 + 'index'.
1.127 """
1.128
1.129 - if not path and not repeating:
1.130 + if not path and not fragment and not repeating:
1.131 return args
1.132
1.133 result = []
1.134 @@ -483,6 +489,8 @@
1.135 for arg in args:
1.136 if arg.startswith("path="):
1.137 old_path = arg[5:]
1.138 + elif arg.startswith("fragment=") and fragment:
1.139 + pass
1.140 else:
1.141 result.append(arg)
1.142 if arg.startswith("name="):
1.143 @@ -494,6 +502,9 @@
1.144 qualified = old_path and ("%s/%s" % (old_path, path)) or path
1.145 result.append("path=%s" % qualified)
1.146
1.147 + if fragment:
1.148 + result.append("fragment=%s" % fragment)
1.149 +
1.150 if repeating and repeating == found_name:
1.151 result.append("index=%s" % index)
1.152
1.153 @@ -524,6 +535,7 @@
1.154 dictpage = None
1.155 label = None
1.156 section = None
1.157 + fragment = None
1.158
1.159 for arg in parsed_args:
1.160 if arg.startswith("name="):
1.161 @@ -541,13 +553,16 @@
1.162 elif arg.startswith("section="):
1.163 section = arg[8:]
1.164
1.165 + elif arg.startswith("fragment="):
1.166 + fragment = arg[9:]
1.167 +
1.168 elif name is None:
1.169 name = arg
1.170
1.171 elif dictpage is None:
1.172 dictpage = arg
1.173
1.174 - return name, path, dictpage, label, section
1.175 + return name, path, dictpage, label, section, fragment
1.176
1.177 def getFields(d, remove=False):
1.178
1.179 @@ -685,7 +700,7 @@
1.180 # Obtain page text for the form, incorporating subregions and applicable
1.181 # sections.
1.182
1.183 - output = getFormOutput(text, fields)
1.184 + output = getFormOutput(text, fields, fragment=fragment)
1.185 write(formatText(output, request, fmt, inhibit_p=False))
1.186
1.187 write(fmt.rawHTML('</form>'))