U
    }HaR\                     @   sF  d dl 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mZ 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mZmZmZmZmZmZm Z  d dl!m"Z" ddl#m$Z$m%Z%m&Z&m'Z' ddlm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3 ddl4m5Z5m6Z6m7Z7 dd Z8dd Z9dd Z:dd Z;dd Z<dd Z=dd Z>dd  Z?d!d" Z@e&d#d$ ZAd%d& ZBd'd( ZCd)d* ZDd+d, ZEd-d. ZFd/d0 ZGd1d2 ZHd3d4 ZIe'd5d6 ZJe'd7d8 ZKe$d9d: ZLe&d;d< ZMe&d=d> ZNe&e jOPd?d@dAgdBdC ZQe%dDdE ZRe&dFdG ZSdHdI ZTe&dJdK ZUe&dLdM ZVdS )N    N)forms)formset_factorymodelformset_factory)_get_new_csrf_string)render)ContextTemplate)reverse)gettext_lazy)FieldInlineCheckboxes)
FormHelper)	HTMLButtonHolderColumnDivFieldsetLayout
MultiFieldRowSubmit)render_crispy_form   )only_bootstraponly_bootstrap3only_bootstrap4only_uni_form)AdvancedFileFormCheckboxesSampleFormCrispyEmptyChoiceTestModelCrispyTestModelFileForm
SampleFormSampleForm2SampleForm3SampleForm4SampleForm5SampleForm6SelectSampleForm)contains_partialparse_expected
parse_formc              	   C   sV   t  }|td td}tt |d}d| _tt	 |
| W 5 Q R X d S )Nu   españaP
        {% load crispy_forms_tags %}
        {% crispy form form_helper %}
    formform_helperFr   
add_layoutr   r   r   r"   ZCRISPY_FAIL_SILENTLYpytestZraises	Exceptionr   settingsr/   templatec r8   B/tmp/pip-unpacked-wheel-rp2i33ek/crispy_forms/tests/test_layout.pytest_invalid_unicode_characters"   s    r:   c                  C   s,   G dd dt j} t|  }d|ks(td S )Nc                       s,   e Zd Z fddZe Zede_  ZS )z,test_unicode_form_field.<locals>.UnicodeFormc                    s    t  j|| t | jd< d S )N   contraseña)super__init__r   	CharFieldfields)selfargskwargs	__class__r8   r9   r=   5   s    z5test_unicode_form_field.<locals>.UnicodeForm.__init__r;   )	__name__
__module____qualname__r=   r   helperr   layout__classcell__r8   r8   rC   r9   UnicodeForm4   s   rK   u   id="id_contraseña")r   Formr   AssertionError)rK   htmlr8   r8   r9   test_unicode_form_field3   s    
rO   c                  C   s^   G dd dt } |  }|jd= t }td|_td}t||d}||}d|ksZtd S )Nc                   @   s   e Zd ZG dd dZdS )z@test_meta_extra_fields_with_missing_fields.<locals>.FormWithMetac                   @   s   e Zd ZdZdS )zEtest_meta_extra_fields_with_missing_fields.<locals>.FormWithMeta.Meta)email
first_name	last_nameN)rE   rF   rG   r?   r8   r8   r8   r9   MetaB   s   rS   N)rE   rF   rG   rS   r8   r8   r8   r9   FormWithMetaA   s   rT   rP   rQ   r,   r-   )	r"   r?   r   r   rI   r   r   r   rM   )rT   r.   r/   r6   r7   rN   r8   r8   r9   *test_meta_extra_fields_with_missing_fields@   s    

rU   c              	   C   sV   t  }|td td}tt |d}d| _tt	 |
| W 5 Q R X d S )NZtypor,   r-   Fr0   r4   r8   r8   r9   test_layout_unresolved_fieldW   s    rV   c              	   C   sX   t  }|tdd td}tt |d}d| _tt	 |
| W 5 Q R X d S )N
is_companyr,   r-   Fr0   r4   r8   r8   r9   test_double_rendered_fieldg   s    rX   c                  C   sf   G dd dt j} |  }t }td}t||d}||}|ddksPt|ddksbtd S )	Nc                   @   s   e Zd Ze ZdS )z+test_context_pollution.<locals>.ExampleFormN)rE   rF   rG   r   r>   commentr8   r8   r8   r9   ExampleFormx   s   rZ   zw
        {% load crispy_forms_tags %}
        {{ form.as_ul }}
        {% crispy form2 %}
        {{ form.as_ul }}
    )r.   form2zname="comment"   zname="is_company"r   )r   rL   r"   r   r   r   countrM   )rZ   r.   r[   r6   r7   rN   r8   r8   r9   test_context_pollutionw   s    
