1.1 --- a/macros/FormField.py Thu Nov 29 00:53:51 2012 +0100
1.2 +++ b/macros/FormField.py Sat Dec 01 20:38:35 2012 +0100
1.3 @@ -8,6 +8,7 @@
1.4
1.5 from MoinMoin import wikiutil
1.6 from MoinSupport import *
1.7 +from MoinForms import parseMacroArguments
1.8
1.9 Dependencies = ['pages']
1.10
1.11 @@ -26,6 +27,7 @@
1.12 The following optional named arguments are also supported:
1.13
1.14 label=TEXT The label employed by button-like fields
1.15 + path=PATH The location of the field in the form section hierarchy
1.16
1.17 The nature of each field is described by a WikiDict entry for the given
1.18 field name.
1.19 @@ -37,32 +39,33 @@
1.20 _ = request.getText
1.21
1.22 # Interpret the arguments.
1.23 - # NOTE: The argument parsing should really be more powerful in order to
1.24 - # NOTE: support labels.
1.25
1.26 - try:
1.27 - parsed_args = args and wikiutil.parse_quoted_separated(args, name_value=False) or []
1.28 - except AttributeError:
1.29 - parsed_args = args.split(",")
1.30 -
1.31 - parsed_args = [arg for arg in parsed_args if arg]
1.32 + parsed_args = parseMacroArguments(args)
1.33
1.34 # Get special arguments.
1.35
1.36 name = None
1.37 + path = None
1.38 dictpage = None
1.39 label = None
1.40 + section = None
1.41
1.42 for arg in parsed_args:
1.43 if arg.startswith("name="):
1.44 name = arg[5:]
1.45
1.46 + elif arg.startswith("path="):
1.47 + path = arg[5:]
1.48 +
1.49 elif arg.startswith("dict="):
1.50 dictpage = arg[5:]
1.51
1.52 elif arg.startswith("label="):
1.53 label = arg[6:]
1.54
1.55 + elif arg.startswith("section="):
1.56 + section = arg[8:]
1.57 +
1.58 elif name is None:
1.59 name = arg
1.60
1.61 @@ -72,41 +75,55 @@
1.62 if not name:
1.63 return showError(_("No field name specified."), request)
1.64
1.65 - if not dictpage:
1.66 - return showError(_("No WikiDict specified."), request)
1.67 + # Detect special modification fields.
1.68 +
1.69 + if name in ("_add", "_remove"):
1.70 + field_args = {"type" : "submit"}
1.71 +
1.72 + # The field name is a combination of the name, path and section.
1.73 +
1.74 + ref = "%s=%s%s%s" % (name, path or "", path and section and "/" or "", section or "")
1.75
1.76 # Get the WikiDict and the field's definition.
1.77
1.78 - wikidict = getWikiDict(dictpage, request)
1.79 -
1.80 - if not wikidict:
1.81 - return showError(_("WikiDict %s cannot be loaded.") % dictpage, request)
1.82 + elif dictpage:
1.83 + wikidict = getWikiDict(dictpage, request)
1.84
1.85 - try:
1.86 - field_definition = wikidict[name]
1.87 - except KeyError:
1.88 - return showError(_("No entry for %s in %s.") % (name, dictpage), request)
1.89 -
1.90 - field_args = {}
1.91 -
1.92 - for field_arg in field_definition.split():
1.93 -
1.94 - # Record the key-value details.
1.95 + if not wikidict:
1.96 + return showError(_("WikiDict %s cannot be loaded for %s.") % (dictpage, name), request)
1.97
1.98 try:
1.99 - argname, argvalue = field_arg.split("=", 1)
1.100 - field_args[argname] = argvalue
1.101 + field_definition = wikidict[name]
1.102 + except KeyError:
1.103 + return showError(_("No entry for %s in %s.") % (name, dictpage), request)
1.104 +
1.105 + field_args = {}
1.106 +
1.107 + for field_arg in field_definition.split():
1.108 +
1.109 + # Record the key-value details.
1.110
1.111 - # Single keywords are interpreted as type descriptions.
1.112 + try:
1.113 + argname, argvalue = field_arg.split("=", 1)
1.114 + field_args[argname] = argvalue
1.115 +
1.116 + # Single keywords are interpreted as type descriptions.
1.117
1.118 - except ValueError:
1.119 - if not field_args.has_key("type"):
1.120 - field_args["type"] = field_arg
1.121 + except ValueError:
1.122 + if not field_args.has_key("type"):
1.123 + field_args["type"] = field_arg
1.124 +
1.125 + # The field name is a combination of the path and the name.
1.126 +
1.127 + ref = "%s%s" % (path and ("%s/" % path) or "", name)
1.128 +
1.129 + else:
1.130 + return showError(_("No WikiDict specified for %s.") % name, request)
1.131
1.132 # Obtain any request parameters corresponding to the field.
1.133
1.134 form = get_form(request)
1.135 - value = form.get(name, [""])[0]
1.136 + value = form.get(ref, [""])[0]
1.137
1.138 # Render the field.
1.139
1.140 @@ -114,23 +131,42 @@
1.141
1.142 if type == "text":
1.143 return fmt.rawHTML('<input name="%s" type="%s" size="%s" value="%s" />' % (
1.144 - escattr(name), escattr(type), escattr(field_args.get("size", "10")), escattr(value)
1.145 + escattr(ref), escattr(type), escattr(field_args.get("size", "10")), escattr(value)
1.146 ))
1.147
1.148 elif type == "textarea":
1.149 return fmt.rawHTML('<textarea name="%s" cols="%s" rows="%s">%s</textarea>' % (
1.150 - escattr(name), escattr(field_args.get("cols", "60")), escattr(field_args.get("rows", "5")), escape(value)
1.151 + escattr(ref), escattr(field_args.get("cols", "60")), escattr(field_args.get("rows", "5")), escape(value)
1.152 ))
1.153
1.154 elif type == "submit":
1.155 return fmt.rawHTML('<input name="%s" type="submit" value="%s" />' % (
1.156 - escattr(name), escattr(_(label or name))
1.157 + escattr(ref), escattr(_(label or name))
1.158 ))
1.159
1.160 - # NOTE: Support select fields at the very least.
1.161 + elif type == "select":
1.162 +
1.163 + if not field_args.has_key("source"):
1.164 + return showError(_("No source dictionary given for %s.") % name, request)
1.165 +
1.166 + sourcedict = getWikiDict(field_args["source"], request)
1.167 +
1.168 + if not sourcedict:
1.169 + return showError(_("WikiDict %s cannot be loaded for %s.") % (sourcedict, name), request)
1.170 +
1.171 + output = []
1.172 + output.append(fmt.rawHTML('<select name="%s">' % escattr(ref)))
1.173 +
1.174 + for option, label in sourcedict.items():
1.175 + output.append(fmt.rawHTML('<option value="%s" %s>%s</option>' % (
1.176 + escattr(option), value == option and "selected" or "", escape(label))
1.177 + ))
1.178 +
1.179 + output.append(fmt.rawHTML('</select>'))
1.180 + return u''.join(output)
1.181
1.182 else:
1.183 - return ''
1.184 + return u''
1.185
1.186 def showError(text, request):
1.187 fmt = request.formatter