o
    [h.                     @   s   d dl Z zd dlmZ W n ey   d dlmZ Y nw d dlmZmZmZ d dl	m
Z
 dZG dd deZG dd	 d	eZG d
d deeeZdS )    N)OrderedDict)with_metaclass	iteritems
itervalues)DefaultMeta)BaseFormFormc                   @   s   e Zd ZdZde fddZdd Zdd Zd	d
 Zdd Z	dd Z
dd Zdd ZdddZdddZedd Zedd ZdS )r   zy
    Base Form Class.  Provides core behaviour like field construction,
    validation, and data and error proxying.
     c           
      C   s   |r|d dvr|d7 }|| _ || _t | _t|dr| }|  }g }|jr7|| | _	|
| j	|  t||D ]\}}t|||d}|| ||}	|	| j|< q=dS )af  
        :param fields:
            A dict or sequence of 2-tuples of partially-constructed fields.
        :param prefix:
            If provided, all fields will have their name prefixed with the
            value.
        :param meta:
            A meta instance which is used for configuration and customization
            of WTForms behaviors.
        z-_;:/.-items)nameprefixtranslationsN)meta_prefixr   _fieldshasattrr   _get_translationsZcsrfZ
build_csrfZ_csrfextendZ
setup_form	itertoolschaindictZ
bind_field)
selffieldsr   r   r   Zextra_fieldsr   unbound_fieldoptionsfield r   s/home/ubuntu/experiments/live_experiments/Pythonexperiments/Otree/venv/lib/python3.10/site-packages/wtforms/form.py__init__   s"   
zBaseForm.__init__c                 C   s   t t| jS )z&Iterate form fields in creation order.)iterr   r   r   r   r   r   __iter__6   s   zBaseForm.__iter__c                 C   s
   || j v S )z= Returns `True` if the named field is a member of this form. r   r   r   r   r   r   __contains__:      
zBaseForm.__contains__c                 C   s
   | j | S )z) Dict-style access to this form's fields.r$   r%   r   r   r   __getitem__>   r'   zBaseForm.__getitem__c                 C   s   |j | || jd| j|< dS )z Bind a field to this form. )formr   r   N)bindr   r   r   r   valuer   r   r   __setitem__B   s   zBaseForm.__setitem__c                 C   s   | j |= dS )z  Remove a field from this form. Nr$   r%   r   r   r   __delitem__F   s   zBaseForm.__delitem__c                 C   s   | j | S )a1  
        .. deprecated:: 2.0
            `_get_translations` is being removed in WTForms 3.0, use
            `Meta.get_translations` instead.

        Override in subclasses to provide alternate translations factory.

        Must return an object that provides gettext() and ngettext() methods.
        )r   Zget_translationsr"   r   r   r   r   J   s   
zBaseForm._get_translationsc                 C   s$   t | jD ]
\}}||| qdS )z
        Populates the attributes of the passed `obj` with data from the form's
        fields.

        :note: This is a destructive operation; Any attribute with the same name
               as a field will be overridden. Use with caution.
        N)r   r   populate_obj)r   objr   r   r   r   r   r/   V   s   zBaseForm.populate_objNc                 K   s   | j | |}|durt|fi |}t| jD ])\}}|dur/t||r/||t|| q||v r<||||  q|| qdS )a  
        Take form, object data, and keyword arg input and have the fields
        process them.

        :param formdata:
            Used to pass data coming from the enduser, usually `request.POST` or
            equivalent.
        :param obj:
            If `formdata` is empty or not provided, this object is checked for
            attributes matching form field names, which will be used for field
            values.
        :param data:
            If provided, must be a dictionary of data. This is only used if
            `formdata` is empty or not provided and `obj` does not contain
            an attribute named the same as the field.
        :param `**kwargs`:
            If `formdata` is empty or not provided and `obj` does not contain
            an attribute named the same as a field, form will assign the value
            of a matching keyword argument to the field, if one exists.
        N)r   Zwrap_formdatar   r   r   r   processgetattr)r   formdatar0   datakwargsr   r   r   r   r   r1   a   s   zBaseForm.processc                 C   sL   d}t | jD ]\}}|dur||v r|| }nt }|| |s#d}q|S )aL  
        Validates the form by calling `validate` on each field.

        :param extra_validators:
            If provided, is a dict mapping field names to a sequence of
            callables which will be passed as extra validators to the field's
            `validate` method.

        Returns `True` if no errors occur.
        TNF)r   r   tuplevalidate)r   extra_validatorssuccessr   r   extrar   r   r   r7      s   
zBaseForm.validatec                 C   s   t dd t| jD S )Nc                 s   s    | ]
\}}||j fV  qd S N)r4   .0r   fr   r   r   	<genexpr>   s    z BaseForm.data.<locals>.<genexpr>)r   r   r   r"   r   r   r   r4      s   zBaseForm.datac                 C   s   dd | j  D S )Nc                 S   s   i | ]\}}|j r||j qS r   )errorsr<   r   r   r   
<dictcomp>   s    z#BaseForm.errors.<locals>.<dictcomp>)r   r   r"   r   r   r   r@      s   zBaseForm.errors)NNNr;   )__name__
__module____qualname____doc__r   r    r#   r&   r(   r-   r.   r   r/   r1   r7   propertyr4   r@   r   r   r   r   r      s      

