a
    QhR                     @   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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)Form)BooleanFieldIntegerField)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_FORMSZORDERZDELETEi  c                       sP   e Zd 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.
    zdjango/forms/div.html)widgetF)requiredr   c                    s&   t   }|td |td |S Nr   )superclean
setdefaultTOTAL_FORM_COUNTINITIAL_FORM_COUNT)selfcleaned_data	__class__ N/var/www/sistema_ama/venv/lib/python3.9/site-packages/django/forms/formsets.pyr   -   s    
zManagementForm.clean)__name__
__module____qualname____doc__template_namer   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 | _|p6i | _|| _|pFi | _|| _d | _	d | _
i }	tt| jD ]}
|	t|
di  qn|d ur|	| |	| _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   r4   r5   r3   r2   r6   r8   r7   r@   messagesclsr#   r#   r$   __init__S   s     



zBaseFormSet.__init__c                 C   s
   t | jS )z5Yield the forms in the order they should be rendered.)iterformsr   r#   r#   r$   __iter__p   s    zBaseFormSet.__iter__c                 C   s
   | j | S )zAReturn the form at the given index, based on the rendering order.rE   r   indexr#   r#   r$   __getitem__t   s    zBaseFormSet.__getitem__c                 C   s
   t | jS N)lenrE   rF   r#   r#   r$   __len__x   s    zBaseFormSet.__len__c                 C   s   dS )zt
        Return True since all formsets have a management form which is not
        included in the length.
        Tr#   rF   r#   r#   r$   __bool__{   s    zBaseFormSet.__bool__c                 C   sN   | j d u rd}n$| jo2| j o2tdd | j D  }d| jj| j||  f S )NUnknownc                 s   s   | ]
}|V  qd S rL   r#   .0form_errorsr#   r#   r$   	<genexpr>       z'BaseFormSet.__repr__.<locals>.<genexpr>z&<%s: bound=%s valid=%s total_forms=%s>)r9   r0   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}|  n2t| j| jt|  t	| 
 t| jt| ji| jd}|S )z4Return the ManagementForm instance for this FormSet.)r3   r2   renderer)r3   r2   r6   rZ   )r0   r   r4   r3   r2   rZ   
full_cleanr   rW   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rJdkrTn n|}n || j
  krjdkrtn n| j
}|S )z1Return the total number of forms in this FormSet.r   )r0   minrc   r    r   absolute_maxr\   maxr^   extrar`   )r   initial_formsZtotal_formsr#   r#   r$   rW      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   )r0   rc   r    r   r6   rM   )r   rh   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rR   irF   r#   r$   
<listcomp>   s   z%BaseFormSet.forms.<locals>.<listcomp>)rangerW   rF   r#   rF   r$   rE      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.
        )r7   copyrI   r#   r#   r$   rj      s    zBaseFormSet.get_form_kwargsc                 K   s   | j | || jd| jd}| jr6| j|d< | j|d< | jrjd|vrjz| j| |d< W n tyh   Y n0 || 	 kr|| j
krd|d< || | jf i |}| || |S )z;Instantiate and return the i-th form instance in a formset.F)r3   r2   r8   use_required_attributerZ   r4   r5   r6   Tempty_permitted)r3   
add_prefixr8   rZ   r0   r4   r5   r6   
IndexErrorr\   r^   r>   rb   
add_fields)r   rl   kwargsdefaultsrb   r#   r#   r$   ri      s(    



zBaseFormSet._construct_formc                 C   s   | j d|   S )z7Return a list of all the initial forms in this formset.NrE   r\   rF   r#   r#   r$   rh      s    zBaseFormSet.initial_formsc                 C   s   | j |  d S )z5Return a list of all the extra forms in this formset.Nrw   rF   r#   r#   r$   extra_forms   s    zBaseFormSet.extra_formsc                 C   sH   i |  d | j| ddd| jd}| jf i |}| |d  |S )NZ
__prefix__TF)r3   r2   rq   rp   rZ   )rj   r3   rr   rZ   rb   rt   )r   r7   rb   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    rR   rb   r#   r#   r$   rm     rU   z,BaseFormSet.cleaned_data.<locals>.<listcomp>)rX   AttributeErrorr"   r%   rE   rF   r#   r#   r$   r      s
    
zBaseFormSet.cleaned_datac                    sv      r jsg S t dsbg  _t jD ]4\}}|  krJ| sJq, |r, 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#   rH   rk   rF   r#   r$   rm   *  rU   z-BaseFormSet.deleted_forms.<locals>.<listcomp>)
rX   
can_deletehasattrr|   	enumeraterE   r\   has_changed_should_delete_formappend)r   rl   rb   r#   rF   r$   deleted_forms  s    

zBaseFormSet.deleted_formsc                    s      r jstd jj t dsg  _t jD ]F\}}| 	 krV|
 sVq8 jrh |rhq8 j||jt f q8d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   rH   rk   rF   r#   r$   rm   R  rU   z-BaseFormSet.ordered_forms.<locals>.<listcomp>)rX   	can_orderr{   r"   r%   r~   r   r   rE   r\   r   r}   r   r   r    ORDERING_FIELD_NAMEsort)r   rl   rb   r   r#   rF   r$   ordered_forms,  s    

zBaseFormSet.ordered_formsc                 C   s   dS )Nrb   r#   rB   r#   r#   r$   r1   T  s    zBaseFormSet.get_default_prefixc                 C   s   | j S rL   )deletion_widgetr   r#   r#   r$   get_deletion_widgetX  s    zBaseFormSet.get_deletion_widgetc                 C   s   | j S rL   )ordering_widgetr   r#   r#   r$   get_ordering_widget\  s    zBaseFormSet.get_ordering_widgetc                 C   s   | 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[   rF   r#   r#   r$   non_form_errors`  s    
zBaseFormSet.non_form_errorsc                 C   s   | j du r|   | j S )z:Return a list of form.errors for every form in self.forms.N)r9   r[   rF   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 rL   )rM   rQ   r#   r#   r$   rT   s  s   z0BaseFormSet.total_error_count.<locals>.<genexpr>)rM   r   sumr   rF   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_NAMEra   r#   r#   r$   r   w  s    zBaseFormSet._should_delete_formc                    s6    j s
dS  j t fdd jD }|o4   S )z1Return True if every form in self.forms is valid.Fc                    s$   g | ]} j r |s| qS r#   )r}   r   rX   rz   rF   r#   r$   rm     s   z(BaseFormSet.is_valid.<locals>.<listcomp>)r0   r   allrE   r   )r   Zforms_validr#   rF   r$   rX   {  s    
zBaseFormSet.is_validc              
      s  g  _  jd jd _d} js&dS  j slt jd dd	 fdd	 jj
