o
    uvXjG  ã                   @   sà   d dl mZ d dlmZ d dlmZ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mZ d
ZdZdZdZdZdZdZd ZdZG dd„ deƒZeG dd„ dƒƒZedddddddfdd„Z dd„ Z!dS )é    )ÚValidationError)ÚForm)ÚBooleanFieldÚIntegerField)Ú	ErrorList)ÚHiddenInput)Úcached_property)Ú	html_safe)Ú	mark_safe)ÚgettextÚngettext)ÚBaseFormSetÚformset_factoryÚ	all_validÚTOTAL_FORMSÚINITIAL_FORMSÚMIN_NUM_FORMSÚMAX_NUM_FORMSÚORDERÚDELETEiè  c                       s    e Zd ZdZ‡ fdd„Z‡  ZS )ÚManagementFormz±
    Keep track of how many form instances are displayed on the page. If adding
    new forms via JavaScript, you should increment the count field of this form
    as well.
    c                    sZ   t td�| jt< t td�| jt< t dtd�| jt< t dtd�| jt< tƒ j|i |¤Ž d S )N)ÚwidgetF)Úrequiredr   )	r   r   Úbase_fieldsÚTOTAL_FORM_COUNTÚINITIAL_FORM_COUNTÚMIN_NUM_FORM_COUNTÚMAX_NUM_FORM_COUNTÚsuperÚ__init__)ÚselfÚargsÚkwargs©Ú	__class__© úR/var/www/html/myproject/venv/lib/python3.10/site-packages/django/forms/formsets.pyr   "   s
   zManagementForm.__init__)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ú__classcell__r%   r%   r#   r&   r      s    r   c                   @   s\  e Zd ZdZdddddedfdd„Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
edd„ ƒZdd„ Zdd„ Zedd„ ƒZdd„ Zdd„ Zedd„ ƒZedd„ ƒZed d!„ ƒZed"d#„ ƒZed$d%„ ƒZed&d'„ ƒZed(d)„ ƒZd*d+„ Zed,d-„ ƒZd.d/„ Zd0d1„ Zd2d3„ Zd4d5„ Z d6d7„ Z!d8d9„ Z"d:d;„ Z#d<d=„ Z$d>d?„ Z%ed@dA„ ƒZ&dBdC„ Z'dDdE„ Z(dFdG„ Z)dS )Hr   z;
    A collection of instances of the same Form class.
    Nzid_%sc                 C   s`   |d up|d u| _ |p|  ¡ | _|| _|pi | _|pi | _|| _|p#i | _|| _d | _	d | _
d S ©N)Úis_boundÚget_default_prefixÚprefixÚauto_idÚdataÚfilesÚinitialÚform_kwargsÚerror_classÚ_errorsÚ_non_form_errors)r    r1   r2   r0   r/   r3   r5   r4   r%   r%   r&   r   2   s   



zBaseFormSet.__init__c                 C   s   |   ¡ S r,   ©Úas_table©r    r%   r%   r&   Ú__str__?   s   zBaseFormSet.__str__c                 C   ó
   t | jƒS )z5Yield the forms in the order they should be rendered.)ÚiterÚformsr:   r%   r%   r&   Ú__iter__B   ó   
zBaseFormSet.__iter__c                 C   s
   | j | S )zAReturn the form at the given index, based on the rendering order.©r>   ©r    Úindexr%   r%   r&   Ú__getitem__F   r@   zBaseFormSet.__getitem__c                 C   r<   r,   )Úlenr>   r:   r%   r%   r&   Ú__len__J   s   
zBaseFormSet.__len__c                 C   ó   dS )zt
        Return True since all formsets have a management form which is not
        included in the length.
        Tr%   r:   r%   r%   r&   Ú__bool__M   s   zBaseFormSet.__bool__c                 C   sh   | j rt| j| j| jd�}| ¡ sttdƒdd�‚|S t| j| jt|  	¡ t
|  ¡ t| jt| jid�}|S )z4Return the ManagementForm instance for this FormSet.)r0   r/   z8ManagementForm data is missing or has been tampered withÚmissing_management_form©Úcode)r0   r/   r3   )r-   r   r1   r0   r/   Úis_validr   Ú_r   Útotal_form_countr   Úinitial_form_countr   Úmin_numr   Úmax_num©r    Úformr%   r%   r&   Úmanagement_formT   s   þ
úüzBaseFormSet.management_formc                 C   sx   | j rt| jjt | jƒS |  ¡ }t|| jƒ| j	 }|| j
  kr(dkr)|}|S  || j
  kr4dkr:n |S | j
}|S )z1Return the total number of forms in this FormSet.r   )r-   ÚminrT   Úcleaned_datar   Úabsolute_maxrO   ÚmaxrP   ÚextrarQ   )r    Úinitial_formsÚtotal_formsr%   r%   r&   rN   g   s   üÿzBaseFormSet.total_form_countc                 C   s*   | j r	| jjt S | jrt| jƒnd}|S )z=Return the number of forms that are required in this FormSet.r   )r-   rT   rV   r   r3   rE   )r    rZ   r%   r%   r&   rO   z   s   zBaseFormSet.initial_form_countc                    s   ‡ fdd„t ˆ  ¡ ƒD ƒ}|S )z+Instantiate forms at first property access.c                    s$   g | ]}ˆ j |fi ˆ  |¡¤Ž‘qS r%   )Ú_construct_formÚget_form_kwargs©Ú.0Úir:   r%   r&   Ú
<listcomp>‡   s    ÿz%BaseFormSet.forms.<locals>.<listcomp>)ÚrangerN   ©r    r>   r%   r:   r&   r>   ƒ   s   

ÿzBaseFormSet.formsc                 C   s
   | j  ¡ S )z®
        Return additional keyword arguments for each individual formset form.

        index will be None if the form being constructed is a new empty
        form.
        )r4   ÚcopyrB   r%   r%   r&   r]   ‹   s   
zBaseFormSet.get_form_kwargsc                 K   s®   | j |  |¡| jddœ}| jr| j|d< | j|d< | jr3d|vr3z	| j| |d< W n	 ty2   Y nw ||  ¡ krB|| j	krBd|d< | 
|¡ | jd	i |¤Ž}|  ||¡ |S )
z;Instantiate and return the i-th form instance in a formset.F)r0   r/   r5   Úuse_required_attributer1   r2   r3   TÚempty_permittedNr%   )r0   Ú
add_prefixr5   r-   r1   r2   r3   Ú
IndexErrorrO   rP   ÚupdaterS   Ú
add_fields)r    r`   r"   ÚdefaultsrS   r%   r%   r&   r\   ”   s(   ù	

