MoinForms

Change of macros/FormMessage.py

4:115c012b287e
macros/FormMessage.py
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/macros/FormMessage.py	Sun Dec 02 00:40:22 2012 +0100
     1.3 @@ -0,0 +1,80 @@
     1.4 +# -*- coding: iso-8859-1 -*-
     1.5 +"""
     1.6 +    MoinMoin - FormMessage Macro
     1.7 +
     1.8 +    @copyright: 2012 by Paul Boddie <paul@boddie.org.uk>
     1.9 +    @license: GNU GPL (v2 or later), see COPYING.txt for details.
    1.10 +"""
    1.11 +
    1.12 +from MoinSupport import *
    1.13 +from MoinForms import parseMacroArguments
    1.14 +
    1.15 +Dependencies = ['pages']
    1.16 +
    1.17 +# Macro functions.
    1.18 +
    1.19 +def execute(macro, args):
    1.20 +
    1.21 +    """
    1.22 +    Execute the 'macro' with the given 'args' to produce a form field element:
    1.23 +
    1.24 +      * A field name
    1.25 +
    1.26 +    The following optional named arguments are also supported:
    1.27 +
    1.28 +      path=PATH     The location of the field in the form section hierarchy
    1.29 +    """
    1.30 +
    1.31 +    request = macro.request
    1.32 +    fmt = macro.formatter
    1.33 +    page = fmt.page
    1.34 +    _ = request.getText
    1.35 +
    1.36 +    # Interpret the arguments.
    1.37 +
    1.38 +    parsed_args = parseMacroArguments(args)
    1.39 +
    1.40 +    # Get special arguments.
    1.41 +
    1.42 +    name = None
    1.43 +    path = None
    1.44 +
    1.45 +    for arg in parsed_args:
    1.46 +        if arg.startswith("name="):
    1.47 +            name = arg[5:]
    1.48 +
    1.49 +        elif arg.startswith("path="):
    1.50 +            path = arg[5:]
    1.51 +
    1.52 +        elif name is None:
    1.53 +            name = arg
    1.54 +
    1.55 +    if not name:
    1.56 +        return showError(_("No field name specified."), request)
    1.57 +
    1.58 +    # The field name is a combination of the path and the name.
    1.59 +
    1.60 +    ref = "%s%s" % (path and ("%s/" % path) or "", name)
    1.61 +
    1.62 +    # Obtain any request parameters corresponding to the field.
    1.63 +
    1.64 +    form = get_form(request)
    1.65 +    value = form.get(ref, [""])[0]
    1.66 +
    1.67 +    # Render the message.
    1.68 +
    1.69 +    return fmt.text(value)
    1.70 +
    1.71 +def showError(text, request):
    1.72 +    fmt = request.formatter
    1.73 +
    1.74 +    output = []
    1.75 +    append = output.append
    1.76 +
    1.77 +    append(fmt.span(on=1, attrs={"class" : "form-field-error"}))
    1.78 +    append(fmt.text(text))
    1.79 +    append(fmt.span(on=0))
    1.80 +
    1.81 +    return "".join(output)
    1.82 +
    1.83 +# vim: tabstop=4 expandtab shiftwidth=4