annotate MoinMoin/util/forms.py @ 1983:bb2f526d961c

fix some stuff discovered by pycharm code inspection some changes fix yet undiscovered bugs (e.g. due to wrong names), other changes are rather cosmetic or improve docstrings.
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Mon, 11 Feb 2013 18:48:03 +0100
parents 384555088cab
children eca842df2ec4
rev   line source
26
4cd8edde9e94 automatically transform module docstrings, moving legal stuff to comments
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 21
diff changeset
1 # Copyright: 2010 Thomas Waldmann, Jason Kirtland, Scott Wilson
4cd8edde9e94 automatically transform module docstrings, moving legal stuff to comments
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 21
diff changeset
2 # License: see flatland license
4cd8edde9e94 automatically transform module docstrings, moving legal stuff to comments
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 21
diff changeset
3
0
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
4 """
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
5 MoinMoin - form helpers for flatland / jinja2
26
4cd8edde9e94 automatically transform module docstrings, moving legal stuff to comments
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 21
diff changeset
6 """
0
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
7
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
8
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
9 from jinja2 import Markup
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
10
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
11 from flatland.out.markup import Generator
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
12 from flatland.schema.util import find_i18n_function
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
13
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
14 from MoinMoin.i18n import _, L_, N_
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
15
1975
384555088cab lots of pep8 fixes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1397
diff changeset
16
0
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
17 def label_filter(tagname, attributes, contents, context, bind):
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
18 """Provide a translated, generated fallback for field labels."""
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
19 if bind is not None and not contents:
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
20 contents = _(bind.label)
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
21 return contents
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
22 label_filter.tags = set(['label'])
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
23
1975
384555088cab lots of pep8 fixes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1397
diff changeset
24
0
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
25 def button_filter(tagname, attributes, contents, context, bind):
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
26 """Show translated text in clickable buttons and submits."""
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
27 if bind is None:
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
28 return contents
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
29 if tagname == 'input':
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
30 if ('value' not in attributes and
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
31 attributes.get('type') in ['submit', 'reset', ]):
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
32 attributes['value'] = _(bind.default_value)
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
33 elif tagname == 'button' and not contents:
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
34 contents = _(bind.default_value)
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
35 return contents
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
36 button_filter.tags = set(['input', 'button'])
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
37
1975
384555088cab lots of pep8 fixes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1397
diff changeset
38
218
fd36d76dd0e3 util.forms: new filter for required attributes added, thanks to Jason Kirtland
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 26
diff changeset
39 def required_filter(tagname, attributes, contents, context, bind):
1983
bb2f526d961c fix some stuff discovered by pycharm code inspection
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1975
diff changeset
40 if bind is not None and not bind.optional:
218
fd36d76dd0e3 util.forms: new filter for required attributes added, thanks to Jason Kirtland
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 26
diff changeset
41 attributes[u'class'] = u'required'
229
c956811fba9a flatland filters: fix copy&paste bug, only use "required" attr in input fields
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 226
diff changeset
42 if tagname == 'input':
c956811fba9a flatland filters: fix copy&paste bug, only use "required" attr in input fields
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 226
diff changeset
43 attributes[u'required'] = u'required'
218
fd36d76dd0e3 util.forms: new filter for required attributes added, thanks to Jason Kirtland
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 26
diff changeset
44 return contents
fd36d76dd0e3 util.forms: new filter for required attributes added, thanks to Jason Kirtland
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 26
diff changeset
45 required_filter.tags = set(['input', 'label'])
0
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
46
1975
384555088cab lots of pep8 fixes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1397
diff changeset
47
226
029cb888d253 use flatland filters for autofocus/placeholder html5 attrs, use html5 input field types email/url/search
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 218
diff changeset
48 def autofocus_filter(tagname, attributes, contents, context, bind):
029cb888d253 use flatland filters for autofocus/placeholder html5 attrs, use html5 input field types email/url/search
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 218
diff changeset
49 if bind is not None:
029cb888d253 use flatland filters for autofocus/placeholder html5 attrs, use html5 input field types email/url/search
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 218
diff changeset
50 autofocus = bind.properties.get('autofocus')
029cb888d253 use flatland filters for autofocus/placeholder html5 attrs, use html5 input field types email/url/search
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 218
diff changeset
51 if autofocus:
029cb888d253 use flatland filters for autofocus/placeholder html5 attrs, use html5 input field types email/url/search
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 218
diff changeset
52 attributes[u'autofocus'] = u'autofocus'
029cb888d253 use flatland filters for autofocus/placeholder html5 attrs, use html5 input field types email/url/search
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 218
diff changeset
53 return contents
229
c956811fba9a flatland filters: fix copy&paste bug, only use "required" attr in input fields
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 226
diff changeset
54 autofocus_filter.tags = set(['input', 'textarea', ])
226
029cb888d253 use flatland filters for autofocus/placeholder html5 attrs, use html5 input field types email/url/search
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 218
diff changeset
55
1975
384555088cab lots of pep8 fixes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1397
diff changeset
56
226
029cb888d253 use flatland filters for autofocus/placeholder html5 attrs, use html5 input field types email/url/search
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 218
diff changeset
57 def placeholder_filter(tagname, attributes, contents, context, bind):
029cb888d253 use flatland filters for autofocus/placeholder html5 attrs, use html5 input field types email/url/search
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 218
diff changeset
58 if bind is not None:
029cb888d253 use flatland filters for autofocus/placeholder html5 attrs, use html5 input field types email/url/search
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 218
diff changeset
59 placeholder = bind.properties.get('placeholder')
029cb888d253 use flatland filters for autofocus/placeholder html5 attrs, use html5 input field types email/url/search
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 218
diff changeset
60 if placeholder:
029cb888d253 use flatland filters for autofocus/placeholder html5 attrs, use html5 input field types email/url/search
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 218
diff changeset
61 attributes[u'placeholder'] = placeholder
029cb888d253 use flatland filters for autofocus/placeholder html5 attrs, use html5 input field types email/url/search
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 218
diff changeset
62 return contents
229
c956811fba9a flatland filters: fix copy&paste bug, only use "required" attr in input fields
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 226
diff changeset
63 placeholder_filter.tags = set(['input', 'textarea', ])
226
029cb888d253 use flatland filters for autofocus/placeholder html5 attrs, use html5 input field types email/url/search
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 218
diff changeset
64
1975
384555088cab lots of pep8 fixes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1397
diff changeset
65
0
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
66 def error_filter_factory(class_='moin-error'):
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
67 """Returns an HTML generation filter annotating field CSS class on error.
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
68
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
69 :param class: The css class to apply in case of validation error on a
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
70 field. Default: 'error'
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
71 """
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
72 def error_filter(tagname, attributes, contents, context, bind):
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
73 if bind is not None and bind.errors:
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
74 if 'class' in attributes:
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
75 attributes['class'] = ' '.join([attributes['class'], class_])
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
76 else:
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
77 attributes['class'] = class_
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
78 return contents
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
79 error_filter.tags = set(['input'])
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
80 return error_filter
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
81
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
82 error_filter = error_filter_factory()
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
83
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
84
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
85 def make_generator():
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
86 """make an html generator"""
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
87 return Generator(auto_domid=True, auto_for=True, auto_filter=True,
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
88 markup_wrapper=Markup,
218
fd36d76dd0e3 util.forms: new filter for required attributes added, thanks to Jason Kirtland
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 26
diff changeset
89 filters=[label_filter, button_filter,
226
029cb888d253 use flatland filters for autofocus/placeholder html5 attrs, use html5 input field types email/url/search
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 218
diff changeset
90 error_filter,
029cb888d253 use flatland filters for autofocus/placeholder html5 attrs, use html5 input field types email/url/search
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 218
diff changeset
91 required_filter, placeholder_filter, autofocus_filter])
0
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
92
244
a11656ba1eab modify views: use flatland also for the file upload form field
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 229
diff changeset
93
a11656ba1eab modify views: use flatland also for the file upload form field
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 229
diff changeset
94 # other flatland stuff
a11656ba1eab modify views: use flatland also for the file upload form field
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 229
diff changeset
95
a11656ba1eab modify views: use flatland also for the file upload form field
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 229
diff changeset
96 from flatland import AdaptationError, Scalar
a11656ba1eab modify views: use flatland also for the file upload form field
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 229
diff changeset
97 import werkzeug
a11656ba1eab modify views: use flatland also for the file upload form field
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 229
diff changeset
98
a11656ba1eab modify views: use flatland also for the file upload form field
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 229
diff changeset
99
a11656ba1eab modify views: use flatland also for the file upload form field
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 229
diff changeset
100 class FileStorage(Scalar):
a11656ba1eab modify views: use flatland also for the file upload form field
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 229
diff changeset
101 """Schema element for Werkzeug FileStorage instances."""
a11656ba1eab modify views: use flatland also for the file upload form field
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 229
diff changeset
102
a11656ba1eab modify views: use flatland also for the file upload form field
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 229
diff changeset
103 def adapt(self, value):
a11656ba1eab modify views: use flatland also for the file upload form field
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 229
diff changeset
104 if not isinstance(value, (type(None), werkzeug.FileStorage)):
a11656ba1eab modify views: use flatland also for the file upload form field
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 229
diff changeset
105 raise AdaptationError
a11656ba1eab modify views: use flatland also for the file upload form field
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 229
diff changeset
106 return value