U
     5øi÷M  ã                   @   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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)ÚBaseFormSetÚformset_factoryÚ	all_validZTOTAL_FORMSZINITIAL_FORMSZMIN_NUM_FORMSZMAX_NUM_FORMSZ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                    sV   t td�| jt< t td�| jt< t dtd�| jt< t dtd�| jt< tƒ j||Ž d S )N)ÚwidgetF)Úrequiredr   )	r   r
   Zbase_fieldsÚTOTAL_FORM_COUNTÚINITIAL_FORM_COUNTÚMIN_NUM_FORM_COUNTÚMAX_NUM_FORM_COUNTÚsuperÚ__init__)ÚselfÚargsÚkwargs©Ú	__class__© ú9/tmp/pip-unpacked-wheel-gcyiijx6/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                   @   s‚  e Zd ZdZeZeZdedƒi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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$e"d0d1„ ƒZ%d2d3„ Z&ed4d5„ ƒZ'd6d7„ Z(d8d9„ Z)d:d;„ Z*d<d=„ Z+d>d?„ Z,d@dA„ Z-dBdC„ Z.dDdE„ Z/dFdG„ Z0edHdI„ ƒZ1dJdK„ Z2dS )Lr   z;
    A collection of instances of the same Form class.
    Úmissing_management_formz“ManagementForm 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"django/forms/formsets/default.htmlzdjango/forms/formsets/p.htmlz django/forms/formsets/table.htmlzdjango/forms/formsets/ul.htmlNzid_%sc	                 C   s¢   |d k	p|d k	| _ |p|  ¡ | _|| _|p,i | _|p6i | _|| _|pFi | _|| _d | _	d | _
i }	tt| ƒjƒD ]}
|	 t|
di ƒ¡ qn|d k	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   r2   r3   r1   r0   r4   r6   r5   r>   ÚmessagesÚclsr!   r!   r"   r   E   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__Z   s    zBaseFormSet.__iter__c                 C   s
   | j | S )zAReturn the form at the given index, based on the rendering order.©rB   ©r   Úindexr!   r!   r"   Ú__getitem__^   s    zBaseFormSet.__getitem__c                 C   s
   t | jƒS ©N)ÚlenrB   rC   r!   r!   r"   Ú__len__b   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!   rC   r!   r!   r"   Ú__bool__e   s    zBaseFormSet.__bool__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.)r1   r0   Úrenderer)r1   r0   r4   rM   )r.   r   r2   r1   r0   rM   Ú
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rJdkrTn n|}n || j
  krjdkrtn n| j
}|S )z1Return the total number of forms in this FormSet.r   )r.   ÚminrU   r&   r   Úabsolute_maxrP   ÚmaxrQ   ÚextrarR   )r   Úinitial_formsZtotal_formsr!   r!   r"   rO   €   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.   rU   r&   r   r4   rJ   )r   rZ   r!   r!   r"   rP   “   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ˆ  |¡Ž‘qS r!   )Ú_construct_formÚget_form_kwargs©Ú.0ÚirC   r!   r"   Ú
<listcomp>    s   ÿz%BaseFormSet.forms.<locals>.<listcomp>)ÚrangerO   rC   r!   rC   r"   rB   œ   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.
        )r5   ÚcopyrF   r!   r!   r"   r\   ¥   s    zBaseFormSet.get_form_kwargsc                 K   s°   | j |  |¡| jd| jdœ}| jr6| j|d< | j|d< | jrld|krlz| j| |d< W n tk
