U
    gg                     @   s   d dl mZ d dl mZ d dl mZ d dlmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZ d d	lmZ d d
lmZ ddlmZ zddlmZ W n ek
r   dZY nX ddddhZe ZG dd deZdd ZdS )    )current_app)request)session)Markup)CombinedMultiDict)ImmutableMultiDict)cached_property)Form)DefaultMeta)HiddenInput   )_FlaskFormCSRF)translationsNPOSTPUTPATCHDELETEc                       sN   e Zd ZdZG dd deZef fdd	Zdd Zdd	d
Z	dd Z
  ZS )	FlaskFormzFlask-specific subclass of WTForms :class:`~wtforms.form.Form`.

    If ``formdata`` is not specified, this will use :attr:`flask.request.form`
    and :attr:`flask.request.files`.  Explicitly pass ``formdata=None`` to
    prevent this.
    c                       s\   e Zd ZeZeZedd Zedd Z	edd Z
edd Zd	d
 Z fddZ  ZS )zFlaskForm.Metac                 C   s   t jddS )NZWTF_CSRF_ENABLEDTr   configgetself r   2/tmp/pip-unpacked-wheel-htolim9p/flask_wtf/form.pycsrf$   s    zFlaskForm.Meta.csrfc                 C   s   t jdt jS )NZWTF_CSRF_SECRET_KEY)r   r   r   Z
secret_keyr   r   r   r   csrf_secret(   s    zFlaskForm.Meta.csrf_secretc                 C   s   t jddS )NZWTF_CSRF_FIELD_NAMEZ
csrf_tokenr   r   r   r   r   csrf_field_name,   s    zFlaskForm.Meta.csrf_field_namec                 C   s   t jddS )NZWTF_CSRF_TIME_LIMITi  r   r   r   r   r   csrf_time_limit0   s    zFlaskForm.Meta.csrf_time_limitc                 C   sJ   |t krFt rBtjr$ttjtjfS tjr0tjS tjrBtt S d S |S N)	_Auto_is_submittedr   filesr   formZis_jsonr   Zget_json)r   r#   formdatar   r   r   wrap_formdata4   s    zFlaskForm.Meta.wrap_formdatac                    s   t jddst |S tS )NZWTF_I18N_ENABLEDT)r   r   r   superget_translationsr   )r   r#   	__class__r   r   r'   B   s    zFlaskForm.Meta.get_translations)__name__
__module____qualname__r   Z
csrf_classr   Zcsrf_contextr   r   r   r   r   r%   r'   __classcell__r   r   r(   r   Meta    s   



r.   c                    s   t  jf d|i| d S )Nr$   )r&   __init__)r   r$   kwargsr(   r   r   r/   H   s    zFlaskForm.__init__c                 C   s   t  S )zConsider the form submitted if there is an active request and
        the method is ``POST``, ``PUT``, ``PATCH``, or ``DELETE``.
        )r!   r   r   r   r   is_submittedK   s    zFlaskForm.is_submittedNc                 C   s   |   o| j|dS )zCall :meth:`validate` only if the form is submitted.
        This is a shortcut for ``form.is_submitted() and form.validate()``.
        )extra_validators)r1   validate)r   r2   r   r   r   validate_on_submitR   s    zFlaskForm.validate_on_submitc                    s,    fdd}t ddd ||p  D S )a?  Render the form's hidden fields in one call.

        A field is considered hidden if it uses the
        :class:`~wtforms.widgets.HiddenInput` widget.

        If ``fields`` are given, only render the given fields that
        are hidden.  If a string is passed, render the field with that
        name if it exists.

        .. versionchanged:: 0.13

           No longer wraps inputs in hidden div.
           This is valid HTML 5.

        .. versionchanged:: 0.13

           Skip passed fields that aren't hidden.
           Skip passed names that don't exist.
        c                 3   s@   | D ]6}t |trt |d }|d kst |jts4q|V  qd S r   )
isinstancestrgetattrZwidgetr   )fieldsfr   r   r   hidden_fieldsm   s    
z+FlaskForm.hidden_tag.<locals>.hidden_fields
c                 s   s   | ]}t |V  qd S r   )r6   ).0r9   r   r   r   	<genexpr>w   s     z'FlaskForm.hidden_tag.<locals>.<genexpr>)r   join)r   r8   r:   r   r   r   
hidden_tagX   s    
zFlaskForm.hidden_tag)N)r*   r+   r,   __doc__r
   r.   r    r/   r1   r4   r?   r-   r   r   r(   r   r      s   (
r   c                   C   s   t totjtkS )zConsider the form submitted if there is an active request and
    the method is ``POST``, ``PUT``, ``PATCH``, or ``DELETE``.
    )boolr   methodSUBMIT_METHODSr   r   r   r   r!   z   s    r!   )Zflaskr   r   r   Z
markupsafer   Zwerkzeug.datastructuresr   r   Zwerkzeug.utilsr   Zwtformsr	   Zwtforms.metar
   Zwtforms.widgetsr   r   r   Zi18nr   ImportErrorrC   objectr    r   r!   r   r   r   r   <module>   s$   
b