codekingpro/portable-devtools
114k
1from wtforms import i18n2from wtforms.utils import WebobInputWrapper3from wtforms.widgets.core import clean_key4 5 6class DefaultMeta:7 """8 This is the default Meta class which defines all the default values and9 therefore also the 'API' of the class Meta interface.10 """11 12 # -- Basic form primitives13 14 def bind_field(self, form, unbound_field, options):15 """16 bind_field allows potential customization of how fields are bound.17 18 The default implementation simply passes the options to19 :meth:`UnboundField.bind`.20 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.25 26 :return: A bound field27 """28 return unbound_field.bind(form=form, **options)29 30 def wrap_formdata(self, form, formdata):31 """32 wrap_formdata allows doing custom wrappers of WTForms formdata.33 34 The default implementation detects webob-style multidicts and wraps35 them, otherwise passes formdata back un-changed.36 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 formdata50 51 def render_field(self, field, render_kw):52 """53 render_field allows customization of how widget rendering is done.54 55 The default implementation calls ``field.widget(field, **render_kw)``56 """57 58 render_kw = {clean_key(k): v for k, v in render_kw.items()}59 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)65 66 # -- CSRF67 68 csrf = False69 csrf_field_name = "csrf_token"70 csrf_secret = None71 csrf_context = None72 csrf_class = None73 74 def build_csrf(self, form):75 """76 Build a CSRF implementation. This is called once per form instance.77 78 The default implementation builds the class referenced to by79 :attr:`csrf_class` with zero arguments. If `csrf_class` is ``None``,80 will instead use the default implementation81 :class:`wtforms.csrf.session.SessionCSRF`.82 83 :param form: The form.84 :return: A CSRF implementation.85 """86 if self.csrf_class is not None:87 return self.csrf_class()88 89 from wtforms.csrf.session import SessionCSRF90 91 return SessionCSRF()92 93 # -- i18n94 95 locales = False96 cache_translations = True97 translations_cache = {}98 99 def get_translations(self, form):100 """101 Override in subclasses to provide alternate translations factory.102 See the i18n documentation for more.103 104 :param form: The form.105 :return: An object that provides gettext() and ngettext() methods.106 """107 locales = self.locales108 if locales is False:109 return None110 111 if self.cache_translations:112 # Make locales be a hashable value113 locales = tuple(locales) if locales else None114 115 translations = self.translations_cache.get(locales)116 if translations is None:117 translations = self.translations_cache[locales] = i18n.get_translations(118 locales119 )120 121 return translations122 123 return i18n.get_translations(locales)124 125 # -- General126 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)133 