D idd
} j| t jD ]H\}}| s|  kr|d7 }|j
} jr |rqv j | qvz jr  t j  jks jjt  jkrt jd d ji dd jrR  t j |  jk rRt jd d ji dd   W n< ty } z" j|jd jd _W Y d}~n
d}~0 0 dS )ze
        Clean all of self.data and populate self._errors and
        self._non_form_errors.
        Znonform)r8   rZ   r   Nr,   field_namesz, c                 3   s   | ]} j |V  qd S rL   )rc   rr   )rR   
field_namerF   r#   r$   rT     s   z)BaseFormSet.full_clean.<locals>.<genexpr>)paramscoder   r-   r+   )r   r.   )r9   r8   rZ   r:   r0   rc   rX   r   r@   joinr   r   r   rE   r   r\   r}   r   validate_maxrW   rM   r   r`   r    r   re   validate_minr^   r   
error_list)r   Zempty_forms_counterrorrl   rb   rS   er#   rF   r$   r[     sx    

zBaseFormSet.full_cleanc                 C   s   dS )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#   rF   r#   r#   r$   r     s    zBaseFormSet.cleanc                 C   s   t dd | D S )z5Return True if data in any form differs from initial.c                 s   s   | ]}|  V  qd S rL   )r   rz   r#   r#   r$   rT     rU   z*BaseFormSet.has_changed.<locals>.<genexpr>)rV   rF   r#   r#   r$   r     s    zBaseFormSet.has_changedc                 C   s   |   }| jr^|durB||k rBttd|d d|  d|jt< nttdd|  d|jt< | jr| jsz|dur||k rt	tdd| 
 d|jt< dS )z8A hook for adding extra fields on to each form instance.NZOrderr   F)labelr6   r   r   )r   r   r   Delete)r\   r   r   _r   fieldsr   r}   can_delete_extrar   r   r   )r   rb   rJ   r\   r#   r#   r$   rt     s4    zBaseFormSet.add_fieldsc                 C   s   d| j |f S )Nz%s-%s)r2   rI   r#   r#   r$   rr     s    zBaseFormSet.add_prefixc                 C   s"   | j r| j d  S | j S dS )zv
        Return True if the formset needs to be multipart, i.e. it
        has FileInput, or False otherwise.
        r   N)rE   is_multipartry   rF   r#   r#   r$   r     s    zBaseFormSet.is_multipartc                 C   s   | j r| j d jS | jjS d S r   )rE   mediary   rF   r#   r#   r$   r     s    zBaseFormSet.mediac                 C   s   | j jS rL   )rZ   formset_template_namerF   r#   r#   r$   r)     s    zBaseFormSet.template_namec                 C   s   d| iS )Nformsetr#   rF   r#   r#   r$   get_context  s    zBaseFormSet.get_context)6r%   r&   r'   r(   r	   r   r   r   r   r   r/   template_name_divtemplate_name_ptemplate_name_tabletemplate_name_ulr   rC   rG   rK   rN   rO   rY   r   rc   rW   r\   rE   rj   ri   propertyrh   rx   ry   r    r   r   classmethodr1   r   r   r   r   r   r   rX   r[   r   r   rt   rr   r   r   r)   r   r#   r#   r#   r$   r   6   s   

	
	






'




D	


r   r   FNTc                 C   sn   |du rt }|du rt}|	du r(|t }	||	kr8td| ||||
|||	|||pTt 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'.)rb   rg   r   r}   r   r^   r`   re   r   r   rZ   ZFormSet)DEFAULT_MIN_NUMDEFAULT_MAX_NUM
ValueErrorr   r<   r%   )rb   r   rg   r   r}   r`   r   r^   r   re   r   rZ   attrsr#   r#   r$   r     s*    r   c                 C   s   t dd | D S )z8Validate every formset and return True if all are valid.c                 S   s   g | ]}|  qS r#   )rX   )rR   r   r#   r#   r$   rm   C  rU   zall_valid.<locals>.<listcomp>)r   )Zformsetsr#   r#   r$   r   @  s    r   )#django.core.exceptionsr   Zdjango.formsr   django.forms.fieldsr   r   django.forms.renderersr   django.forms.utilsr   r   django.forms.widgetsr	   r
   r   django.utils.functionalr   django.utils.translationr   r   r   __all__r   r   r]   r_   r   r   r   r   r   r   r   r   r#   r#   r#   r$   <module>   sF      e
*