
    +hF                     V    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
e      Z	y)    N   )widgets   )Field)UnboundField)unset_value)	FieldListc                        e Zd ZdZ ej
                         Z	 	 	 	 	 	 d fd	ZedfdZ	d Z
ddZd ZdedfdZefd	Zd
 Zd Zd Zd Zed        Z x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.
    Nc                 >   t        	|   ||fd|i| | j                  rt        d      t	        |t
              sJ d       || _        || _        || _        d| _	        |j                  dd      | _        || _        |j                  j                  dd      | _        y )	NdefaultRFieldList does not accept any filters. Instead, define them on the enclosed field.z(Field must be unbound, not a field class_prefix 	separator-)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__s
            F/var/www/html/venv/lib/python3.12/site-packages/wtforms/fields/list.pyr   zFieldList.__init__#   s     	
FGFvF<</  <
 	65	6 
 +&&zz)R0# - 4 4 8 8c J    c                    |rt        d      g | _        |t        u s|s	 | j                         }|| _        |r|t        t        | j                  | j                  |                  }| j                  r|d | j                   }t        |      }|D ]"  }	 t        |      }| j                  |||       $ n|D ]  }| j                  ||        t        | j                        | j                  k  r5| j                  |       t        | j                        | j                  k  r4y y # t         $ r | j                  }Y w xY w# t        $ r	 t        }Y w xY w)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_datas           r$   processzFieldList.process?   sK   / 
 ;d$||~  S!6!6tyy(!KLMG!"4D$4$45JE  A+#E{H (%@A ! 4(34 $,,$"2"22OOH% $,,$"2"22+  $||$ % +*H+s#   D& E&D?>D?EEc              #      K   t        |      dz   }|D ]T  }|j                  |      s||d j                  | j                  d      d   }|j	                         sHt        |       V yw)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   )r2   
startswithsplitr   isdigitint)r    prefixr3   offsetks        r$   r,   zFieldList._extract_indicesb   se      Vq 	!A||F#fgJ$$T%:%:A>qA99;a&L		!s   %A+2A+A+c                 x   g | _         | j                  D ]8  }|j                  |       | j                   j                  |j                          : t	        d | j                   D              sg | _         t        j                  | j                  |      }| 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              3       K   | ]  }|  y wN ).0xs     r$   	<genexpr>z%FieldList.validate.<locals>.<genexpr>   s     *1*s   r   )
errorsr(   validateappendany	itertoolschainr"   _run_validation_chainr2   )r    formextra_validatorssubfieldrN   s        r$   rJ   zFieldList.validateq   s       	0Hd#KKx/	0 *dkk**DK1AB""4/4;;1$$r%   c                    t        ||d       }	 t        |      }t        j                  |t        j
                  d             }t        dt        fi       }g }t        | j                  |      D ]@  \  }}	 |       }
|	|
_
        |j                  |
d       |j                  |
j                         B t        |||       y # t        $ r t        g       }Y w xY w)N_faker4   )getattrr.   r   rM   rN   repeattypeobjectzipr(   r4   populate_objrK   setattr)r    objr-   valuesivalues
candidatesrT   outputfieldr4   fake_objs              r$   rZ   zFieldList.populate_obj   s    dD)	6lG __Wi.>.>t.DE
Wvi,t||Z8 	)KE4wH HMx0MM(--(		) 	T6"  	2hG	s   B> >CCc                    | j                   r)t        | j                        | j                   k  sJ d       || j                  dz   }|| _        | j                   | j
                   | }| j                   | j
                   | }| j                  j                  d || j                  || j                  | j                        }|j                  ||       | j                  j                  |       |S )Nz?You cannot have more than max_entries entries in this FieldListr   )rP   r-   r?   id_metatranslations)r   r2   r(   r   
short_namer   rd   r   bindr   meta_translationsr9   rK   )r    r3   r4   r'   r-   rd   ra   s          r$   r1   zFieldList._add_entry   s      C$58H8H$H	ML	MH=OOa'E//"4??"3E7;y)%1""''<<))++ ( 
 	h%E"r%   c                 &    | j                  |      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.
        )r4   )r1   )r    r4   s     r$   append_entryzFieldList.append_entry   s     D))r%   c                 d    | j                   j                         }| xj                  dz  c_        |S )z4Removes the last entry from the list and returns it.r   )r(   popr   )r    entrys     r$   	pop_entryzFieldList.pop_entry   s'      "1r%   c                 ,    t        | j                        S rD   )r.   r(   r    s    r$   __iter__zFieldList.__iter__   s    DLL!!r%   c                 ,    t        | j                        S rD   )r2   r(   rr   s    r$   __len__zFieldList.__len__   s    4<<  r%   c                      | j                   |   S rD   )r(   )r    r'   s     r$   __getitem__zFieldList.__getitem__   s    ||E""r%   c                 T    | j                   D cg c]  }|j                   c}S c c}w rD   )r(   r4   )r    fs     r$   r4   zFieldList.data   s     $-1---s   %)NNr   Nr   rE   )rE   )__name__
__module____qualname____doc__r   
ListWidgetwidgetr   r   r9   r,   rJ   rZ   r1   rl   rp   rs   ru   rw   propertyr4   __classcell__)r#   s   @r$   r	   r	      s    *  W!F
 K8 &1 !&F!%.#$ #'[ * !, *"!# . .r%   r	   )
rM   r   r   corer   r   wtforms.utilsr   __all__r	   rE   r%   r$   <module>r      s'        %
~. ~.r%   