# HG changeset patch # User Paul Boddie # Date 1358632229 -3600 # Node ID 08235af0d835e58d8759816178b0274016cae1c4 # Parent f83b49f1603e243f5971f2dddf0467ce2d851483 Added a fragment parameter to the macros so that form fields only display submitted data in the appropriate form. diff -r f83b49f1603e -r 08235af0d835 MoinForms.py --- a/MoinForms.py Sat Jan 12 00:51:50 2013 +0100 +++ b/MoinForms.py Sat Jan 19 22:50:29 2013 +0100 @@ -82,8 +82,9 @@ "Return the structure of the form being handled." + fragment = fields.get("fragment", [None])[0] text = Page(self.request, self.pagename).get_raw_body() - text = getFormForFragment(text, fields.get("fragment", [None])[0]) + text = getFormForFragment(text, fragment) return getFormStructure(text, self.request) def validateFields(self, fields, structure): @@ -300,7 +301,7 @@ # Common formatting functions. -def getFormOutput(text, fields, path=None, repeating=None, index=None): +def getFormOutput(text, fields, path=None, fragment=None, repeating=None, index=None): """ Combine regions found in the given 'text' and then return them as a single @@ -315,6 +316,9 @@ The optional 'path' is used to adjust form fields to refer to the correct part of the form hierarchy. + The optional 'fragment' is used to indicate the form to which the fields + belong. + The optional 'repeating' and 'index' is used to refer to individual values of a designated field. """ @@ -328,8 +332,8 @@ # Adjust any FormField macros to use hierarchical names. if format is None: - output.append((path or repeating) and - adjustFormFields(body, path, repeating, index) or body) + output.append((path or fragment or repeating) and + adjustFormFields(body, path, fragment, repeating, index) or body) # Include form sections only if fields exist for those sections. @@ -350,7 +354,7 @@ # Get the output for the section. output.append(getFormOutput(body, element, - path and ("%s/%s" % (path, element_ref)) or element_ref)) + path and ("%s/%s" % (path, element_ref)) or element_ref, fragment)) # Message regions are conditional on a particular field and # reference the current namespace. @@ -359,21 +363,21 @@ if attributes.get("repeating"): for index in range(0, len(section[message_name])): - output.append(getFormOutput(body, section, path, message_name, index)) + output.append(getFormOutput(body, section, path, fragment, message_name, index)) else: - output.append(getFormOutput(body, section, path)) + output.append(getFormOutput(body, section, path, fragment)) # Not-message regions are conditional on a particular field being # absent. They reference the current namespace. elif absent_message_name and not section.has_key(absent_message_name): - output.append(getFormOutput(body, section, path)) + output.append(getFormOutput(body, section, path, fragment)) # Inspect and include other regions. else: output.append(header) - output.append(getFormOutput(body, section, path, repeating, index)) + output.append(getFormOutput(body, section, path, fragment, repeating, index)) output.append(close) return "".join(output) @@ -396,7 +400,7 @@ # Obtain the macro arguments, adjusted to consider the path. - name, path, dictpage, label, section = \ + name, path, dictpage, label, section, fragment = \ getMacroArguments(adjustMacroArguments(parseMacroArguments(match), path)) # Obtain field information from the cache, if possible. @@ -431,12 +435,13 @@ return fields -def adjustFormFields(body, path, repeating=None, index=None): +def adjustFormFields(body, path, fragment, repeating=None, index=None): """ Return a version of the 'body' with the names in FormField macros updated to - 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'. + incorporate the given 'path' and 'fragment'. If 'repeating' is specified, + any field with such a name will be adjusted to reference the value with the + given 'index'. """ result = [] @@ -459,21 +464,22 @@ else: result.append("<>" % (type, ",".join( - adjustMacroArguments(parseMacroArguments(match), path, repeating, index) + adjustMacroArguments(parseMacroArguments(match), path, fragment, repeating, index) ))) return "".join(result) -def adjustMacroArguments(args, path, repeating=None, index=None): +def adjustMacroArguments(args, path, fragment=None, 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. If 'repeating' is specified, any field with such a name will be - adjusted to reference the value with the given 'index'. + 'path' and 'fragment', returning a new list containing the revised path, + fragment and remaining 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 and not repeating: + if not path and not fragment and not repeating: return args result = [] @@ -483,6 +489,8 @@ for arg in args: if arg.startswith("path="): old_path = arg[5:] + elif arg.startswith("fragment=") and fragment: + pass else: result.append(arg) if arg.startswith("name="): @@ -494,6 +502,9 @@ qualified = old_path and ("%s/%s" % (old_path, path)) or path result.append("path=%s" % qualified) + if fragment: + result.append("fragment=%s" % fragment) + if repeating and repeating == found_name: result.append("index=%s" % index) @@ -524,6 +535,7 @@ dictpage = None label = None section = None + fragment = None for arg in parsed_args: if arg.startswith("name="): @@ -541,13 +553,16 @@ elif arg.startswith("section="): section = arg[8:] + elif arg.startswith("fragment="): + fragment = arg[9:] + elif name is None: name = arg elif dictpage is None: dictpage = arg - return name, path, dictpage, label, section + return name, path, dictpage, label, section, fragment def getFields(d, remove=False): @@ -685,7 +700,7 @@ # Obtain page text for the form, incorporating subregions and applicable # sections. - output = getFormOutput(text, fields) + output = getFormOutput(text, fields, fragment=fragment) write(formatText(output, request, fmt, inhibit_p=False)) write(fmt.rawHTML('')) diff -r f83b49f1603e -r 08235af0d835 macros/FormField.py --- a/macros/FormField.py Sat Jan 12 00:51:50 2013 +0100 +++ b/macros/FormField.py Sat Jan 19 22:50:29 2013 +0100 @@ -2,7 +2,7 @@ """ MoinMoin - FormField Macro - @copyright: 2012 by Paul Boddie + @copyright: 2012, 2013 by Paul Boddie @license: GNU GPL (v2 or later), see COPYING.txt for details. """ @@ -28,6 +28,7 @@ label=TEXT The label employed by button-like fields path=PATH The location of the field in the form section hierarchy + fragment=NAME The name of the form region or fragment in the page The nature of each field is described by a WikiDict entry for the given field name. @@ -44,7 +45,7 @@ # Get special arguments. - name, path, dictpage, label, section = getMacroArguments(parsed_args) + name, path, dictpage, label, section, fragment = getMacroArguments(parsed_args) if not name: return showError(_("No field name specified."), request) @@ -83,8 +84,16 @@ # Obtain any request parameters corresponding to the field. form = get_form(request) - values = form.get(ref, []) - value = form.get(ref, [""])[0] + form_fragment = form.get("fragment", [None])[0] + + # Exclude values intended for other forms. + + if fragment and form_fragment != fragment or not fragment and form_fragment: + values = [] + value = "" + else: + values = form.get(ref, []) + value = form.get(ref, [""])[0] # Render the field. diff -r f83b49f1603e -r 08235af0d835 macros/FormMessage.py --- a/macros/FormMessage.py Sat Jan 12 00:51:50 2013 +0100 +++ b/macros/FormMessage.py Sat Jan 19 22:50:29 2013 +0100 @@ -2,7 +2,7 @@ """ MoinMoin - FormMessage Macro - @copyright: 2012 by Paul Boddie + @copyright: 2012, 2013 by Paul Boddie @license: GNU GPL (v2 or later), see COPYING.txt for details. """ @@ -23,6 +23,7 @@ The following optional named arguments are also supported: path=PATH The location of the field in the form section hierarchy + fragment=NAME The name of the form region or fragment in the page index=INDEX The index of the value to be displayed (instead of the first value) """ @@ -65,7 +66,14 @@ # Obtain any request parameters corresponding to the field. form = get_form(request) - value = form.get(ref, [""])[index and int(index) or 0] + form_fragment = form.get("fragment", [None])[0] + + # Exclude values intended for other forms. + + if fragment and form_fragment != fragment or not fragment and form_fragment: + value = "" + else: + value = form.get(ref, [""])[index and int(index) or 0] # Render the message.