annotate MoinMoin/util/forms.py @ 1397:4ac437141bbe

make file ends pep8 compliant (== exactly 1 LF at EOF)
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sun, 24 Jun 2012 16:14:45 +0200
parents 7c7b05a0a30e
children 384555088cab
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
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
16 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
17 """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
18 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
19 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
20 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
21 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
22
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
23 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
24 """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
25 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
26 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
27 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
28 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
29 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
30 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
31 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
32 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
33 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
34 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
35
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
36 def required_filter(tagname, attributes, contents, context, bind):
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
37 if (bind is not None and not bind.optional):
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
38 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
39 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
40 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
41 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
42 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
43
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
44 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
45 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
46 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
47 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
48 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
49 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
50 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
51
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 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
53 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
54 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
55 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
56 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
57 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
58 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
59
0
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
60 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
61 """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
62
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
63 :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
64 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
65 """
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(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
67 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
68 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
69 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
70 else:
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
71 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
72 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
73 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
74 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
75
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
76 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
77
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
78
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
79 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
80 """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
81 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
82 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
83 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
84 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
85 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
86
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
87
a11656ba1eab modify views: use flatland also for the file upload form field
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 229
diff changeset
88 # 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
89
a11656ba1eab modify views: use flatland also for the file upload form field
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 229
diff changeset
90 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
91 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
92
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 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
95 """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
96
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 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
98 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
99 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
100 return value