annotate data/plugin/macro/FormBase.py @ 628:819c4121f9a0

FormCreate/FormSubmit: change to LF-only line endings
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Mon, 25 Mar 2013 19:48:31 +0100
parents a8d1eb833705
children
rev   line source
628
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
1 # -*- coding: iso-8859-1 -*-
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
2 """
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
3 MoinMoin - FormBase Macro
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
4
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
5 Base stuff for all form macros.
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
6 Macro generates HTML form elements in following sequence:
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
7 - parse_args parse defined parameters
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
8 - validate validate correct definition of parameters
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
9 - build build HTML element and stores it into self.output
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
10
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
11 @copyright: 2008 by Peter Bodi <petrdll@centrum.sk>
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
12 @license: GNU GPL, see COPYING for details.
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
13 """
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
14
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
15 from MoinMoin import wikiutil
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
16
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
17
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
18 class FormBase(object):
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
19
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
20 debug = False
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
21
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
22 def __init__(self, macro, args):
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
23 self.macro = macro
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
24 self.request = macro.request
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
25 self._ = self.request.getText
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
26 self.module = self.__module__.split(".").pop()
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
27 self.args = args
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
28
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
29 self.msg = ""
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
30 self.output = ""
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
31
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
32 # Macro parameters
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
33 self._main = {} # Main parameters
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
34 self._attribs = {} # in FormField macros used as HTML tag attributes
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
35 self._params = {} # Additional parameters
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
36
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
37 def parse_args(self):
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
38 """ Parse parameters into specific macro properties """
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
39 self._main, self._attribs, self._params = wikiutil.parse_quoted_separated(self.args)
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
40
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
41 def validate(self):
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
42 """ Validates macro parameters """
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
43 for name, value in self._attribs.iteritems():
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
44 if not hasattr(self, name):
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
45 self.msg = self._("Invalid attribute [%(attr)s] specified in macro [%(mod)s]" % {
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
46 'attr': name, 'mod': self.module})
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
47 raise FormValidationError(self.msg)
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
48
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
49 def build(self):
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
50 """ Build HTML element """
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
51 raise NotImplementedError
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
52
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
53 def render(self):
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
54 """ Executes core methods: parse_args, validate, build
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
55
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
56 Returns output as HTML element or error message.
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
57 """
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
58 if not self.debug:
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
59 try:
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
60 self.parse_args()
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
61 self.validate()
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
62 self.build()
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
63 except FormValidationError:
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
64 return self.msg
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
65 except Exception, e:
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
66 # TODO: Log exception
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
67 self.output = self._("Error - %s") % e
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
68
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
69 return self.output
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
70
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
71 else:
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
72 try:
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
73 self.parse_args()
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
74 self.validate()
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
75 self.build()
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
76 except FormValidationError:
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
77 return self.msg
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
78
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
79 return self.output
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
80
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
81 def _build_attribs(self, attribs):
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
82 """ Converts HTML element attributes from dictionary to string """
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
83 attrstr = ""
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
84 for name, value in attribs.iteritems():
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
85 if value:
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
86 attrstr += "%(name)s=\"%(value)s\" " % {'name': name, 'value': value}
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
87
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
88 return attrstr
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
89
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
90 def _build_input(self, type, name, args):
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
91 """ Builds HTML input element """
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
92 attrs = self._build_attribs(args)
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
93 if name is None:
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
94 input = "<input type=\"%(type)s\" %(attrs)s/>\n" % {'type': type, 'attrs': attrs}
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
95 else:
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
96 input = "<input type=\"%(type)s\" name=\"%(name)s\" %(attrs)s/>\n" % {
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
97 'type': type, 'name': name, 'attrs': attrs}
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
98 return input
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
99
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
100
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
101 class FormValidationError(Exception):
819c4121f9a0 FormCreate/FormSubmit: change to LF-only line endings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 621
diff changeset
102 """ Exception raised for errors during validation """