# HG changeset patch # User Paul Boddie # Date 1354405222 -3600 # Node ID 115c012b287ee046953671ff65b14fd508647b05 # Parent 3f974f3a51e717c2f3b26d8e9c2c697f79b1b58c Added a FormMessage macro for displaying field-related messages. Added initial support for validation methods. diff -r 3f974f3a51e7 -r 115c012b287e MoinForms.py --- a/MoinForms.py Sat Dec 01 23:54:16 2012 +0100 +++ b/MoinForms.py Sun Dec 02 00:40:22 2012 +0100 @@ -13,7 +13,7 @@ __version__ = "0.1" -form_field_regexp_str = r"<>" +form_field_regexp_str = r"<>" form_field_regexp = re.compile(form_field_regexp_str, re.DOTALL) # Common action functionality. @@ -41,9 +41,13 @@ # Modify, serialise and show the form. self.modifyFields(fields) + self.validateFields(fields) self.serialiseFields(fields, form) do_show(self.pagename, self.request) + def validateFields(self, fields): + pass + def serialiseFields(self, fields, form, path=None): """ @@ -201,17 +205,30 @@ """ result = [] - in_macro = False + state = None + type = None for match in form_field_regexp.split(body): - if not in_macro: + + # Reproduce normal text as is. + + if not state: result.append(match) + state = "TYPE" + + # Capture the macro type. + + elif state == "TYPE": + type = match + state = "ARGS" + + # Substitute the macro and modified arguments. + else: - result.append("<>" % ",".join( + result.append("<>" % (type, ",".join( adjustMacroArguments(parseMacroArguments(match), section_name, index) - )) - - in_macro = not in_macro + ))) + state = None return "".join(result) @@ -299,6 +316,13 @@ return fields def getPathDetails(path): + + """ + Return the given 'path' as a list of (name, index) tuples providing details + of section instances, with any specific field appearing as the last element + and having the form (name, None). + """ + parts = [] for part in path.split("/"): diff -r 3f974f3a51e7 -r 115c012b287e macros/FormMessage.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/macros/FormMessage.py Sun Dec 02 00:40:22 2012 +0100 @@ -0,0 +1,80 @@ +# -*- coding: iso-8859-1 -*- +""" + MoinMoin - FormMessage Macro + + @copyright: 2012 by Paul Boddie + @license: GNU GPL (v2 or later), see COPYING.txt for details. +""" + +from MoinSupport import * +from MoinForms import parseMacroArguments + +Dependencies = ['pages'] + +# Macro functions. + +def execute(macro, args): + + """ + Execute the 'macro' with the given 'args' to produce a form field element: + + * A field name + + The following optional named arguments are also supported: + + path=PATH The location of the field in the form section hierarchy + """ + + request = macro.request + fmt = macro.formatter + page = fmt.page + _ = request.getText + + # Interpret the arguments. + + parsed_args = parseMacroArguments(args) + + # Get special arguments. + + name = None + path = None + + for arg in parsed_args: + if arg.startswith("name="): + name = arg[5:] + + elif arg.startswith("path="): + path = arg[5:] + + elif name is None: + name = arg + + if not name: + return showError(_("No field name specified."), request) + + # The field name is a combination of the path and the name. + + ref = "%s%s" % (path and ("%s/" % path) or "", name) + + # Obtain any request parameters corresponding to the field. + + form = get_form(request) + value = form.get(ref, [""])[0] + + # Render the message. + + return fmt.text(value) + +def showError(text, request): + fmt = request.formatter + + output = [] + append = output.append + + append(fmt.span(on=1, attrs={"class" : "form-field-error"})) + append(fmt.text(text)) + append(fmt.span(on=0)) + + return "".join(output) + +# vim: tabstop=4 expandtab shiftwidth=4