1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/macros/FormField.py Thu Nov 29 00:53:51 2012 +0100
1.3 @@ -0,0 +1,147 @@
1.4 +# -*- coding: iso-8859-1 -*-
1.5 +"""
1.6 + MoinMoin - FormField 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 MoinMoin import wikiutil
1.13 +from MoinSupport import *
1.14 +
1.15 +Dependencies = ['pages']
1.16 +
1.17 +escape = wikiutil.escape
1.18 +
1.19 +# Macro functions.
1.20 +
1.21 +def execute(macro, args):
1.22 +
1.23 + """
1.24 + Execute the 'macro' with the given 'args' to produce a form field element:
1.25 +
1.26 + * A field name
1.27 + * The WikiDict describing form fields
1.28 +
1.29 + The following optional named arguments are also supported:
1.30 +
1.31 + label=TEXT The label employed by button-like fields
1.32 +
1.33 + The nature of each field is described by a WikiDict entry for the given
1.34 + field name.
1.35 + """
1.36 +
1.37 + request = macro.request
1.38 + fmt = macro.formatter
1.39 + page = fmt.page
1.40 + _ = request.getText
1.41 +
1.42 + # Interpret the arguments.
1.43 + # NOTE: The argument parsing should really be more powerful in order to
1.44 + # NOTE: support labels.
1.45 +
1.46 + try:
1.47 + parsed_args = args and wikiutil.parse_quoted_separated(args, name_value=False) or []
1.48 + except AttributeError:
1.49 + parsed_args = args.split(",")
1.50 +
1.51 + parsed_args = [arg for arg in parsed_args if arg]
1.52 +
1.53 + # Get special arguments.
1.54 +
1.55 + name = None
1.56 + dictpage = None
1.57 + label = None
1.58 +
1.59 + for arg in parsed_args:
1.60 + if arg.startswith("name="):
1.61 + name = arg[5:]
1.62 +
1.63 + elif arg.startswith("dict="):
1.64 + dictpage = arg[5:]
1.65 +
1.66 + elif arg.startswith("label="):
1.67 + label = arg[6:]
1.68 +
1.69 + elif name is None:
1.70 + name = arg
1.71 +
1.72 + elif dictpage is None:
1.73 + dictpage = arg
1.74 +
1.75 + if not name:
1.76 + return showError(_("No field name specified."), request)
1.77 +
1.78 + if not dictpage:
1.79 + return showError(_("No WikiDict specified."), request)
1.80 +
1.81 + # Get the WikiDict and the field's definition.
1.82 +
1.83 + wikidict = getWikiDict(dictpage, request)
1.84 +
1.85 + if not wikidict:
1.86 + return showError(_("WikiDict %s cannot be loaded.") % dictpage, request)
1.87 +
1.88 + try:
1.89 + field_definition = wikidict[name]
1.90 + except KeyError:
1.91 + return showError(_("No entry for %s in %s.") % (name, dictpage), request)
1.92 +
1.93 + field_args = {}
1.94 +
1.95 + for field_arg in field_definition.split():
1.96 +
1.97 + # Record the key-value details.
1.98 +
1.99 + try:
1.100 + argname, argvalue = field_arg.split("=", 1)
1.101 + field_args[argname] = argvalue
1.102 +
1.103 + # Single keywords are interpreted as type descriptions.
1.104 +
1.105 + except ValueError:
1.106 + if not field_args.has_key("type"):
1.107 + field_args["type"] = field_arg
1.108 +
1.109 + # Obtain any request parameters corresponding to the field.
1.110 +
1.111 + form = get_form(request)
1.112 + value = form.get(name, [""])[0]
1.113 +
1.114 + # Render the field.
1.115 +
1.116 + type = field_args.get("type", "text")
1.117 +
1.118 + if type == "text":
1.119 + return fmt.rawHTML('<input name="%s" type="%s" size="%s" value="%s" />' % (
1.120 + escattr(name), escattr(type), escattr(field_args.get("size", "10")), escattr(value)
1.121 + ))
1.122 +
1.123 + elif type == "textarea":
1.124 + return fmt.rawHTML('<textarea name="%s" cols="%s" rows="%s">%s</textarea>' % (
1.125 + escattr(name), escattr(field_args.get("cols", "60")), escattr(field_args.get("rows", "5")), escape(value)
1.126 + ))
1.127 +
1.128 + elif type == "submit":
1.129 + return fmt.rawHTML('<input name="%s" type="submit" value="%s" />' % (
1.130 + escattr(name), escattr(_(label or name))
1.131 + ))
1.132 +
1.133 + # NOTE: Support select fields at the very least.
1.134 +
1.135 + else:
1.136 + return ''
1.137 +
1.138 +def showError(text, request):
1.139 + fmt = request.formatter
1.140 +
1.141 + output = []
1.142 + append = output.append
1.143 +
1.144 + append(fmt.span(on=1, attrs={"class" : "form-field-error"}))
1.145 + append(fmt.text(text))
1.146 + append(fmt.span(on=0))
1.147 +
1.148 + return "".join(output)
1.149 +
1.150 +# vim: tabstop=4 expandtab shiftwidth=4