r^   c                 C   s  t  }|ttdddddddtdd	td
ddddtdtddd td}tt |ddd}|	|}d|ks|t
d|kst
d|kst
d|kst
d|kst
|ddkst
| jdkrd|kst
n$| jd krd!|kst
nd"|kst
d|kst
d#|kst
d S )$NCompany DatarW   Zfieldset_company_dataZ	fieldsetsZfieldset_title123)css_id	css_classtitleZtest_fieldsetz	User DatarP   	password1	password2Zrow_passwordsZrowsra   rb   z'<a href="#" id="testLink">test link</a>zK
                    {% if flag %}{{ message }}{% endif %}
                rQ   rR   r,   TzHello!)r.   r/   flagmessagezid="fieldset_company_data"zclass="fieldsetsztitle="fieldset_title"ztest-fieldset="123"zid="row_passwords"z<label   uni_formzclass="formRow rows"
bootstrap4zclass="form-row rows"zclass="row rows"ZtestLink)r   r1   r   r   r   r   r   r   r"   r   rM   r]   CRISPY_TEMPLATE_PACK)r5   r/   r6   r7   rN   r8   r8   r9   5test_layout_fieldset_row_html_with_unicode_fieldnames   s^    


rm   c                  C   s~   t d} t }t }|ttdddddddtd	d
dd |jd= |jjd jd= t	||d}| 
|}d|ksztd S )Nr,   r_   rW   rP   rd   re   multifield_infora   rQ   rR   column_namer   r   r-   )r   r"   r   r1   r   r   r   r?   rI   r   r   rM   )r6   r.   r/   r7   rN   r8   r8   r9   +test_change_layout_dynamically_delete_field   s6    
rq   c                 C   s   t d}t }t }|ttdddddddtd	d
 t||d}||}| j	dkr|
ddksnt|
ddkstn.| j	dkr|
ddkst|
ddkstd S )Nr,   r_   rW   rP   rd   re   rn   ro   rQ   rR   r-   rj   
formColumnr   colr   rk   col-md)r   r"   r   r1   r   r   r   r   r   rl   r]   rM   r5   r6   r.   r/   r7   rN   r8   r8   r9   test_column_has_css_classes   s4    


rv   c              
   C   s   t d}t }t }|ttddtddddtdddd t||d}||}|dd	ksjt	|dd