ÿ
zBaseFormSet._construct_formc                 C   s   | j d|  ¡ … S )z7Return a list of all the initial forms in this formset.N©r>   rO   r:   r%   r%   r&   rZ   °   ó   zBaseFormSet.initial_formsc                 C   s   | j |  ¡ d… S )z5Return a list of all the extra forms in this formset.Nrl   r:   r%   r%   r&   Úextra_formsµ   rm   zBaseFormSet.extra_formsc                 C   s8   | j d| j|  d¡dddœ|  d ¡¤Ž}|  |d ¡ |S )NÚ
__prefix__TF)r0   r/   rf   re   r%   )rS   r0   rg   r]   rj   rR   r%   r%   r&   Ú
empty_formº   s   üûzBaseFormSet.empty_formc                 C   s(   |   ¡ std| jj ƒ‚dd„ | jD ƒS )zX
        Return a list of form.cleaned_data dicts for every form in self.forms.
        z+'%s' object has no attribute 'cleaned_data'c                 S   s   g | ]}|j ‘qS r%   )rV   ©r_   rS   r%   r%   r&   ra   Í   s    z,BaseFormSet.cleaned_data.<locals>.<listcomp>)rL   ÚAttributeErrorr$   r'   r>   r:   r%   r%   r&   rV   Æ   s   zBaseFormSet.cleaned_datac                    s€   ˆ   ¡ rˆ js	g S tˆ dƒs6g ˆ _tdˆ  ¡ ƒD ]}ˆ j| }|ˆ  ¡ kr*| ¡ s*qˆ  	|¡r5ˆ j 
