o
    {,hZS                     @   s   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
mZ d dlmZmZmZ d dlmZ d dlmZ d d	l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G dd deZ e ddddddddddfddZ!dd Z"dS )    )ValidationError)BooleanFieldIntegerField)Form)get_default_renderer)	ErrorListRenderableFormMixin)CheckboxInputHiddenInputNumberInput)cached_property)gettext_lazy)ngettext_lazy)BaseFormSetformset_factory	all_validTOTAL_FORMSINITIAL_FORMSMIN_NUM_FORMSMAX_NUM_FORMSORDERDELETEi  c                       sL   e Zd ZdZeedZeedZededZede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.
    )widgetF)requiredr   c                    s&   t   }|td |td |S Nr   )superclean
setdefaultTOTAL_FORM_COUNTINITIAL_FORM_COUNT)selfcleaned_data	__class__ K/var/www/html/rh/venv/lib/python3.10/site-packages/django/forms/formsets.pyr   +   s   
zManagementForm.clean)__name__
__module____qualname____doc__r   r
   r   r   r   r   r   __classcell__r%   r%   r#   r&   r      s    

r   c                   @   s  e Zd ZdZeZeZede	ddde	ddddZ
d	Zd
ZdZd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/d0 Z"ed1d2 Z#e$d3d4 Z%e$d5d6 Z&e$d7d8 Z'd9d: Z(ed;d< Z)d=d> Z*d?d@ Z+dAdB Z,dCdD Z-dEdF Z.dGdH Z/dIdJ Z0dKdL Z1dMdN Z2edOdP Z3edQdR Z4dSdT Z5dS )Ur   z;
    A collection of instances of the same Form class.
    zManagementForm 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.z#Please submit at most %(num)d form.z$Please submit at most %(num)d forms.numz$Please submit at least %(num)d form.z%Please submit at least %(num)d forms.)missing_management_formtoo_many_formstoo_few_formszdjango/forms/formsets/div.htmlzdjango/forms/formsets/p.htmlz django/forms/formsets/table.htmlzdjango/forms/formsets/ul.htmlNzid_%sc	                 C   s   |d up|d u| _ |p|  | _|| _|pi | _|pi | _|| _|p#i | _|| _d | _	d | _
| j| _| jp7t | _i }	tt| jD ]}
|	t|
di  qB|d urW|	| |	| _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rendererform_rendererr   reversedtype__mro__updategetattrerror_messages)r!   r5   r6   r4   r3   r7   r9   r8   rC   messagesclsr%   r%   r&   __init__Q   s$   




zBaseFormSet.__init__c                 C   
   t | jS )z5Yield the forms in the order they should be rendered.)iterformsr!   r%   r%   r&   __iter__p      
zBaseFormSet.__iter__c                 C   s
   | j | S )zAReturn the form at the given index, based on the rendering order.rI   r!   indexr%   r%   r&   __getitem__t   rL   zBaseFormSet.__getitem__c                 C   rG   N)lenrI   rJ   r%   r%   r&   __len__x   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%   rJ   r%   r%   r&   __bool__{   s   zBaseFormSet.__bool__c                 C   sN   | j d u rd}n| jo| j otdd | j D  }d| jj| j||  f S )NUnknownc                 s   s    | ]}|V  qd S rQ   r%   .0form_errorsr%   r%   r&   	<genexpr>   s    z'BaseFormSet.__repr__.<locals>.<genexpr>z&<%s: bound=%s valid=%s total_forms=%s>)r:   r1   r;   anyr$   r)   total_form_count)r!   is_validr%   r%   r&   __repr__   s   
