o
    ^"<fM                  
   @   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
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ddf
ddZ!dd Z"dS )    )ValidationError)Form)BooleanFieldIntegerField)	ErrorList)HiddenInputNumberInput)cached_property)	html_safe)	mark_safe)gettext_lazy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 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__ L/var/www/html/kck/venv/lib/python3.10/site-packages/django/forms/formsets.pyr    "   s
   zManagementForm.__init__c                    s&   t   }|td |td |S Nr   )r   clean
setdefaultr   r   )r!   cleaned_datar$   r&   r'   r)   ,   s   
zManagementForm.clean)__name__
__module____qualname____doc__r    r)   __classcell__r&   r&   r$   r'   r      s    
r   c                   @   sz  e Zd ZdZeZdediZddddded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ed,d- Zd.d/ Z ed0d1 Z!d2d3 Z"d4d5 Z#d6d7 Z$d8d9 Z%d:d; Z&d<d= Z'd>d? Z(d@dA Z)dBdC Z*edDdE Z+dFdG Z,dHdI Z-dJdK Z.dS )Lr   z;
    A collection of instances of the same Form class.
    missing_management_formzManagementForm data is missing or has been tampered with. Missing fields: %(field_names)s. You may need to file a bug report if the issue persists.Nzid_%sc	                 C   s   |d up|d u| _ |p|  | _|| _|pi | _|pi | _|| _|p#i | _|| _d | _	d | _
i }	tt| jD ]}
|	t|
di  q7|d urL|	| |	| _d S )Ndefault_error_messages)is_boundget_default_prefixprefixauto_iddatafilesinitialform_kwargserror_class_errors_non_form_errorsreversedtype__mro__updategetattrerror_messages)r!   r7   r8   r6   r5   r9   r;   r:   rC   messagesclsr&   r&   r'   r    B   s    




zBaseFormSet.__init__c                 C   s   |   S Nas_tabler!   r&   r&   r'   __str__W   s   zBaseFormSet.__str__c                 C   
   t | jS )z5Yield the forms in the order they should be rendered.)iterformsrI   r&   r&   r'   __iter__Z      
zBaseFormSet.__iter__c                 C   s
   | j | S )zAReturn the form at the given index, based on the rendering order.rM   r!   indexr&   r&   r'   __getitem__^   rO   zBaseFormSet.__getitem__c                 C   rK   rF   )lenrM   rI   r&   r&   r'   __len__b   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&   rI   r&   r&   r'   __bool__e   s   zBaseFormSet.__bool__c                 C   sX   | j rt| j| j| jd}|  |S t| j| jt|  t| 	 t
| jt| jid}|S )z4Return the ManagementForm instance for this FormSet.)r6   r5   )r6   r5   r9   )r3   r   r7   r6   r5   
full_cleanr   total_form_countr   initial_form_countr   min_numr   max_numr!   formr&   r&   r'   management_forml   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   )r3   minr_   r+   r   absolute_maxrZ   maxr[   extrar\   )r!   initial_formstotal_formsr&   r&   r'   rY   {   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   )r3   r_   r+   r   r9   rT   )r!   rd   r&   r&   r'   rZ      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irI   r&   r'   
<listcomp>   s    z%BaseFormSet.forms.<locals>.<listcomp>)rangerY   rI   r&   rI   r'   rM      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.
        )r:   copyrQ   r&   r&   r'   rg      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)r6   r5   r;   use_required_attributer7   r8   r9   Tempty_permittedNr&   )r6   
add_prefixr;   r3   r7   r8   r9   
IndexErrorrZ   r[   rA   r^   
add_fields)r!   rj   r#   defaultsr^   r&   r&   r'   rf      s(   	


zBaseFormSet._construct_formc                 C   s   | j d|   S )z7Return a list of all the initial forms in this formset.NrM   rZ   rI   r&   r&   r'   rd         zBaseFormSet.initial_formsc                 C   s   | j |  d S )z5Return a list of all the extra forms in this formset.Nrt   rI   r&   r&   r'   extra_forms   ru   zBaseFormSet.extra_formsc                 C   s8   | j d| j| dddd| d }| |d  |S )N
__prefix__TF)r6   r5   ro   rn   r&   )r^   r6   rp   rg   rr   r]   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&   )r+   ri   r^   r&   r&   r'   rk      s    z,BaseFormSet.cleaned_data.<locals>.<listcomp>)is_validAttributeErrorr%   r,   rM   rI   r&   r&   r'   r+      s   zBaseFormSet.cleaned_datac                    sv      r js	g S t ds1g  _t jD ]\}}|  kr%| s%q |r0 j	| q fdd jD S )z:Return a list of forms that have been marked for deletion._deleted_form_indexesc                    s   g | ]} j | qS r&   rP   rh   rI   r&   r'   rk      s    z-BaseFormSet.deleted_forms.<locals>.<listcomp>)
rz   
can_deletehasattrr|   	enumeraterM   rZ   has_changed_should_delete_formappend)r!   rj   r^   r&   rI   r'   deleted_forms   s   

zBaseFormSet.deleted_formsc                    s      r jstd jj t dsKg  _t jD ]#\}}| 	 kr+|
 s+q jr4 |r4q 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'	_orderingc                 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   rP   rh   rI   r&   r'   rk     s    z-BaseFormSet.ordered_forms.<locals>.<listcomp>)rz   	can_orderr{   r%   r,   r~   r   r   rM   rZ   r   r}   r   r   r+   ORDERING_FIELD_NAMEsort)r!   rj   r^   r   r&   rI   r'   ordered_forms   s   