|¡ q‡ fdd„ˆ jD ƒS )z:Return a list of forms that have been marked for deletion.Ú_deleted_form_indexesr   c                    s   g | ]}ˆ j | ‘qS r%   rA   r^   r:   r%   r&   ra   ß   s    z-BaseFormSet.deleted_forms.<locals>.<listcomp>)rL   Ú
can_deleteÚhasattrrs   rb   rN   r>   rO   Úhas_changedÚ_should_delete_formÚappend)r    r`   rS   r%   r:   r&   Údeleted_formsÏ   s   


€zBaseFormSet.deleted_formsc                    s´   ˆ   ¡ rˆ jstdˆ jj ƒ‚tˆ dƒsPg ˆ _tdˆ  ¡ ƒD ]&}ˆ j	| }|ˆ  
¡ kr0| ¡ s0qˆ jr9ˆ  |¡r9qˆ j ||jt f¡ qdd„ }ˆ jj|d� ‡ fdd„ˆ jD ƒS )	z�
        Return a list of form in the order specified by the incoming data.
        Raise an AttributeError if ordering is not allowed.
        z,'%s' object has no attribute 'ordered_forms'Ú	_orderingr   c                 S   s   | d d u rdS d| d fS )Né   )r{   r   r   r%   )Úkr%   r%   r&   Úcompare_ordering_keyþ   s   z7BaseFormSet.ordered_forms.<locals>.compare_ordering_key)Úkeyc                    s   g | ]	}ˆ j |d   ‘qS )r   rA   r^   r:   r%   r&   ra     s    z-BaseFormSet.ordered_forms.<locals>.<listcomp>)rL   Ú	can_orderrr   r$   r'   ru   rz   rb   rN   r>   rO   rv   rt   rw   rx   rV   ÚORDERING_FIELD_NAMEÚsort)r    r`   rS   r}   r%   r:   r&   Úordered_formsá   s   

zBaseFormSet.ordered_formsc                 C   rG   )NrS   r%   )Úclsr%   r%   r&   r.     s   zBaseFormSet.get_default_prefixc                 C   ó   | j du r	|  ¡  | j S )z¹
        Return an ErrorList of errors that aren't associated with a particular
        form -- i.e., from formset.clean(). Return an empty ErrorList if there
        are none.
        N)r7   Ú
full_cleanr:   r%   r%   r&   Únon_form_errors  s   
zBaseFormSet.non_form_errorsc                 C   r„   )z:Return a list of form.errors for every form in self.forms.N)r6   r…   r:   r%   r%   r&   Úerrors  s   
zBaseFormSet.errorsc                 C   s    t |  ¡ ƒtdd„ | jD ƒƒ S )z<Return the number of errors across all forms in the formset.c                 s   s   � | ]}t |ƒV  qd S r,   )rE   )r_   Úform_errorsr%   r%   r&   Ú	<genexpr>  ó   € z0BaseFormSet.total_error_count.<locals>.<genexpr>)rE   r†   Úsumr‡   r:   r%   r%   r&   Útotal_error_count  s   
ÿzBaseFormSet.total_error_countc                 C   s   |j  td¡S )z7Return whether or not the form was marked for deletion.F)rV   ÚgetÚDELETION_FIELD_NAMErR   r%   r%   r&   rw   !  s   zBaseFormSet._should_delete_formc                 C   s^   | j sdS d}| j td|  ¡ ƒD ]}| j| }| jr!|  |¡r!q|| ¡ M }q|o.|  ¡  S )z1Return True if every form in self.forms is valid.FTr   )	r-   r‡   rb   rN   r>   rt   rw   rL   r†   )r    Úforms_validr`   rS   r%   r%   r&   rL   %  s   
zBaseFormSet.is_validc              
   C   sJ  g | _ |  ¡ | _d}| jsdS td|  ¡ ƒD ]'}| j| }| ¡ s+||  ¡ kr+|d7 }|j	}| j
r7|  |¡r7q| j  |¡ qzL| jrN|  ¡ t| jƒ | jksW| jjt | jkrettdd| jƒ| j dd�‚| jr„|  ¡ t| jƒ | | jk r„ttdd| jƒ| j dd�‚|  ¡  W dS  ty¤ } z|  |j¡| _W Y d}~dS d}~ww )	ze
        Clean all of self.data and populate self._errors and
        self._non_form_errors.
        r   Nr{   z Please submit %d or fewer forms.Útoo_many_formsrJ   zPlease submit %d or more forms.Útoo_few_forms)r6   r5   r7   r-   rb   rN   r>   rv   rO   r‡   rt   rw   rx   Úvalidate_maxrE   ry   rQ   rT   rV   r   rW   r   r   Úvalidate_minrP   ÚcleanÚ
error_list)r    Úempty_forms_countr`   rS   rˆ   Úer%   r%   r&   r…   7  sP   

