U
    R+|j¯R  ã                   @   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ORDERÚ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__© ú]c:\users\snmko\desktop\web_content\django_cbt\venv\Lib\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.
    z“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#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 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    r5   r6   r4   r3   r7   r9   r8   rA   Ú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.©rF   ©r    Úindexr$   r$   r%   Ú__getitem__t   s    zBaseFormSet.__getitem__c                 C   s
   t | jƒS ©N)ÚlenrF   rG   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$   rG   r$   r$   r%   Ú__bool__{   s    zBaseFormSet.__bool__c                 C   sN   | j d k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 rM   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�}| ¡  n2t| j| jt|  ¡ t	|  
¡ t| jt| ji| jd�}|S )z4Return the ManagementForm instance for this FormSet.)r4   r3   Úrenderer)r4   r3   r7   rZ   )r1   r   r5   r4   r3   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   )r1   Ú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   )r1   rc   r!   r   r7   rN   )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ˆ  |¡Ž‘qS r$   )Ú_construct_formÚget_form_kwargs©rS   ÚirG   r$   r%   Ú
<listcomp>Í   s   ÿz%BaseFormSet.forms.<locals>.<listcomp>)ÚrangerW   rG   r$   rG   r%   rF   É   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   ÚcopyrJ   r$   r$   r%   rj   Ò   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)r4   r3   r9   Úuse_required_attributerZ   r5   r6   r7   TÚempty_permitted)r4   Ú
add_prefixr9   rZ   r1   r5   r6   r7   Ú
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©rF   r\   rG   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   rG   r$   r$   r%   Úextra_formsý   s    zBaseFormSet.extra_formsc                 C   s@   |   d ¡| j|  d¡dd| jdœ–}| jf |Ž}|  |d ¡ |S )NZ
__prefix__TF)r4   r3   rq   rp   rZ   )rj   r4   rr   rZ   rb   rt   )r    r8   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!   ©rS   rb   r$   r$   r%   rm     s     z,BaseFormSet.cleaned_data.<locals>.<listcomp>)rX   ÚAttributeErrorr#   r&   rF   rG   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$   rI   rk   rG   r$   r%   rm   *  s     z-BaseFormSet.deleted_forms.<locals>.<listcomp>)
rX   Ú
can_deleteÚhasattrr|   Ú	enumeraterF   r\   Úhas_changedÚ_should_delete_formÚappend)r    rl   rb   r$   rG   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é   )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   rI   rk   rG   r$   r%   rm   R  s     z-BaseFormSet.ordered_forms.<locals>.<listcomp>)rX   Ú	can_orderr{   r#   r&   r~   r„   r   rF   r\   r€   r}   r�   r‚   r!   ÚORDERING_FIELD_NAMEÚsort)r    rl   rb   r‡   r$   rG   r%   Úordered_forms,  s    
ÿ
zBaseFormSet.ordered_formsc                 C   s   dS )Nrb   r$   ©rC   r$   r$   r%   r2   T  s    zBaseFormSet.get_default_prefixc                 C   s   | j S rM   )Údeletion_widgetr�   r$   r$   r%   Úget_deletion_widgetX  s    zBaseFormSet.get_deletion_widgetc                 C   s   | j S rM   )Ú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)r;   r[   rG   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)r:   r[   rG   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 rM   )rN   rR   r$   r$   r%   rU   s  s    z0BaseFormSet.total_error_count.<locals>.<genexpr>)rN   r’   Úsumr“   rG   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   rG   r$   r%   rm   „  s    
þz(BaseFormSet.is_valid.<locals>.<listcomp>)r1   r“   ÚallrF   r’   )r    Zforms_validr$   rG   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²ˆ  |¡r²qvˆ 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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)r9   rZ   r   Nr-   Úfield_namesz, c                 3   s   | ]}ˆ j  |¡V  qd S rM   )rc   rr   )rS   Ú
field_namerG   r$   r%   rU   ž  s   ÿz)BaseFormSet.full_clean.<locals>.<genexpr>)ÚparamsÚcoder…   r.   r,   )rœ   r/   )r:   r9   rZ   r;   r1   rc   rX   r   rA   Újoinr“   r‚   r   rF   r€   r\   r}   r�   Úvalidate_maxrW   rN   rƒ   r`   r!   r   re   Úvalidate_minr^   r   Z
error_list)r    Zempty_forms_countÚerrorrl   rb   rT   Úer$   rG   r%   r[   Œ  s|     ÿ
 þÿø
ÿþÿþýþÿÿþýþþý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$   rG   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 rM   )r€   rz   r$   r$   r%   rU   Û  s     z*BaseFormSet.has_changed.<locals>.<genexpr>)rV   rG   r$   r$   r%   r€   Ù  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sz|dk	r–||k r–t	tdƒd|  
¡ d�|jt< dS )z8A hook for adding extra fields on to each form instance.NZOrderr…   F)Úlabelr7   r   r   )r¢   r   r   ZDelete)r\   r‰   r   Ú_r‘   ÚfieldsrŠ   r}   Úcan_delete_extrar   r�   r—   )r    rb   rK   r\   r$   r$   r%   rt   Ý  s4    üýÿÿÿýzBaseFormSet.add_fieldsc                 C   s   d| j |f S )Nz%s-%s)r3   rJ   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)rF   Úis_multipartry   rG   r$   r$   r%   r¦   û  s    zBaseFormSet.is_multipartc                 C   s   | j r| j d jS | jjS d S r   )rF   Úmediary   rG   r$   r$   r%   r§     s    zBaseFormSet.mediac                 C   s   | j jS rM   )rZ   Zformset_template_namerG   r$   r$   r%   r*     s    zBaseFormSet.template_namec                 C   s   d| iS )NÚformsetr$   rG   r$   r$   r%   Úget_context  s    zBaseFormSet.get_context)6r&   r'   r(   r)   r	   rŽ   r   r�   r£   r   r0   Ztemplate_name_divZtemplate_name_pZtemplate_name_tableZtemplate_name_ulr   rD   rH   rL   rO   rP   rY   r   rc   rW   r\   rF   rj   ri   Úpropertyrh   rx   ry   r!   rƒ   rŒ   Úclassmethodr2   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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'.)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   )rS   r¨   r$   r$   r%   rm   C  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>   sF      eô
*