ks|t	d S )Nr,   rQ   rR   zcol-smrb   zmb-4r-   rt   r\   r   )
r   r"   r   r1   r   r   r   r   r]   rM   ru   r8   r8   r9   test_bs4_column_css_classes  s     
rx   c                 C   s  t tdd}| }t }d|_d|_d|_d|_ttddd	t	d
t
ddtddd|_t||dt id}t|ds|tt|dstt|dstt|dst|ddkst|ddkst|ddkstd|kstd|kstd|kstdtd |kstd|ks&td|ks4td |ksBt|d!dksVt| jd"krx|d#dkstn | jd$kr|d%dkst| jd&kr|d'd(kstd S ))N   )extraZthisFormsetRockszformsets-that-rockPOSTZsimpleActionzItem {{ forloop.counter }}rW   rP   z9{% if forloop.first %}Note for first form only{% endif %}rd   re    rQ   rR   Z
csrf_token)r.   rH   contextQ<input id="id_form-TOTAL_FORMS" name="form-TOTAL_FORMS" type="hidden" value="3"/>U<input id="id_form-INITIAL_FORMS" name="form-INITIAL_FORMS" type="hidden" value="0"/>X<input id="id_form-MAX_NUM_FORMS" name="form-MAX_NUM_FORMS" type="hidden" value="1000"/>zU<input id="id_form-MIN_NUM_FORMS" name="form-MIN_NUM_FORMS" type="hidden" value="0"/>hidden   z<formr   Zcsrfmiddlewaretokenzmethod="post"zid="thisFormsetRocks"zaction="%s"zItem 1zItem 2zItem 3zNote for first form onlyrj   ZformRow)
bootstrap3rk   rowrk   z
form-group   )r   r"   r   Zform_id
form_classZform_methodZform_actionr   r   r   r   rI   r   r   r)   rM   r]   r	   rl   )r5   ZSampleFormSetformsetrH   rN   r8   r8   r9   test_formset_layout+  s`    
   r   c                  C   s   t ttdd} | tj d}t }td|_t||d}|	ddksLt
|	ddks^t
|	d	dkspt
t|d
s~t
t|dst
t|dst
|	ddkst
|	ddkst
|	ddkst
|	ddkst
|	ddkst
d S )Nry   )r.   rz   )ZquerysetrP   )r.   rH   zid_form-0-idr   zid_form-1-idzid_form-2-idr~   r   r   zname="form-0-email"zname="form-1-email"zname="form-2-email"zname="form-3-email"r   password)r   r    r%   Zobjectsnoner   r   rI   r   r]   rM   r)   )ZCrispyModelFormSetr   rH   rN   r8   r8   r9   test_modelformset_layoutc  s,    
  r   c                  C   sb   t d} t }t }tttdttddd|_||_| 	t
d|i}|ddks^td S )NzP
        {% load crispy_forms_tags %}
        {% crispy form form.helper %}
    z	i18n textzi18n legendrQ   rR   r.   r   )r   r"   r   r   r   _r   rI   rH   r   r   r]   rM   )r6   r.   r/   rN   r8   r8   r9   	test_i18n~  s     
r   c                  C   s*   t  } | jjjdddddddgks&td S )NrW   rP   rd   re   rQ   rR   Zdatetime_field)r#   rH   rI   r?   rM   	test_formr8   r8   r9   test_default_layout  s    r   c                  C   s   t  } | jjjdgkstd S )NrP   )r$   rH   rI   r?   rM   r   r8   r8   r9   test_default_layout_two  s    r   c                  C   s>   t  } t | _td| j_t| }d|ks.td|ks:td S )NrP   r   )r%   r   rH   r   rI   r   rM   r   rN   r8   r8   r9   "test_modelform_layout_without_meta  s    r   c                  C   sH   t  } t | jd _t | _tdtd| j_	t
| }d|ksDtd S )NrP   z0<span>first span</span> <span>second span</span>)r"   r   ZTextarear?   Zwidgetr   rH   r   r   rI   r   rM   r   r8   r8   r9   2test_specialspaceless_not_screwing_intended_spaces  s    r   c                  C   s.   t  } d | _t| d}t|}d|ks*td S )N)instancechecked)r   Zfruitr'   r   rM   )Zmodel_instancer   rN   r8   r8   r9   !test_choice_with_none_is_selected  s
    
r   c                  C   s   t  } | tttdddddtdddd	ttd
d
ddtddddd	 td}t	t
 | d}||}d|ks|td|kstd|kstd|kstd|kstd|kstd|kstd|kstd|kstd|kstd|kstd S )NSome company datarW   rP   rn   ro   rQ   rp   columnsrf   Savebutton whiterw   rd   re   
custom-div
customdivs\
            {% load crispy_forms_tags %}
            {% crispy form form_helper %}
        r-   
multiFieldrr   id="multifield_info"id="column_name"class="formColumn columns"class="buttonHolder">input type="submit"zname="Save"id="custom-div"class="customdivs"rR   r   r1   r   r   r   r   r   r   r   r   r"   r   rM   r/   r6   r7   rN   r8   r8   r9   test_layout_composition  sV    
r   c                  C   s|  t  } | ttdddddddtdd	d
ddttddddddtddtdddddd td}t	t
 | dd}||}d|kstd|kstd|kstd|kstd |kstd!|kstd"|kstd#|kstd$|kstd|kstd%|kstd&|kstd'|ks$td(|ks2td)|ks@td*|ksNtd+|ks\td,|ksjtd-|ksxtd S ).Nr   rW   rP   rn   Zmultifield_titler`   )ra   rc   Zmultifield_testrQ   rR   rp   r   rf   zSave the worldz{{ value_var }}r   test)rb   Zdata_idZ	data_namestorezStore resultsrd   re   r   r   )ra   rb   Ztest_markupr   r   )r.   r/   Z	value_varr   rr   r   ztitle="multifield_title"zmultifield-test="123"r   r   r   r   zdata-id="test"zdata-name="test"zname="save-the-world"zvalue="Save"zname="store"zvalue="Store results"r   r   ztest-markup="123"r   r   r8   r8   r9   <test_second_layout_multifield_column_buttonholder_submit_div  sl        
r   c                 C   s   t  }t |_tdtdd|j_d|i}td d|d}| jdkrX|j	dd	kst
nR| jd
krx|j	dd	kst
n2| jdkr|j	dd	kst
|j	ddkst
d S )N
checkboxesalphacheckboxesnumeric_multiple_checkboxesr.   zcrispy_render_template.html)requestZtemplate_namer}   Z	bootstraps   checkbox inlinery   r   s   checkbox-inlinerk   s   custom-control-inlines   custom-checkboxr   )r   r   rH   r   r   rI   r   rl   contentr]   rM   )r5   r.   r}   responser8   r8   r9    test_keepcontext_context_manager4  s    