zBaseFormSet.__repr__c                 C   s`   | j rt| j| j| j| jd}|  |S t| j| jt|  t	| 
 t| jt| ji| jd}|S )z4Return the ManagementForm instance for this FormSet.)r4   r3   r<   )r4   r3   r7   r<   )r1   r   r5   r4   r3   r<   
full_cleanr   r\   r    initial_form_countMIN_NUM_FORM_COUNTmin_numMAX_NUM_FORM_COUNTmax_numr!   formr%   r%   r&   management_form   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   )r1   minrg   r"   r   absolute_maxr`   maxrb   extrard   )r!   initial_formstotal_formsr%   r%   r&   r\      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   )r1   rg   r"   r    r7   rR   )r!   rl   r%   r%   r&   r`      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rX   irJ   r%   r&   
<listcomp>   s    z%BaseFormSet.forms.<locals>.<listcomp>)ranger\   rJ   r%   rJ   r&   rI      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.
        )r8   copyrN   r%   r%   r&   ro      s   
zBaseFormSet.get_form_kwargsc                 K   s   | j | || jd| jd}| jr| j|d< | j|d< | jr5d|vr5z	| j| |d< W n	 ty4   Y nw || 	 krD|| j
krDd|d< || | jd	i |}| || |S )
z;Instantiate and return the i-th form instance in a formset.F)r4   r3   r9   use_required_attributer<   r5   r6   r7   Tempty_permittedNr%   )r4   
add_prefixr9   r=   r1   r5   r6   r7   
IndexErrorr`   rb   rA   rf   
add_fields)r!   rq   kwargsdefaultsrf   r%   r%   r&   rn      s*   



zBaseFormSet._construct_formc                 C   s   | j d|   S )z7Return a list of all the initial forms in this formset.NrI   r`   rJ   r%   r%   r&   rl         zBaseFormSet.initial_formsc                 C   s   | j |  d S )z5Return a list of all the extra forms in this formset.Nr|   rJ   r%   r%   r&   extra_forms   r}   zBaseFormSet.extra_formsc                 C   sH   i |  d | j| ddd| jd}| jdi |}| |d  |S )N
__prefix__TF)r4   r3   rv   ru   r<   r%   )ro   r4   rw   r=   rf   ry   )r!   r8   rf   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"   rX   rf   r%   r%   r&   rr     s    z,BaseFormSet.cleaned_data.<locals>.<listcomp>)r]   AttributeErrorr$   r'   rI   rJ   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%   rM   rp   rJ   r%   r&   rr   *  s    z-BaseFormSet.deleted_forms.<locals>.<listcomp>)
r]   
can_deletehasattrr   	enumeraterI   r`   has_changed_should_delete_formappend)r!   rq   rf   r%   rJ   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_keyJ  s   z7BaseFormSet.ordered_forms.<locals>.compare_ordering_key)keyc                    s   g | ]	} j |d   qS )r   rM   rp   rJ   r%   r&   rr   R  s    z-BaseFormSet.ordered_forms.<locals>.<listcomp>)r]   	can_orderr   r$   r'   r   r   r   rI   r`   r   r   r   r   r"   ORDERING_FIELD_NAMEsort)r!   rq   rf   r   r%   rJ   r&   ordered_forms,  s   

zBaseFormSet.ordered_formsc                 C   rT   )Nrf   r%   rE   r%   r%   r&   r2   T  s   zBaseFormSet.get_default_prefixc                 C      | j S rQ   )deletion_widgetr   r%   r%   r&   get_deletion_widgetX     zBaseFormSet.get_deletion_widgetc                 C   r   rQ   )ordering_widgetr   r%   r%   r&   get_ordering_widget\  r   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;   r_   rJ   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:   r_   rJ   r%   r%   r&   errorsj  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 rQ   )rR   rW   r%   r%   r&   rZ   s  s    
z0BaseFormSet.total_error_count.<locals>.<genexpr>)rR   r   sumr   rJ   r%   r%   r&   total_error_countq  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_NAMEre   r%   r%   r&   r   w  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   r]   r   rJ   r%   r&   rr     s    z(BaseFormSet.is_valid.<locals>.<listcomp>)r1   r   allrI   r   )r!   forms_validr%   rJ   r&   r]   {  s   
zBaseFormSet.is_validc              
      s  g  _  jd jd _d} jsdS  j s6t jd dd	 fdd	 jj
