
    WeIiG                     ^    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	dS )    N)unset_value   )widgets   )Field)UnboundField)	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.
    Nr   - c                 h    t                      j        ||fd|i| | j        rt          d          t	          |t
                    s
J d            || _        || _        || _        d| _	        |
                    dd          | _        || _        |j        
                    dd          | _        d S )	NdefaultRFieldList 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__s
            X/var/www/html/MCyber-Diagnostic/venv/lib/python3.11/site-packages/wtforms/fields/list.pyr   zFieldList.__init__$   s     	
FFGFvFFF< 	/   <
 
 	6 	65	6 	6 	6 +&&zz)R00# - 4 8 8c J J    c                    |rt          d          g | _        |t          u s|s-	 |                                 }n# t           $ r
 | j        }Y nw xY w|| _        |rt          t          |                     | j        |                              }| j	        r|d | j	                 }t          |          }|D ]B}	 t          |          }n# t          $ r
 t          }Y nw xY w|                     |||           Cn|D ]}|                     ||           t          | j                  | j        k     r4|                     |           t          | j                  | j        k     2d 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_datas           r%   processzFieldList.process@   s    	/  
 ;d$||~~ $ $ $|$   	4S!6!6ty(!K!KLLMMG 6!"4D$4"45JJE  A A+#E{{HH$ + + +*HHH+(%@@@@A ! 4 4(3333$,$"222OOH%%% $,$"2222222s!   : AA9C		CCc              #      K   t          |          dz   }|D ]e}|                    |          rN||d                             | j        d          d         }|                                rt          |          V  fd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   )r3   
startswithsplitr    isdigitint)r!   prefixr4   offsetks        r%   r-   zFieldList._extract_indicesc   s       Vq 	! 	!A||F## !fggJ$$T%:A>>qA99;; !a&&LLL		! 	!r&   c                 f   g | _         | j        D ]6}|                    |           | j                             |j                    7t	          d | j         D                       sg | _         t          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   | ]}|V  d S Nr   ).0xs     r%   	<genexpr>z%FieldList.validate.<locals>.<genexpr>   s"      **1******r&   r   )
errorsr)   validateappendany	itertoolschainr#   _run_validation_chainr3   )r!   formextra_validatorssubfieldrN   s        r%   rJ   zFieldList.validater   s       	0 	0Hd###Kx////**dk***** 	DK1ABB""4///4;1$$r&   c                    t          ||d           }	 t          |          }n# t          $ r t          g           }Y nw xY wt          j        |t          j        d                     }t          dt          fi           }g }t          | j	        |          D ]F\  }}	 |            }
|	|
_
        |                    |
d           |                    |
j
                   Gt          |||           d S )N_faker5   )getattrr/   r   rM   rN   repeattypeobjectzipr)   r5   populate_objrK   setattr)r!   objr.   valuesivalues
candidatesrT   outputfieldr5   fake_objs              r%   rZ   zFieldList.populate_obj   s    dD))	6llGG 	 	 	2hhGGG	 _Wi.>t.D.DEE
Wvi,,t|Z88 	) 	)KE4uwwH HMx000MM(-((((T6"""""s   # ??c                    | j         r't          | j                  | j         k     s
J d            |
| j        dz   }|| _        | j         | j         | }| j         | j         | }| j                            d || j	        || j
        | j                  }|                    ||           | j                            |           |S )Nz?You cannot have more than max_entries entries in this FieldListr   )rP   r.   r@   id_metatranslations)r   r3   r)   r   
short_namer   rd   r   bindr   meta_translationsr:   rK   )r!   r4   r5   r(   r.   rd   ra   s          r%   r2   zFieldList._add_entry   s     	M$'$5$58H$H$H$HL %I$H$H=Oa'E/;4?;E;;11%11"''<)+ ( 
 
 	h%%%E"""r&   c                 .    |                      |          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.
        r5   )r2   )r!   r5   s     r%   append_entryzFieldList.append_entry   s     D)))r&   c                 X    | 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 rE   )r/   r)   r!   s    r%   __iter__zFieldList.__iter__   s    DL!!!r&   c                 *    t          | j                  S rE   )r3   r)   rs   s    r%   __len__zFieldList.__len__   s    4<   r&   c                     | j         |         S rE   r)   )r!   r(   s     r%   __getitem__zFieldList.__getitem__   s    |E""r&   c                 $    d | j         D             S )Nc                     g | ]	}|j         
S r   rl   )rF   fs     r%   
<listcomp>z"FieldList.data.<locals>.<listcomp>   s    ---1---r&   rx   rs   s    r%   r5   zFieldList.data   s    ------r&   )NNr   Nr   r   )r   )__name__
__module____qualname____doc__r   
ListWidgetwidgetr   r   r:   r-   rJ   rZ   r2   rm   rq   rt   rv   ry   propertyr5   __classcell__)r$   s   @r%   r	   r	      sN        *  W!!F
 K K K K K K8 &1 !& !& !& !&F! ! !% % % %.# # #$ #'[    * !, * * * *  " " "! ! !# # # . . X. . . . .r&   r	   )
rM   wtforms.utilsr   r   r   corer   r   __all__r	   r   r&   r%   <module>r      s        % % % % % %                  
~. ~. ~. ~. ~. ~. ~. ~. ~. ~.r&   