$
r   c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )FormMetaa  
    The metaclass for `Form` and any subclasses of `Form`.

    `FormMeta`'s responsibility is to create the `_unbound_fields` list, which
    is a list of `UnboundField` instances sorted by their order of
    instantiation.  The list is created at the first instantiation of the form.
    If any fields are added/removed from the form, the list is cleared to be
    re-generated on the next instantiation.

    Any properties which begin with an underscore or are not `UnboundField`
    instances are ignored by the metaclass.
    c                 C   s    t | ||| d | _d | _d S r;   )typer    _unbound_fields_wtforms_meta)clsr   basesattrsr   r   r   r       s   
zFormMeta.__init__c                 O   s   | j du r/g }t| D ]}|ds#t| |}t|dr#|||f q|jdd d || _ | jdu rPg }| jD ]}d|j	v rF||j
 q9tdt|i | _tj| g|R i |S )z
        Construct a new `Form` instance.

        Creates the `_unbound_fields` list and the internal `_wtforms_meta`
        subclass of the class Meta in order to allow a proper inheritance
        hierarchy.
        N_
_formfieldc                 S   s   | d j | d fS )N   r   )Zcreation_counter)xr   r   r   <lambda>   s    z#FormMeta.__call__.<locals>.<lambda>)keyMeta)rI   dir
startswithr2   r   appendsortrJ   __mro____dict__rT   rH   r6   __call__)rK   argsr5   r   r   r   rL   Z	mro_classr   r   r   r[      s$   






zFormMeta.__call__c                 C   s<   |dkrd| _ n|dst|drd| _t| || dS )zV
        Add an attribute to the class, clearing `_unbound_fields` if needed.
        rT   NrN   rO   )rJ   rV   r   rI   rH   __setattr__)rK   r   r,   r   r   r   r]      s
   zFormMeta.__setattr__c                 C   s    | dsd| _t| | dS )zc
        Remove an attribute from the class, clearing `_unbound_fields` if
        needed.
        rN   N)rV   rI   rH   __delattr__)rK   r   r   r   r   r^      s   
zFormMeta.__delattr__N)rB   rC   rD   rE   r    r[   r]   r^   r   r   r   r   rG      s    
rG   c                       sP   e Zd ZdZeZd fdd	Zdd Zdd	 Z fd
dZ	d fdd	Z
  ZS )r   z
    Declarative Form base class. Extends BaseForm's core behaviour allowing
    fields to be defined on Form subclasses as class attributes.

    In addition, form and instance input data are taken at construction time
    and passed to `process()`.
    Nr	   c           
         sx   |   }|durt|tr|| tt| j| j||d t| j	D ]
\}}	t
| ||	 q#| j||fd|i| dS )a  
        :param formdata:
            Used to pass data coming from the enduser, usually `request.POST` or
            equivalent. formdata should be some sort of request-data wrapper which
            can get multiple parameters from the form input, and values are unicode
            strings, e.g. a Werkzeug/Django/WebOb MultiDict
        :param obj:
            If `formdata` is empty or not provided, this object is checked for
            attributes matching form field names, which will be used for field
            values.
        :param prefix:
            If provided, all fields will have their name prefixed with the
            value.
        :param data:
            Accept a dictionary of data. This is only used if `formdata` and
            `obj` are not present.
        :param meta:
            If provided, this is a dictionary of values to override attributes
            on this form's meta instance.
        :param `**kwargs`:
            If `formdata` is empty or not provided and `obj` does not contain
            an attribute named the same as a field, form will assign the value
            of a matching keyword argument to the field, if one exists.
        N)r   r   r4   )rJ   
isinstancer   Zupdate_valuessuperr   r    rI   r   r   setattrr1   )
r   r3   r0   r   r4   r   r5   Zmeta_objr   r   	__class__r   r   r       s   
zForm.__init__c                 C   s   t d)Nz8Fields may not be added to Form instances, only classes.)	TypeErrorr+   r   r   r   r-     s   zForm.__setitem__c                 C   s   | j |= t| |d  d S r;   )r   ra   r%   r   r   r   r.     s   zForm.__delitem__c                    s\   || j v r| | d S t| j|d }|d ur$t|dr$t| |d  d S tt| | d S )NrO   )	r   r.   r2   rc   r   ra   r`   r   r^   )r   r   r   rb   r   r   r^     s   
zForm.__delattr__c                    s^   |dur	|  }ni }| jD ]}t| jd| d}|dur&||g | qtt| |S )a  Validate the form by calling ``validate`` on each field.
        Returns ``True`` if validation passes.

        If the form defines a ``validate_<fieldname>`` method, it is
        appended as an extra validator for the field's ``validate``.

        :param extra_validators: A dict mapping field names to lists of
            extra validator methods to run. Extra validators run after
            validators passed when creating the field. If the form has
            ``validate_<fieldname>``, it is the last extra validator.
        Nzvalidate_%s)	copyr   r2   rc   
setdefaultrW   r`   r   r7   )r   r8   r:   r   inlinerb   r   r   r7   '  s   

zForm.validate)NNr	   NNr;   )rB   rC   rD   rE   r   rT   r    r-   r.   r^   r7   __classcell__r   r   rb   r   r      s    $r   )r   collectionsr   ImportErrorZordereddictZwtforms.compatr   r   r   Zwtforms.metar   __all__objectr   rH   rG   r   r   r   r   r   <module>   s     C