rj   Y nX ||  	¡ krŠ|| j
krŠd|d< | |¡ | jf |Ž}|  ||¡ |S )z;Instantiate and return the i-th form instance in a formset.F)r1   r0   r6   Úuse_required_attributerM   r2   r3   r4   TÚempty_permitted)r1   Ú
add_prefixr6   rM   r.   r2   r3   r4   Ú
IndexErrorrP   rQ   r<   rT   Ú
add_fields)r   r_   r   ÚdefaultsrT   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©rB   rP   rC   r!   r!   r"   rZ   Ë   s    zBaseFormSet.initial_formsc                 C   s   | j |  ¡ d… S )z5Return a list of all the extra forms in this formset.Nri   rC   r!   r!   r"   Úextra_formsÐ   s    zBaseFormSet.extra_formsc                 C   s@   | j f | j|  d¡dddœ|  d ¡d| ji—Ž}|  |d ¡ |S )NZ
__prefix__TF)r1   r0   rd   rc   rM   )rT   r1   re   r\   rM   rg   rS   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&   ©r^   rT   r!   r!   r"   r`   é   s     z,BaseFormSet.cleaned_data.<locals>.<listcomp>)Úis_validÚAttributeErrorr    r'   rB   rC   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!   rE   r]   rC   r!   r"   r`   ú   s     z-BaseFormSet.deleted_forms.<locals>.<listcomp>)
rm   Ú
can_deleteÚhasattrro   Ú	enumeraterB   rP   Úhas_changedÚ_should_delete_formÚappend)r   r_   rT   r!   rC   r"   Údeleted_formsë   s    

zBaseFormSet.deleted_formsc                    sª   ˆ   ¡ rˆ jstdˆ jj ƒ‚tˆ dƒs–g ˆ _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 krdS d| d fS )Né   )rx   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   rE   r]   rC   r!   r"   r`     s     z-BaseFormSet.ordered_forms.<locals>.<listcomp>)rm   Ú	can_orderrn   r    r'   rq   rw   rr   rB   rP   rs   rp   rt   ru   r&   ÚORDERING_FIELD_NAMEÚsort)r   r_   rT   rz   r!   rC   r"   Úordered_formsü   s    
zBaseFormSet.ordered_formsc                 C   s   dS )NrT   r!   ©r@   r!   r!   r"   r/   !  s    zBaseFormSet.get_default_prefixc                 C   s   | j S rI   )Údeletion_widgetr€   r!   r!   r"   Úget_deletion_widget%  s    zBaseFormSet.get_deletion_widgetc                 C   s   | j S rI   )Úordering_widgetr€   r!   r!   r"   Úget_ordering_widget)  s    zBaseFormSet.get_ordering_widgetc                 C   s   | j dk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)r8   rN   rC   r!   r!   r"   Únon_form_errors-  s    
zBaseFormSet.non_form_errorsc                 C   s   | j dkr|  ¡  | j S )z:Return a list of form.errors for every form in self.forms.N)r7   rN   rC   r!   r!   r"   Úerrors7  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 rI   )rJ   )r^   Úform_errorsr!   r!   r"   Ú	<genexpr>A  s     z0BaseFormSet.total_error_count.<locals>.<genexpr>)rJ   r…   Úsumr†   rC   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)r&   ÚgetÚDELETION_FIELD_NAMErS   r!   r!   r"   rt   C  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!   )rp   rt   rm   rl   rC   r!   r"   r`   O  s    
ÿz(BaseFormSet.is_valid.<locals>.<listcomp>)r.   r†   ÚallrB   r…   )r   Zforms_validr!   rC   r"   rm   G  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²ˆ  |¡r²qvˆ j  |¡ qvzœˆ jràˆ  ¡ tˆ jƒ ˆ jksôˆ jjt ˆ jk�rttddˆ jƒˆ j dd�‚ˆ j�rRˆ  ¡ tˆ jƒ | ˆ jk �rRttddˆ jƒˆ j dd�‚ˆ  ¡  W n: tk
�r– } zˆ j|jdˆ jd�ˆ _W 5 d}~X Y nX dS )ze
        Clean all of self.data and populate self._errors and
        self._non_form_errors.
        Znonform)r6   rM   r   Nr,   Úfield_namesz, c                 3   s   | ]}ˆ j  |¡V  qd S rI   )rU   re   )r^   Ú
field_namerC   r!   r"   rˆ   e  s   ÿz)BaseFormSet.full_clean.<locals>.<genexpr>)ÚparamsÚcoderx   zPlease submit at most %d form.zPlease submit at most %d forms.Ztoo_many_forms)r‘   zPlease submit at least %d form.z Please submit at least %d forms.Ztoo_few_forms)r7   r6   rM   r8   r.   rU   rm   r   r>   Újoinr†   ru   rr   rB   rs   rP   rp   rt   Úvalidate_maxrO   rJ   rv   rR   r&   r   rW   r   Úvalidate_minrQ   r$   Z
error_list)r   Zempty_forms_countÚerrorr_   rT   r‡   Úer!   rC   r"   rN   U  sr    
 þÿø
ÿþ þþýÿ þþýý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!   rC   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 rI   )rs   rl   r!   r!   r"   rˆ   ›  s     z*BaseFormSet.has_changed.<locals>.<genexpr>)ÚanyrC   r!   r!   r"   rs   ™  s    zBaseFormSet.has_changedc                 C   s’   |   ¡ }| jr^|dk	rB||k rBttdƒ|d d|  ¡ d�|jt< nttdƒd|  ¡ d�|jt< | jrŽ| jsr||k rŽt	tdƒd|  
¡ d�|jt< dS )z8A hook for adding extra fields on to each form instance.NZOrderrx   F)Úlabelr4   r   r   )r˜   r   r   ZDelete)rP   r|   r   Ú_r„   Úfieldsr}   rp   Úcan_delete_extrar   r‚   rŒ   )r   rT   rG   rP   r!   r!   r"   rg   �  s(    üýýzBaseFormSet.add_fieldsc                 C   s   d| j |f S )Nz%s-%s)r0   rF   r!   r!   r"   re   ¶  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)rB   Úis_multipartrk   rC   r!   r!   r"   rœ   ¹  s    zBaseFormSet.is_multipartc                 C   s   | j r| j d jS | jjS d S r#   )rB   Úmediark   rC   r!   r!   r"   r�   Ã  s    zBaseFormSet.mediac                 C   s   d| iS )NÚformsetr!   rC   r!   r!   r"   Úget_contextÌ  s    zBaseFormSet.get_context)3r'   r(   r)   r*   r	   r�   r   rƒ   r™   r-   Ztemplate_nameZtemplate_name_pZtemplate_name_tableZtemplate_name_ulr   r   rD   rH   rK   rL   r   rU   rO   rP   rB   r\   r[   ÚpropertyrZ   rj   rk   r&   rv   r   Úclassmethodr/   r‚   r„   r…   r†   rŠ   rt   rm   rN   r$   rs   rg   re   rœ   r�   rŸ   r!   r!   r!   r"   r   4   s~    ÿÿ  þ

	
	





$




;	

r   rx   FNTc                 C   sn   |dkrt }|dkrt}|	dk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'.)rT   rY   r|   rp   r›   rQ   rR   rW   r”   r“   rM   ZFormSet)ÚDEFAULT_MIN_NUMÚDEFAULT_MAX_NUMÚ
ValueErrorr   r:   r'   )rT   rž   rY   r|   rp   rR   r“   rQ   r”   rW   r›   rM   Ú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!   )rm   )r^   rž   r!   r!   r"   r`   õ  s     zall_valid.<locals>.<listcomp>)r�   )Zformsetsr!   r!   r"   r   ò  s    r   )#Zdjango.core.exceptionsr   Zdjango.formsr   Zdjango.forms.fieldsr   r   Zdjango.forms.renderersr   Zdjango.forms.utilsr   r   Zdjango.forms.widgetsr	   r
   r   Zdjango.utils.functionalr   Zdjango.utils.translationr   r™   r   Ú__all__r   r   r   r   r}   rŒ   r¢   r£   r   r   r   r   r!   r!   r!   r"   Ú<module>   s@           ý
"