þþýþþý€ÿzBaseFormSet.full_cleanc                 C   rG   )a  
        Hook for doing any extra formset-wide cleaning after Form.clean() has
        been called on every form. Any ValidationError raised by this method
        will not be associated with a particular form; it will be accessible
        via formset.non_form_errors()
        Nr%   r:   r%   r%   r&   r”   a  s   zBaseFormSet.cleanc                 C   s   t dd„ | D ƒƒS )z5Return True if data in any form differs from initial.c                 s   ó   � | ]}|  ¡ V  qd S r,   )rv   rq   r%   r%   r&   r‰   l  rŠ   z*BaseFormSet.has_changed.<locals>.<genexpr>)Úanyr:   r%   r%   r&   rv   j  s   zBaseFormSet.has_changedc                 C   sr   | j r'|dur||  ¡ k rttdƒ|d dd�|jt< nttdƒdd�|jt< | jr7ttdƒdd�|jt< dS dS )z8A hook for adding extra fields on to each form instance.NÚOrderr{   F)Úlabelr3   r   )r›   r   ÚDelete)	r   rO   r   rM   Úfieldsr€   rt   r   rŽ   )r    rS   rC   r%   r%   r&   rj   n  s   ÿzBaseFormSet.add_fieldsc                 C   s   d| j |f S )Nz%s-%s)r/   rB   r%   r%   r&   rg   y  s   zBaseFormSet.add_prefixc                 C   s   | j r
| j d  ¡ S | j ¡ S )zv
        Return True if the formset needs to be multipart, i.e. it
        has FileInput, or False otherwise.
        r   )r>   Úis_multipartrp   r:   r%   r%   r&   rž   |  s   
zBaseFormSet.is_multipartc                 C   s   | j r	| j d jS | jjS )Nr   )r>   Úmediarp   r:   r%   r%   r&   rŸ   †  s   zBaseFormSet.mediac                 C   ó*   d  dd„ | D ƒ¡}tt| jƒd | ƒS )zLReturn this formset rendered as HTML <tr>s -- excluding the <table></table>.ú c                 s   r˜   r,   r8   rq   r%   r%   r&   r‰   ”  rŠ   z'BaseFormSet.as_table.<locals>.<genexpr>Ú
©Újoinr
   ÚstrrT   rc   r%   r%   r&   r9   �  s   zBaseFormSet.as_tablec                 C   r    )z*Return this formset rendered as HTML <p>s.r¡   c                 s   r˜   r,   )Úas_prq   r%   r%   r&   r‰   ™  rŠ   z#BaseFormSet.as_p.<locals>.<genexpr>r¢   r£   rc   r%   r%   r&   r¦   —  ó   zBaseFormSet.as_pc                 C   r    )z+Return this formset rendered as HTML <li>s.r¡   c                 s   r˜   r,   )Úas_ulrq   r%   r%   r&   r‰   ž  rŠ   z$BaseFormSet.as_ul.<locals>.<genexpr>r¢   r£   rc   r%   r%   r&   r¨   œ  r§   zBaseFormSet.as_ul)*r'   r(   r)   r*   r   r   r;   r?   rD   rF   rH   r   rT   rN   rO   r>   r]   r\   ÚpropertyrZ   rn   rp   rV   ry   r‚   Úclassmethodr.   r†   r‡   rŒ   rw   rL   r…   r”   rv   rj   rg   rž   rŸ   r9   r¦   r¨   r%   r%   r%   r&   r   -   sb    
ÿ
	
	





%


*	

r   r{   FNc	              
   C   sL   |du rt }|du rt}|t }	| ||||||	||dœ	}
t| jd |f|
ƒS )z*Return a FormSet for the given form class.N)	rS   rY   r   rt   rP   rQ   rW   r“   r’   ÚFormSet)ÚDEFAULT_MIN_NUMÚDEFAULT_MAX_NUMÚtyper'   )rS   ÚformsetrY   r   rt   rQ   r’   rP   r“   rW   Úattrsr%   r%   r&   r   ¢  s    ÷r   c                 C   s   d}| D ]}||  ¡ M }q|S )z8Validate every formset and return True if all are valid.T)rL   )ÚformsetsÚvalidr¯   r%   r%   r&   r   ¼  s   r   )"Údjango.core.exceptionsr   Údjango.formsr   Údjango.forms.fieldsr   r   Údjango.forms.utilsr   Údjango.forms.widgetsr   Údjango.utils.functionalr   Údjango.utils.htmlr	   Údjango.utils.safestringr
   Údjango.utils.translationr   rM   r   Ú__all__r   r   r   r   r€   rŽ   r¬   r­   r   r   r   r   r%   r%   r%   r&   Ú<module>   s8      v
þ