D idd
} j| t jD ]$\}}| sM|  krM|d7 }|j
} jrY |rYq; j | q;zL jrp  t j  jksy jjt  jkrt jd d ji dd jr  t j |  jk rt jd d ji dd   W dS  ty } z j|jd jd _W Y d}~dS d}~ww )ze
        Clean all of self.data and populate self._errors and
        self._non_form_errors.
        nonform)r9   r<   r   Nr-   field_namesz, c                 3   s    | ]	} j |V  qd S rQ   )rg   rw   )rX   
field_namerJ   r%   r&   rZ     s
    

z)BaseFormSet.full_clean.<locals>.<genexpr>)paramscoder   r.   r,   )r   r/   )r:   r9   r<   r;   r1   rg   r]   r   rC   joinr   r   r   rI   r   r`   r   r   validate_maxr\   rR   r   rd   r"   r   ri   validate_minrb   r   
error_list)r!   empty_forms_counterrorrq   rf   rY   er%   rJ   r&   r_     sv   

zBaseFormSet.full_cleanc                 C   rT   )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%   rJ   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   s    | ]}|  V  qd S rQ   )r   r   r%   r%   r&   rZ     s    z*BaseFormSet.has_changed.<locals>.<genexpr>)r[   rJ   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rM| js=|durO||k rQt	tdd| 
 d|jt< dS dS dS dS )z8A hook for adding extra fields on to each form instance.NOrderr   F)labelr7   r   r   )r   r   r   Delete)r`   r   r   _r   fieldsr   r   can_delete_extrar   r   r   )r!   rf   rO   r`   r%   r%   r&   ry     s2   zBaseFormSet.add_fieldsc                 C   s   d| j |f S )Nz%s-%s)r3   rN   r%   r%   r&   rw     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   )rI   is_multipartr   rJ   r%   r%   r&   r     s   
zBaseFormSet.is_multipartc                 C   s   | j r	| j d jS | jjS r   )rI   mediar   rJ   r%   r%   r&   r     s   zBaseFormSet.mediac                 C   s   | j jS rQ   )r<   formset_template_namerJ   r%   r%   r&   template_name  s   zBaseFormSet.template_namec                 C   s   d| iS )Nformsetr%   rJ   r%   r%   r&   get_context  s   zBaseFormSet.get_context)6r'   r(   r)   r*   r	   r   r   r   r   r   r0   template_name_divtemplate_name_ptemplate_name_tabletemplate_name_ulr   rF   rK   rP   rS   rU   r^   r   rg   r\   r`   rI   ro   rn   propertyrl   r~   r   r"   r   r   classmethodr2   r   r   r   r   r   r   r]   r_   r   r   ry   rw   r   r   r   r   r%   r%   r%   r&   r   4   s    

	
	






'




D	


r   r   FNTc                 C   s   |du rt }|du rt}|	du r|t }	||	krtd| ||||
|||	|||d}| j}|dr7|d }n|d }t||f|S )z*Return a FormSet for the given form class.Nz5'absolute_max' must be greater or equal to 'max_num'.)rf   rk   r   r   r   rb   rd   ri   r   r   r<   r   SetFormSet)DEFAULT_MIN_NUMDEFAULT_MAX_NUM
ValueErrorr'   endswithr?   )rf   r   rk   r   r   rd   r   rb   r   ri   r   r<   attrs	form_nameformset_namer%   r%   r&   r     s2   

r   c                 C   r   )z8Validate every formset and return True if all are valid.c                 S   s   g | ]}|  qS r%   )r]   )rX   r   r%   r%   r&   rr   H  s    zall_valid.<locals>.<listcomp>)r   )formsetsr%   r%   r&   r   E  r}   r   )#django.core.exceptionsr   django.forms.fieldsr   r   django.forms.formsr   django.forms.renderersr   django.forms.utilsr   r   django.forms.widgetsr	   r
   r   django.utils.functionalr   django.utils.translationr   r   r   __all__r   r    ra   rc   r   r   r   r   r   r   r   r   r%   r%   r%   r&   <module>   sH       g
/