zBaseFormSet.ordered_formsc                 C   rV   )Nr^   r&   rE   r&   r&   r'   r4     s   zBaseFormSet.get_default_prefixc                 C   s   | j S rF   )ordering_widgetr   r&   r&   r'   get_ordering_widget  s   zBaseFormSet.get_ordering_widgetc                 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)r=   rX   rI   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)r<   rX   rI   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 rF   )rT   )ri   form_errorsr&   r&   r'   	<genexpr>6      z0BaseFormSet.total_error_count.<locals>.<genexpr>)rT   r   sumr   rI   r&   r&   r'   total_error_count3  s   
zBaseFormSet.total_error_countc                 C   s   |j tdS )z7Return whether or not the form was marked for deletion.F)r+   getDELETION_FIELD_NAMEr]   r&   r&   r'   r   8  s   zBaseFormSet._should_delete_formc                    s6    j sdS  j t fdd jD }|o   S )z1Return True if every form in self.forms is valid.Fc                    s$   g | ]} j r |s| qS r&   )r}   r   rz   ry   rI   r&   r'   rk   D  s    z(BaseFormSet.is_valid.<locals>.<listcomp>)r3   r   allrM   r   )r!   forms_validr&   rI   r'   rz   <  s   zBaseFormSet.is_validc              
      s  g  _    _d} jsdS  j s2t jd dd fdd jj	D idd} j
| t jD ]$\}}| sI|  krI|d	7 }|j	} jrU |rUq7 j 
| q7zL jrl  t j  jksu jjt  jkrttd
d j j dd jr  t j |  jk rt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   Nr1   field_namesz, c                 3   s    | ]	} j |V  qd S rF   )r_   rp   )ri   
field_namerI   r&   r'   r   Z  s
    

z)BaseFormSet.full_clean.<locals>.<genexpr>)paramscoder   zPlease submit at most %d form.zPlease submit at most %d forms.too_many_forms)r   zPlease submit at least %d form.z Please submit at least %d forms.too_few_forms)r<   r;   r=   r3   r_   rz   r   rC   joinr   r   r   rM   r   rZ   r}   r   validate_maxrY   rT   r   r\   r+   r   ra   r   validate_minr[   r)   
error_list)r!   empty_forms_counterrorrj   r^   r   er&   rI   r'   rX   J  sb   


zBaseFormSet.full_cleanc                 C   rV   )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&   rI   r&   r&   r'   r)     s   zBaseFormSet.cleanc                 C      t dd | D S )z5Return True if data in any form differs from initial.c                 s       | ]}|  V  qd S rF   )r   ry   r&   r&   r'   r     r   z*BaseFormSet.has_changed.<locals>.<genexpr>)anyrI   r&   r&   r'   r     s   zBaseFormSet.has_changedc                 C   s   |   }| jr/|dur!||k r!ttd|d d|  d|jt< nttdd|  d|jt< | jrF| js9||k rHt	tddd|jt
< dS dS dS )	z8A hook for adding extra fields on to each form instance.NOrderr   F)labelr9   r   r   )r   r   r   Delete)r   r   )rZ   r   r   _r   fieldsr   r}   can_delete_extrar   r   )r!   r^   rR   rZ   r&   r&   r'   rr     s"   zBaseFormSet.add_fieldsc                 C   s   d| j |f S )Nz%s-%s)r5   rQ   r&   r&   r'   rp     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   )rM   is_multipartrx   rI   r&   r&   r'   r     s   
zBaseFormSet.is_multipartc                 C   s   | j r	| j d jS | jjS r(   )rM   mediarx   rI   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   rF   rG   ry   r&   r&   r'   r     r   z'BaseFormSet.as_table.<locals>.<genexpr>
r   r   strr_   r!   rM   r&   r&   r'   rH     s   zBaseFormSet.as_tablec                 C   r   )z*Return this formset rendered as HTML <p>s.r   c                 s   r   rF   )as_pry   r&   r&   r'   r     r   z#BaseFormSet.as_p.<locals>.<genexpr>r   r   r   r&   r&   r'   r        zBaseFormSet.as_pc                 C   r   )z+Return this formset rendered as HTML <li>s.r   c                 s   r   rF   )as_ulry   r&   r&   r'   r     r   z$BaseFormSet.as_ul.<locals>.<genexpr>r   r   r   r&   r&   r'   r     r   zBaseFormSet.as_ul)/r,   r-   r.   r/   r   r   r   r2   r   r    rJ   rN   rS   rU   rW   r	   r_   rY   rZ   rM   rg   rf   propertyrd   rv   rx   r+   r   r   classmethodr4   r   r   r   r   r   rz   rX   r)   r   rr   rp   r   r   rH   r   r   r&   r&   r&   r'   r   5   sr    

	
	





$



7	

r   r   FNTc                 C   sf   |du rt }|du rt}|	du r|t }	||	krtd| ||||
|||	||d
}t| jd |f|S )z*Return a FormSet for the given form class.Nz5'absolute_max' must be greater or equal to 'max_num'.)
r^   rc   r   r}   r   r[   r\   ra   r   r   FormSet)DEFAULT_MIN_NUMDEFAULT_MAX_NUM
ValueErrorr?   r,   )r^   formsetrc   r   r}   r\   r   r[   r   ra   r   attrsr&   r&   r'   r     s,   r   c                 C   r   )z8Validate every formset and return True if all are valid.c                 S   s   g | ]}|  qS r&   )rz   )ri   r   r&   r&   r'   rk     s    zall_valid.<locals>.<listcomp>)r   )formsetsr&   r&   r'   r     ru   r   )#django.core.exceptionsr   django.formsr   django.forms.fieldsr   r   django.forms.utilsr   django.forms.widgetsr   r   django.utils.functionalr	   django.utils.htmlr
   django.utils.safestringr   django.utils.translationr   r   r   __all__r   r   r   r   r   r   r   r   r   r   r   r   r&   r&   r&   r'   <module>   s<       
!