U
    fßôg  ã                   @   s6   d dl mZ d dlmZ d dlmZ G dd„ dƒZdS )é    )Úi18n)ÚWebobInputWrapper©Ú	clean_keyc                   @   s`   e Zd ZdZdd„ Zdd„ Zdd„ ZdZd	Zd
Z	d
Z
d
Zdd„ ZdZdZi Zdd„ Zdd„ Zd
S )ÚDefaultMetaz‹
    This is the default Meta class which defines all the default values and
    therefore also the 'API' of the class Meta interface.
    c                 C   s   |j f d|i|—ŽS )aŽ  
        bind_field allows potential customization of how fields are bound.

        The default implementation simply passes the options to
        :meth:`UnboundField.bind`.

        :param form: The form.
        :param unbound_field: The unbound field.
        :param options:
            A dictionary of options which are typically passed to the field.

        :return: A bound field
        Úform)Úbind)Úselfr   Zunbound_fieldÚoptions© r   ú0/tmp/pip-unpacked-wheel-p06mjha7/wtforms/meta.pyÚ
bind_field   s    zDefaultMeta.bind_fieldc                 C   s0   |dk	r,t |dƒs,t |dƒr$t|ƒS tdƒ‚|S )aZ  
        wrap_formdata allows doing custom wrappers of WTForms formdata.

        The default implementation detects webob-style multidicts and wraps
        them, otherwise passes formdata back un-changed.

        :param form: The form.
        :param formdata: Form data.
        :return: A form-input wrapper compatible with WTForms.
        NÚgetlistZgetallzNformdata should be a multidict-type wrapper that supports the 'getlist' method)Úhasattrr   Ú	TypeError)r	   r   Zformdatar   r   r   Úwrap_formdata   s    
ÿzDefaultMeta.wrap_formdatac                 C   sR   dd„ |  ¡ D ƒ}t|ddƒ}|dk	rDdd„ |  ¡ D ƒ}t|f|Ž}|j|f|ŽS )z£
        render_field allows customization of how widget rendering is done.

        The default implementation calls ``field.widget(field, **render_kw)``
        c                 S   s   i | ]\}}t |ƒ|“qS r   r   ©Ú.0ÚkÚvr   r   r   Ú
<dictcomp>:   s      z,DefaultMeta.render_field.<locals>.<dictcomp>Ú	render_kwNc                 S   s   i | ]\}}t |ƒ|“qS r   r   r   r   r   r   r   >   s      )ÚitemsÚgetattrÚdictZwidget)r	   Úfieldr   Zother_kwr   r   r   Úrender_field3   s    zDefaultMeta.render_fieldFZ
csrf_tokenNc                 C   s$   | j dk	r|   ¡ S ddlm} |ƒ S )a—  
        Build a CSRF implementation. This is called once per form instance.

        The default implementation builds the class referenced to by
        :attr:`csrf_class` with zero arguments. If `csrf_class` is ``None``,
        will instead use the default implementation
        :class:`wtforms.csrf.session.SessionCSRF`.

        :param form: The form.
        :return: A CSRF implementation.
        Nr   )ÚSessionCSRF)Ú
csrf_classZwtforms.csrf.sessionr   )r	   r   r   r   r   r   Ú
build_csrfJ   s    
zDefaultMeta.build_csrfTc                 C   s^   | j }|dkrdS | jrT|r$t|ƒnd}| j |¡}|dkrPt |¡ }| j|< |S t |¡S )zë
        Override in subclasses to provide alternate translations factory.
        See the i18n documentation for more.

        :param form: The form.
        :return: An object that provides gettext() and ngettext() methods.
        FN)ÚlocalesÚcache_translationsÚtupleÚtranslations_cacheÚgetr   Úget_translations)r	   r   r    Ztranslationsr   r   r   r%   c   s    ÿzDefaultMeta.get_translationsc                 C   s"   |  ¡ D ]\}}t| ||ƒ qdS )zV
        Given a dictionary of values, update values on this `Meta` instance.
        N)r   Úsetattr)r	   ÚvaluesÚkeyÚvaluer   r   r   Úupdate_values   s    zDefaultMeta.update_values)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   ZcsrfZcsrf_field_nameZcsrf_secretZcsrf_contextr   r   r    r!   r#   r%   r*   r   r   r   r   r      s   r   N)Zwtformsr   Zwtforms.utilsr   Zwtforms.widgets.corer   r   r   r   r   r   Ú<module>   s   