r   c                  C   s   t  } t | _tdtdd| j_t| tdks6td| j_	t| tdksRtd| j_	t| tdksntt i } t | _tdtdd| j_t| tdkstd S )	Nr   r   r   zNbootstrap4/test_layout/test_use_custom_control_is_used_in_checkboxes_true.htmlTFzObootstrap4/test_layout/test_use_custom_control_is_used_in_checkboxes_false.htmlzVbootstrap4/test_layout/test_use_custom_control_is_used_in_checkboxes_true_failing.html)
r   r   rH   r   r   rI   r+   r*   rM   use_custom_controlr.   r8   r8   r9   -test_use_custom_control_is_used_in_checkboxesI  s8    


r   c                  C   s   t  } t | _td| j_t| tdks.td| j_t| tdksJtd| j_t| tdksftt i } t | _td| j_t| tdkstd S )NZradio_selectzIbootstrap4/test_layout/test_use_custom_control_is_used_in_radio_true.htmlTFzJbootstrap4/test_layout/test_use_custom_control_is_used_in_radio_false.htmlzQbootstrap4/test_layout/test_use_custom_control_is_used_in_radio_true_failing.html)	r&   r   rH   r   rI   r+   r*   rM   r   r   r8   r8   r9   (test_use_custom_control_is_used_in_radiom  s0    


r   z!use_custom_control, expected_html)TzBbootstrap4/test_layout/test_use_custom_control_in_select_true.html)FzCbootstrap4/test_layout/test_use_custom_control_in_select_false.htmlc                 C   sB   t  }t |_d|j_td|j_| |j_t|t|ks>t	d S )Nrk   select)
r(   r   rH   Ztemplate_packr   rI   r   r+   r*   rM   )r   Zexpected_htmlr.   r8   r8   r9   !test_use_custom_control_in_select  s    	r   c                  C   s   t  } t | _d| j_d| j_tddd| j_t| }|ddksHt	|dd	ksZt	|d
dkslt	|ddks~t	|ddkst	|dd	kst	d S )Nform-inlinez#bootstrap3/layout/inline_field.htmlrP   rd   rR   class="form-inline"r   zclass="form-group"ry   $<label for="id_email" class="sr-onlyz$id="div_id_email" class="form-group"placeholder="email"</label> <input
r"   r   rH   r   Zfield_templater   rI   r   r]   rM   r.   rN   r8   r8   r9   test_form_inline  s     r   c                  C   s   t  } t | _d| j_d| j_tddd| j_t| }|ddksHt	|dd	ksZt	|d
dkslt	|ddks~t	|ddkst	|dd	kst	d S )Nr   z#bootstrap4/layout/inline_field.htmlrP   rd   rR   r   r   zclass="input-group"ry   r   z%id="div_id_email" class="input-group"r   r   r   r   r8   r8   r9   test_bootstrap4_form_inline  s    r   c                  C   s   t  } t | _tdtdd| j_| jd jdd t| }|ddksNt	t | _tdtdddd| j_| jd jdd t| }|d	dkst	d S )
NrP   rd   re   Zhellorw   z class="hello textinputr   Zhello2z class="hello hello2 textinput)
r"   r   rH   r   r   rI   Zupdate_attributesr   r]   rM   r   r8   r8   r9   test_update_attributes_class  s    
r   c                  C   s   t  } t | _td| j_t| tdks.td| j_t| tdksJtd| j_td| j_t| tdksrtd| j_t| tdkstd S )	Nclearable_filezDbootstrap4/test_layout/test_file_field_clearable_custom_control.htmlFz5bootstrap4/test_layout/test_file_field_clearable.htmlT
file_fieldz:bootstrap4/test_layout/test_file_field_custom_control.htmlz3bootstrap4/test_layout/test_file_field_default.html)	r!   r   rH   r   rI   r+   r*   rM   r   r   r8   r8   r9   test_file_field  s    r   c                  C   sR   t  } t | _td| j_t| tdks.ttd| j_t| tdksNtd S )Nr   zGbootstrap4/test_layout/test_file_field_with_custom_class_clearable.htmlr   z=bootstrap4/test_layout/test_file_field_with_custom_class.html)r   r   rH   r   rI   r+   r*   rM   r   r8   r8   r9   !test_file_field_with_custom_class  s    
r   )Wr2   Zdjangor   Zdjango.forms.modelsr   r   Zdjango.middleware.csrfr   Zdjango.shortcutsr   Zdjango.templater   r   Zdjango.urlsr	   Zdjango.utils.translationr
   r   Zcrispy_forms.bootstrapr   r   Zcrispy_forms.helperr   Zcrispy_forms.layoutr   r   r   r   r   r   r   r   r   Zcrispy_forms.utilsr   Zconftestr   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   utilsr)   r*   r+   r:   rO   rU   rV   rX   r^   rm   rq   rv   rx   r   r   r   r   r   r   r   r   r   r   r   r   r   markZparametrizer   r   r   r   r   r   r8   r8   r8   r9   <module>   sx   ,8;&#
8

	
4
:

#



