Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/wtforms/meta.py: 39%
46 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:32 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:32 +0000
1from wtforms import i18n
2from wtforms.utils import WebobInputWrapper
3from wtforms.widgets.core import clean_key
6class DefaultMeta:
7 """
8 This is the default Meta class which defines all the default values and
9 therefore also the 'API' of the class Meta interface.
10 """
12 # -- Basic form primitives
14 def bind_field(self, form, unbound_field, options):
15 """
16 bind_field allows potential customization of how fields are bound.
18 The default implementation simply passes the options to
19 :meth:`UnboundField.bind`.
21 :param form: The form.
22 :param unbound_field: The unbound field.
23 :param options:
24 A dictionary of options which are typically passed to the field.
26 :return: A bound field
27 """
28 return unbound_field.bind(form=form, **options)
30 def wrap_formdata(self, form, formdata):
31 """
32 wrap_formdata allows doing custom wrappers of WTForms formdata.
34 The default implementation detects webob-style multidicts and wraps
35 them, otherwise passes formdata back un-changed.
37 :param form: The form.
38 :param formdata: Form data.
39 :return: A form-input wrapper compatible with WTForms.
40 """
41 if formdata is not None and not hasattr(formdata, "getlist"):
42 if hasattr(formdata, "getall"):
43 return WebobInputWrapper(formdata)
44 else:
45 raise TypeError(
46 "formdata should be a multidict-type wrapper that"
47 " supports the 'getlist' method"
48 )
49 return formdata
51 def render_field(self, field, render_kw):
52 """
53 render_field allows customization of how widget rendering is done.
55 The default implementation calls ``field.widget(field, **render_kw)``
56 """
58 render_kw = {clean_key(k): v for k, v in render_kw.items()}
60 other_kw = getattr(field, "render_kw", None)
61 if other_kw is not None:
62 other_kw = {clean_key(k): v for k, v in other_kw.items()}
63 render_kw = dict(other_kw, **render_kw)
64 return field.widget(field, **render_kw)
66 # -- CSRF
68 csrf = False
69 csrf_field_name = "csrf_token"
70 csrf_secret = None
71 csrf_context = None
72 csrf_class = None
74 def build_csrf(self, form):
75 """
76 Build a CSRF implementation. This is called once per form instance.
78 The default implementation builds the class referenced to by
79 :attr:`csrf_class` with zero arguments. If `csrf_class` is ``None``,
80 will instead use the default implementation
81 :class:`wtforms.csrf.session.SessionCSRF`.
83 :param form: The form.
84 :return: A CSRF implementation.
85 """
86 if self.csrf_class is not None:
87 return self.csrf_class()
89 from wtforms.csrf.session import SessionCSRF
91 return SessionCSRF()
93 # -- i18n
95 locales = False
96 cache_translations = True
97 translations_cache = {}
99 def get_translations(self, form):
100 """
101 Override in subclasses to provide alternate translations factory.
102 See the i18n documentation for more.
104 :param form: The form.
105 :return: An object that provides gettext() and ngettext() methods.
106 """
107 locales = self.locales
108 if locales is False:
109 return None
111 if self.cache_translations:
112 # Make locales be a hashable value
113 locales = tuple(locales) if locales else None
115 translations = self.translations_cache.get(locales)
116 if translations is None:
117 translations = self.translations_cache[locales] = i18n.get_translations(
118 locales
119 )
121 return translations
123 return i18n.get_translations(locales)
125 # -- General
127 def update_values(self, values):
128 """
129 Given a dictionary of values, update values on this `Meta` instance.
130 """
131 for key, value in values.items():
132 setattr(self, key, value)