1.1 --- a/MoinForms.py Sat Dec 01 23:54:16 2012 +0100
1.2 +++ b/MoinForms.py Sun Dec 02 00:40:22 2012 +0100
1.3 @@ -13,7 +13,7 @@
1.4
1.5 __version__ = "0.1"
1.6
1.7 -form_field_regexp_str = r"<<FormField\((.*?)\)>>"
1.8 +form_field_regexp_str = r"<<Form(Field|Message)\((.*?)\)>>"
1.9 form_field_regexp = re.compile(form_field_regexp_str, re.DOTALL)
1.10
1.11 # Common action functionality.
1.12 @@ -41,9 +41,13 @@
1.13 # Modify, serialise and show the form.
1.14
1.15 self.modifyFields(fields)
1.16 + self.validateFields(fields)
1.17 self.serialiseFields(fields, form)
1.18 do_show(self.pagename, self.request)
1.19
1.20 + def validateFields(self, fields):
1.21 + pass
1.22 +
1.23 def serialiseFields(self, fields, form, path=None):
1.24
1.25 """
1.26 @@ -201,17 +205,30 @@
1.27 """
1.28
1.29 result = []
1.30 - in_macro = False
1.31 + state = None
1.32 + type = None
1.33
1.34 for match in form_field_regexp.split(body):
1.35 - if not in_macro:
1.36 +
1.37 + # Reproduce normal text as is.
1.38 +
1.39 + if not state:
1.40 result.append(match)
1.41 + state = "TYPE"
1.42 +
1.43 + # Capture the macro type.
1.44 +
1.45 + elif state == "TYPE":
1.46 + type = match
1.47 + state = "ARGS"
1.48 +
1.49 + # Substitute the macro and modified arguments.
1.50 +
1.51 else:
1.52 - result.append("<<FormField(%s)>>" % ",".join(
1.53 + result.append("<<Form%s(%s)>>" % (type, ",".join(
1.54 adjustMacroArguments(parseMacroArguments(match), section_name, index)
1.55 - ))
1.56 -
1.57 - in_macro = not in_macro
1.58 + )))
1.59 + state = None
1.60
1.61 return "".join(result)
1.62
1.63 @@ -299,6 +316,13 @@
1.64 return fields
1.65
1.66 def getPathDetails(path):
1.67 +
1.68 + """
1.69 + Return the given 'path' as a list of (name, index) tuples providing details
1.70 + of section instances, with any specific field appearing as the last element
1.71 + and having the form (name, None).
1.72 + """
1.73 +
1.74 parts = []
1.75
1.76 for part in path.split("/"):
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/macros/FormMessage.py Sun Dec 02 00:40:22 2012 +0100
2.3 @@ -0,0 +1,80 @@
2.4 +# -*- coding: iso-8859-1 -*-
2.5 +"""
2.6 + MoinMoin - FormMessage Macro
2.7 +
2.8 + @copyright: 2012 by Paul Boddie <paul@boddie.org.uk>
2.9 + @license: GNU GPL (v2 or later), see COPYING.txt for details.
2.10 +"""
2.11 +
2.12 +from MoinSupport import *
2.13 +from MoinForms import parseMacroArguments
2.14 +
2.15 +Dependencies = ['pages']
2.16 +
2.17 +# Macro functions.
2.18 +
2.19 +def execute(macro, args):
2.20 +
2.21 + """
2.22 + Execute the 'macro' with the given 'args' to produce a form field element:
2.23 +
2.24 + * A field name
2.25 +
2.26 + The following optional named arguments are also supported:
2.27 +
2.28 + path=PATH The location of the field in the form section hierarchy
2.29 + """
2.30 +
2.31 + request = macro.request
2.32 + fmt = macro.formatter
2.33 + page = fmt.page
2.34 + _ = request.getText
2.35 +
2.36 + # Interpret the arguments.
2.37 +
2.38 + parsed_args = parseMacroArguments(args)
2.39 +
2.40 + # Get special arguments.
2.41 +
2.42 + name = None
2.43 + path = None
2.44 +
2.45 + for arg in parsed_args:
2.46 + if arg.startswith("name="):
2.47 + name = arg[5:]
2.48 +
2.49 + elif arg.startswith("path="):
2.50 + path = arg[5:]
2.51 +
2.52 + elif name is None:
2.53 + name = arg
2.54 +
2.55 + if not name:
2.56 + return showError(_("No field name specified."), request)
2.57 +
2.58 + # The field name is a combination of the path and the name.
2.59 +
2.60 + ref = "%s%s" % (path and ("%s/" % path) or "", name)
2.61 +
2.62 + # Obtain any request parameters corresponding to the field.
2.63 +
2.64 + form = get_form(request)
2.65 + value = form.get(ref, [""])[0]
2.66 +
2.67 + # Render the message.
2.68 +
2.69 + return fmt.text(value)
2.70 +
2.71 +def showError(text, request):
2.72 + fmt = request.formatter
2.73 +
2.74 + output = []
2.75 + append = output.append
2.76 +
2.77 + append(fmt.span(on=1, attrs={"class" : "form-field-error"}))
2.78 + append(fmt.text(text))
2.79 + append(fmt.span(on=0))
2.80 +
2.81 + return "".join(output)
2.82 +
2.83 +# vim: tabstop=4 expandtab shiftwidth=4