o
    ŠågG  ã                   @   sP   d dl Z d dlmZ ddlmZ ddlmZ ddlmZ dZG d	d
„ d
eƒZ	dS )é    N)Úunset_valueé   )Úwidgetsé   )ÚField)ÚUnboundField)Ú	FieldListc                       s¦   e Zd ZdZe ¡ Z						d‡ fdd„	Zedfdd	„Z	d
d„ Z
ddd„Zdd„ Zdedfdd„Zefdd„Zdd„ Zdd„ Zdd„ Zdd„ Zedd„ ƒZ‡  ZS ) r   a/  
    Encapsulate an ordered list of multiple instances of the same field type,
    keeping data as a list.

    >>> authors = FieldList(StringField('Name', [validators.DataRequired()]))

    :param unbound_field:
        A partially-instantiated field definition, just like that would be
        defined on a form directly.
    :param min_entries:
        if provided, always have at least this many entries on the field,
        creating blank ones if the provided input does not specify a sufficient
        amount.
    :param max_entries:
        accept no more than this many entries as input, even if more exist in
        formdata.
    :param separator:
        A string which will be suffixed to this field's name to create the
        prefix to enclosed list entries. The default is fine for most uses.
    Nr   ú-© c           	         sz   t ƒ j||fd|i|¤Ž | jrtdƒ‚t|tƒsJ dƒ‚|| _|| _|| _d| _	| 
dd¡| _|| _|j 
dd¡| _d S )	NÚdefaultúRFieldList does not accept any filters. Instead, define them on the enclosed field.z(Field must be unbound, not a field classéÿÿÿÿÚ_prefixÚ Ú	separatorr	   )ÚsuperÚ__init__ÚfiltersÚ	TypeErrorÚ
isinstancer   Úunbound_fieldÚmin_entriesÚmax_entriesÚ
last_indexÚgetr   Ú
_separatorÚkwargsÚ_field_separator)	Úselfr   ÚlabelÚ
validatorsr   r   r   r   r   ©Ú	__class__r
   úO/var/www/html/flaskapp/venv/lib/python3.10/site-packages/wtforms/fields/list.pyr   $   s"   ÿÿþzFieldList.__init__c              	   C   s  |rt dƒ‚g | _|tu s|s"z|  ¡ }W n t y!   | j}Y nw || _|r`tt|  | j|¡ƒƒ}| j	r<|d | j	… }t
|ƒ}|D ]}zt|ƒ}W n tyU   t}Y nw | j|||d� qBn|D ]}|  ||¡ qbt| jƒ| jk r‚|  |¡ t| jƒ| jk ssd S d S )Nr   )Úindex)r   Úentriesr   r   Úobject_dataÚsortedÚsetÚ_extract_indicesÚnamer   ÚiterÚnextÚStopIterationÚ
_add_entryÚlenr   )r   ÚformdataÚdataÚextra_filtersÚindicesÚidatar$   Úobj_datar
   r
   r#   Úprocess@   s<   ÿ
ÿÿû
ÿzFieldList.processc                 c   sR   � t |ƒd }|D ]}| |¡r&||d…  | jd¡d }| ¡ r&t|ƒV  q	dS )a&  
        Yield indices of any keys with given prefix.

        formdata must be an object which will produce keys when iterated.  For
        example, if field 'foo' contains keys 'foo-0-bar', 'foo-1-baz', then
        the numbers 0 and 1 will be yielded, but not necessarily in order.
        r   Nr   )r/   Ú
startswithÚsplitr   ÚisdigitÚint)r   Úprefixr0   ÚoffsetÚkr
   r
   r#   r)   c   s   €

€üzFieldList._extract_indicesc                 C   sl   g | _ | jD ]}| |¡ | j  |j ¡ qtdd„ | j D ƒƒs"g | _ t | j|¡}|  ||¡ t	| j ƒdkS )zë
        Validate this FieldList.

        Note that FieldList validation differs from normal field validation in
        that FieldList validates all its enclosed fields first before running any
        of its own validators.
        c                 s   s   � | ]}|V  qd S ©Nr
   )Ú.0Úxr
   r
   r#   Ú	<genexpr>�   s   € z%FieldList.validate.<locals>.<genexpr>r   )
Úerrorsr%   ÚvalidateÚappendÚanyÚ	itertoolsÚchainr    Ú_run_validation_chainr/   )r   ÚformÚextra_validatorsÚsubfieldrG   r
   r
   r#   rC   r   s   

zFieldList.validatec                 C   s¢   t ||d ƒ}zt|ƒ}W n ty   tg ƒ}Y nw t |t d ¡¡}tdtfi ƒ}g }t| j	|ƒD ]\}}	|ƒ }
|	|
_
| |
d¡ | |
j
¡ q2t|||ƒ d S )NÚ_faker1   )Úgetattrr+   r   rF   rG   ÚrepeatÚtypeÚobjectÚzipr%   r1   Úpopulate_objrD   Úsetattr)r   Úobjr*   ÚvaluesÚivaluesÚ
candidatesrL   ÚoutputÚfieldr1   Úfake_objr
   r
   r#   rR   ‰   s   ÿzFieldList.populate_objc                 C   s˜   | j rt| jƒ| j k sJ dƒ‚|d u r| jd }|| _| j› | j› |› �}| j› | j› |› �}| jjd || j	|| j
| jd�}| ||¡ | j |¡ |S )Nz?You cannot have more than max_entries entries in this FieldListr   )rI   r*   r;   ÚidÚ_metaÚtranslations)r   r/   r%   r   Ú
short_namer   r[   r   Úbindr   ÚmetaÚ_translationsr6   rD   )r   r0   r1   r$   r*   r[   rY   r
   r
   r#   r.   ›   s*   ÿÿ
úzFieldList._add_entryc                 C   s   | j |d�S )z²
        Create a new entry with optional default data.

        Entries added in this way will *not* receive formdata however, and can
        only receive object data.
        ©r1   )r.   )r   r1   r
   r
   r#   Úappend_entry°   s   zFieldList.append_entryc                 C   s   | j  ¡ }|  jd8  _|S )z4Removes the last entry from the list and returns it.r   )r%   Úpopr   )r   Úentryr
   r
   r#   Ú	pop_entry¹   s   
zFieldList.pop_entryc                 C   ó
   t | jƒS r>   )r+   r%   ©r   r
   r
   r#   Ú__iter__¿   ó   
zFieldList.__iter__c                 C   rg   r>   )r/   r%   rh   r
   r
   r#   Ú__len__Â   rj   zFieldList.__len__c                 C   s
   | j | S r>   ©r%   )r   r$   r
   r
   r#   Ú__getitem__Å   rj   zFieldList.__getitem__c                 C   s   dd„ | j D ƒS )Nc                 S   s   g | ]}|j ‘qS r
   rb   )r?   Úfr
   r
   r#   Ú
<listcomp>Ê   s    z"FieldList.data.<locals>.<listcomp>rl   rh   r
   r
   r#   r1   È   s   zFieldList.data)NNr   Nr	   r
   )r
   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ú
ListWidgetÚwidgetr   r   r6   r)   rC   rR   r.   rc   rf   ri   rk   rm   Úpropertyr1   Ú__classcell__r
   r
   r!   r#   r      s,    ø#
	r   )
rF   Úwtforms.utilsr   r   r   Úcorer   r   Ú__all__r   r
   r
   r
   r#   Ú<module>   s    