o
    h;                     @   sh  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mZ d dlmZ d dlmZ d d	lmZ d d
lmZmZmZmZ d dlmZ d dlmZ d dl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/ d dl0m1Z1 ddl2mZm3Z3m4Z4m5Z5 G dd deZ6G dd deZ7G dd deZ8G dd de1eZ9G dd deZ:G dd deZ;G d d! d!eZ<ed"d#d$iid%G d&d' d'eZ=ed"d#d$iid%G d(d) d)eZ>G d*d+ d+eZ?G d,d- d-eZ@G d.d/ d/eZAG d0d1 d1eZBed2gd3G d4d5 d5e1eZCG d6d7 d7eZDG d8d9 d9eZEG d:d; d;eZFdS )<    Nsettings)Group
Permission)caches)File)Storagedefault_storagestorages)SimpleUploadedFile)Prefetch)IntegrityError)SimpleTestCaseTestCaseTransactionTestCaseoverride_settings)reverse)Image)FilterPicture	RenditionResponsiveImageSourceImageIOErrorget_rendition_storage)Rect)
CollectionGroupCollectionPermissionPageReferenceIndex)	EventPageEventPageCarouselItemReimportedImageModel)WagtailTestUtils   )r   get_test_image_fileget_test_image_file_svgget_test_image_filenamec                   @   s   e Zd ZdS )CustomStorageN)__name__
__module____qualname__ r+   r+   `/var/www/html/ndineBlogger/venv/lib/python3.10/site-packages/wagtail/images/tests/test_models.pyr'   (   s    r'   c                   @   s   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zei ejdddiiddd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%S )&	TestImagec                 C   s   t jjdtddd| _d S )N
Test imagewhitecolourtitlefiler   objectscreater$   imageselfr+   r+   r,   setUp-   s   zTestImage.setUpc                 C   s   |  tt d S N)assertEqualr!   r   r9   r+   r+   r,   #test_get_image_model_at_import_time4   s   z-TestImage.test_get_image_model_at_import_timec                 C      |  | j  d S r<   )assertFalser8   is_portraitr9   r+   r+   r,   test_is_portrait7      zTestImage.test_is_portraitc                 C   r?   r<   )
assertTruer8   is_landscaper9   r+   r+   r,   test_is_landscape:   rC   zTestImage.test_is_landscapec                 C   s    |  | j tdddd d S )Nr       )r=   r8   get_rectr   r9   r+   r+   r,   test_get_rect=   s    zTestImage.test_get_rectc                 C   sP   |  | j  d| j_d| j_d| j_d| j_| | j tdddd d S )	Nd      2      K      }      )	assertIsNoner8   get_focal_pointfocal_point_xfocal_point_yfocal_point_widthfocal_point_heightr=   r   r9   r+   r+   r,   test_get_focal_point@   s    zTestImage.test_get_focal_pointc                 C   sD   |  | j  d| j_d| j_d| j_d| j_| | j  d S )NrK   rL   rM   rN   )r@   r8   has_focal_pointrU   rV   rW   rX   rD   r9   r+   r+   r,   test_has_focal_pointL   s   zTestImage.test_has_focal_pointc                 C   s   |  | jj |  | jj |  | jj |  | jj | jtdddd | | jjd | | jjd | | jjd | | jjd | jd  |  | jj |  | jj |  | jj |  | jj d S )NrK      rL   i^     )	rS   r8   rU   rV   rW   rX   set_focal_pointr   r=   r9   r+   r+   r,   test_set_focal_pointW   s   zTestImage.test_set_focal_pointc                 C   r?   r<   )rD   r8   is_stored_locallyr9   r+   r+   r,   test_is_stored_locallyk   rC   z TestImage.test_is_stored_locallydefaultBACKENDz8wagtail.test.dummy_external_storage.DummyExternalStorage)STORAGESc                 C   r?   r<   )r@   r8   r`   r9   r+   r+   r,   ,test_is_stored_locally_with_external_storagen   s   	z6TestImage.test_is_stored_locally_with_external_storagec                 C   s&   | j  }| |t | |d d S Nr   )r8   get_file_sizeassertIsInstanceintassertGreater)r:   	file_sizer+   r+   r,   test_get_file_sizey   s   
zTestImage.test_get_file_sizec                 C   sJ   | j jjdd | t | j   W d    d S 1 sw   Y  d S )NF)save)r8   r4   deleteassertRaisesr   rg   r9   r+   r+   r,   <test_get_file_size_on_missing_file_raises_sourceimageioerror~   s   "zFTestImage.test_get_file_size_on_missing_file_raises_sourceimageioerrorc                 C   s   |  | j d d S )N(4dd0211870e130b7e1690d2ec53c499a54a48fef)r=   r8   get_file_hashr9   r+   r+   r,   test_file_hash   s   
zTestImage.test_file_hashc                 C   s$   t jjdt d}| |  dS )zw
        Feature detection should not be run on SVGs.

        https://github.com/wagtail/wagtail/issues/11172
        zTest SVGr2   N)r   r6   r7   r%   rS   get_suggested_focal_pointr:   r8   r+   r+   r,   "test_get_suggested_focal_point_svg   s
   z,TestImage.test_get_suggested_focal_point_svgc                 C   s(   t jjddt d}| |j|j d S )N
Test ImagezThis is a test description)r3   descriptionr4   )r   r6   r7   r$   r=   default_alt_textrx   ru   r+   r+   r,   test_default_with_description   s   z'TestImage.test_default_with_descriptionc                 C   s&   t jjdt d}| |j|j d S )Nrw   r2   )r   r6   r7   r$   r=   ry   r3   ru   r+   r+   r,    test_default_without_description   s
   z*TestImage.test_default_without_descriptionN)r(   r)   r*   r;   r>   rB   rF   rJ   rY   r[   r_   ra   r   r   rd   re   rl   rp   rs   rv   rz   r{   r+   r+   r+   r,   r-   ,   s4    
	r-   c                   @   s2   e Zd ZdgZdd Zdd Zdd Zdd	 Zd
S )TestImageQuerySetztest_empty.jsonc                 C   s4   t jjdt d}t jd}| t||g d S )Nr.   r2   Test)r   r6   r7   r$   searchr=   listr:   r8   resultsr+   r+   r,   test_search_method   s   z$TestImageQuerySet.test_search_methodc                 C   sz   t jjdt d}t jjdt d}t jjddd}| t||g t jjddd}t|dd	 d
}| |||g d S )NAAA Test imager2   ZZZ Test imagezaaa testand)operatororc                 S   s   | j S r<   r3   )imgr+   r+   r,   <lambda>   s    z2TestImageQuerySet.test_operators.<locals>.<lambda>)key)r   r6   r7   r$   r~   r=   r   sorted)r:   	aaa_image	zzz_imager   sorted_resultsr+   r+   r,   test_operators   s   z TestImageQuerySet.test_operatorsc                 C   s|   t jjdt d}t jjdt d}t jdjddd}| t|||g t jdjddd}| t|||g d S )	Nr   r2   r   r3   r}   F)order_by_relevancez-title)r   r6   r7   r$   order_byr~   r=   r   )r:   r   r   r   r+   r+   r,   test_custom_ordering   s    z&TestImageQuerySet.test_custom_orderingc                 C   s   t ddD ]}tjjd| t d}|jddd q| d d	d
 t D }| 	d|d  W d    d S 1 s=w   Y  d S )Nr   
   zTest image %dr2   aardvark	artichoke	armadillo   c                 S   s$   i | ]}|j d d |j D qS )c                 S   s   g | ]}|j qS r+   name).0tagr+   r+   r,   
<listcomp>       zUTestImageQuerySet.test_search_indexing_prefetches_tags.<locals>.<dictcomp>.<listcomp>)r3   tagsallr   r8   r+   r+   r,   
<dictcomp>   s    zJTestImageQuerySet.test_search_indexing_prefetches_tags.<locals>.<dictcomp>zTest image 0)
ranger   r6   r7   r$   r   addassertNumQueriesget_indexed_objectsassertIn)r:   ir8   r   r+   r+   r,   $test_search_indexing_prefetches_tags   s   "z6TestImageQuerySet.test_search_indexing_prefetches_tagsN)r(   r)   r*   fixturesr   r   r   r   r+   r+   r+   r,   r|      s    r|   c                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )TestImagePermissionsc                 C   s   | j dddd| _| j dddd| _| j dddd| _| jjtjjd	d
 | j	dddd| _
tjjdd
}tjj|t tjjddd | jj| tjjd| jt d| _d S )Nuserzuser@email.compassword)usernameemailr   ownerzowner@email.comeditorzeditor@email.comEditorsr   administratorzadministrator@email.comzImage adders	add_image)codename)group
collection
permissionr.   )r3   uploaded_by_userr4   )create_userr   r   r   groupsr   r   r6   getcreate_superuserr   r7   r   r   get_first_root_noder   r   r$   r8   )r:   image_adders_groupr+   r+   r,   r;      s6   zTestImagePermissions.setUpc                 C      |  | j| j d S r<   )rD   r8   is_editable_by_userr   r9   r+   r+   r,   test_administrator_can_edit     z0TestImagePermissions.test_administrator_can_editc                 C   r   r<   )rD   r8   r   r   r9   r+   r+   r,   test_editor_can_edit  r   z)TestImagePermissions.test_editor_can_editc                 C   r   r<   )rD   r8   r   r   r9   r+   r+   r,   test_owner_can_edit  r   z(TestImagePermissions.test_owner_can_editc                 C   r   r<   )r@   r8   r   r   r9   r+   r+   r,   test_user_cant_edit  r   z(TestImagePermissions.test_user_cant_editN)r(   r)   r*   r;   r   r   r   r   r+   r+   r+   r,   r      s    "r   c                   @   sL   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dS )TestFiltersc                 C      |  tddg d S )N	width-100r=   r   expand_specr9   r+   r+   r,   test_expand_spec_single  r   z#TestFilters.test_expand_spec_singlec                 C   s   |  tddg d S )Nzwidth-100 jpegquality-20width-100|jpegquality-20r   r9   r+   r+   r,   test_expand_spec_flat     z!TestFilters.test_expand_spec_flatc                 C   r   )Nr   r   r9   r+   r+   r,   test_expand_spec_pipe!  r   z!TestFilters.test_expand_spec_pipec                 C   s   |  tddgdg d S )Nr   zjpegquality-20r   r   r9   r+   r+   r,   test_expand_spec_list&  s   z!TestFilters.test_expand_spec_listc                 C      |  tdddg d S )Nzwidth-{100,200}r   z	width-200r   r9   r+   r+   r,   test_expand_spec_braced,  s   z#TestFilters.test_expand_spec_bracedc                 C   r   )Nzwidth-{100,200} jpegquality-40width-100|jpegquality-40width-200|jpegquality-40r   r9   r+   r+   r,   test_expand_spec_mixed1     z"TestFilters.test_expand_spec_mixedc                 C   r   )Nzwidth-{100,200}|jpegquality-40r   r   r   r9   r+   r+   r,   test_expand_spec_mixed_pipe7  r   z'TestFilters.test_expand_spec_mixed_pipec                 C   s   |  tdg d d S )Nz-width-{100,200} jpegquality-{40,80} grayscale)z"width-100|jpegquality-40|grayscalez"width-100|jpegquality-80|grayscalez"width-200|jpegquality-40|grayscalez"width-200|jpegquality-80|grayscaler   r9   r+   r+   r,    test_expand_spec_multiple_braces=  r   z,TestFilters.test_expand_spec_multiple_bracesN)r(   r)   r*   r   r   r   r   r   r   r   r   r+   r+   r+   r,   r     s    r   c                   @   sT   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd ZdS )TestResponsiveImagec                 C   &   t jjdt d| _| jd| _d S Nr.   r2   width-10r   r6   r7   r$   r8   get_renditionrendition_10r9   r+   r+   r,   r;   J  
   zTestResponsiveImage.setUpc                 C   s(   t i }| |jg  | |jd  d S r<   )r   r=   
renditionsattrsr:   r   r+   r+   r,   test_construct_emptyR  s   z(TestResponsiveImage.test_construct_emptyc                 C   s(   d| j i}t|}| |j| j g d S )Na)r   r   r=   r   r:   r   r   r+   r+   r,   test_construct_with_renditionsW  s   
z2TestResponsiveImage.test_construct_with_renditionsc                 C   s>   |  ti  |  ti ddi d| ji}| t| d S )Nsizes100vwr   )r@   r   r   rD   r:   r   r+   r+   r,   test_evaluate_value\  s   
z'TestResponsiveImage.test_evaluate_valuec                 C   sp   d| j i}t|}t|}td| jdi}t|ddi}| || | |d | || | || d S )Nr   zwidth-15r   r   i90  )r   r   r8   r   assertNotEqualr=   )r:   r   value1value2value3value4r+   r+   r,   test_compare_valuec  s   
z&TestResponsiveImage.test_compare_valuec                 C   s^   | j | jdd}t| jdt| jdg}| tt| |d  d|d  d d S )Nwidth-90r   r   r   r    10w, r#   z 90w)	r   r8   r   r&   r=   r   get_width_srcsetr   values)r:   r   	filenamesr+   r+   r,   test_get_width_srcsetn  s   


z)TestResponsiveImage.test_get_width_srcsetc                 C   s0   d| j i}| tt| t| jd d S Nr   )r   r=   r   r   r   r   r&   r8   r   r+   r+   r,   &test_get_width_srcset_single_rendition|  s
   

z:TestResponsiveImage.test_get_width_srcset_single_renditionc              
   C   sh   | j | jdd}t|}t| jdt| jdg}| | d|d  d|d  d|d  d	 d S )
Nr   r   r   zT
                <img
                    alt="Test image"
                    src="r   z"
                    srcset="r   r#   zb 90w"
                    width="10"
                    height="7"
                >
            )r   r8   r   r   r&   assertHTMLEqual__html__r:   r   r   r   r+   r+   r,   test_render  s"   


zTestResponsiveImage.test_renderc                 C   s(   t d| ji}| | | j  d S r   )r   r   r   r   img_tagr   r+   r+   r,   (test_render_single_image_same_as_img_tag  s   z<TestResponsiveImage.test_render_single_image_same_as_img_tagN)r(   r)   r*   r;   r   r   r   r   r   r   r   r  r+   r+   r+   r,   r   I  s    r   c                   @   sD   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dS )TestPicturec                 C   r   r   r   r9   r+   r+   r,   r;     r   zTestPicture.setUpc                 C   s6   | j | j d}t|}| |j| j g| j gd d S )N)format-jpegformat-webpjpegwebpr   r   r=   formatsr   r+   r+   r,   test_formats  s   zTestPicture.test_formatsc                 C   s$   d| j i}t|}| |ji  d S )Nr  r  r   r+   r+   r,   test_single_format  s   
zTestPicture.test_single_formatc                 C   s>   | j | j | j d}t|}| |j| j g| j | j gd d S )N)r  r  zformat-webp-losslessr  r  r   r+   r+   r,   test_mixed_format  s   
zTestPicture.test_mixed_formatc           	      C   s   d| j i}d| j i}d| j i}d| j i}d| j i}i |||||i ||||i |||i ||d}| D ]\}}| t| | q@d S )Nzformat-avifr  r  z
format-pngz
format-gif)gifpngr  r  )r   itemsr=   r   get_fallback_format)	r:   avifr  r  r  r  	fallbacksfmtr   r+   r+   r,   test_fallback_format  s   





z TestPicture.test_fallback_formatc                 C   s   | j d| j d| j d| j dd}t|ddi}t| j dt| j d	t| j d
t| j dg}| | d|d  d|d  d|d  d|d  d|d  d d S )Nformat-jpeg|width-10format-jpeg|width-90format-webp|width-10format-webp|width-90)r  r  r  r  r   r   zformat-jpeg.width-10zformat-jpeg.width-90zformat-webp.width-10zformat-webp.width-90z?
                <picture>
                    <source srcset="r   r      z 90w" sizes="100vw" type="image/webp">
                    <img
                        alt="Test image"
                        sizes="100vw"
                        src="r   z""
                        srcset="r#   z 90w"
                        width="10"
                        height="7"
                    >
                </picture>
            )r8   r   r   r&   r   r   r   r+   r+   r,   test_render_multi_format_sizes  s2   







z*TestPicture.test_render_multi_format_sizesc                 C   s0   t d| ji}| | d| j  d d S )Nr   z	<picture>z
</picture>)r   r   r   r   r   r   r+   r+   r,   r    s   z4TestPicture.test_render_single_image_same_as_img_tagN)
r(   r)   r*   r;   r
  r  r  r  r  r  r+   r+   r+   r,   r    s    
r  rb   rc   +django.core.cache.backends.dummy.DummyCacheCACHESc                   @   sl  e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Z		dLded ed!efd"d#Zd$d% Zed&d'd(iid)d*d+ Zd,d- Zd.d/ Zd0d1 Zed&d'd(iid)d2d3 Zd4d5 Zd6d7 Zd8d9 Zd:d; Ze d<d= Z ed>d?d@dA Z!ee" d?dBdC Z#ed'dDid'dEid'd>idFdGdHdIdJ Z$dKS )MTestRenditions)	height-66r   	width-400c                 C      t jjdt d| _d S Nr.   r2   r5   r9   r+   r+   r,   r;     s   zTestRenditions.setUpc                 C   s   |  t t d S r<   )assertIsr   get_rendition_modelr   r9   r+   r+   r,   test_get_rendition_model  rC   z'TestRenditions.test_get_rendition_modelc                 C   s:   | j d}| |jd | |jd | |jd d S )Nr   i  i,  )r8   r   r=   widthheightfilter_specr:   	renditionr+   r+   r,   test_minification  s   z TestRenditions.test_minificationc                 C   ,   | j d}| |jd | |jd d S )Nmax-100x100rK   rO   r8   r   r=   r&  r'  r)  r+   r+   r,   test_resize_to_max     z!TestRenditions.test_resize_to_maxc                 C   r,  )Nzmin-120x120   x   r.  r)  r+   r+   r,   test_resize_to_min  r0  z!TestRenditions.test_resize_to_minc                 C   r,  )NoriginalrG   rH   r.  r)  r+   r+   r,   test_resize_to_original$  r0  z&TestRenditions.test_resize_to_originalc                 C   s(   | j d}| j d}| || d S Nr   )r8   r   r=   r:   first_renditionsecond_renditionr+   r+   r,   
test_cache+  s   zTestRenditions.test_cachec                 C   s,   | j d}| j td}| || d S r6  )r8   r   r   r=   r7  r+   r+   r,   test_get_with_filter_instance3  s   z,TestRenditions.test_get_with_filter_instancec                 C   s   |  d | jd}W d    n1 sw   Y  tjdj| jjd}|  d |d}W d    n1 s<w   Y  | || d S )N   r   r   pkr   )	r   r8   r   r   r6   prefetch_relatedr   r>  r=   r:   original_renditionr8   r9  r+   r+   r,   test_prefetched_rendition_found;  s   z.TestRenditions.test_prefetched_rendition_foundc                 C   s   |  d | jd}W d    n1 sw   Y  tjdj| jjd}|  d |d}W d    n1 s<w   Y  | || d S )Nr<  r   r=  r   )	r   r8   r   r   r6   prefetch_renditionsr   r>  r=   r@  r+   r+   r,   test_prefetch_renditions_foundJ  s   z-TestRenditions.test_prefetch_renditions_foundc                 C   s   |  d | jd}W d    n1 sw   Y  tjdj| jjd}|  d |d}W d    n1 s<w   Y  | || |  d |d}W d    n1 s\w   Y  | 	|| d S )Nr<  r   r   r=     r  r   )
r   r8   r   r   r6   r?  r   r>  r   r#  r:   rA  r8   r9  third_renditionr+   r+   r,   #test_prefetched_rendition_not_foundV  s   z2TestRenditions.test_prefetched_rendition_not_foundc                 C   s   |  d | jd}W d    n1 sw   Y  tjdj| jjd}|  d |d}W d    n1 s<w   Y  | || |  d |d}W d    n1 s\w   Y  | 	|| d S )Nr<  r   r=  rE  r  r   )
r   r8   r   r   r6   rC  r   r>  r   r#  rF  r+   r+   r,   "test_prefetch_renditions_not_foundr  s   z1TestRenditions.test_prefetch_renditions_not_foundc                 C   sD   t | jd }t | jtd }| |d |d  d S )Nr   r   )r   r8   get_renditionsr   r   r=   )r:   firstsecondr+   r+   r,   (test_get_renditions_with_filter_instance  s   z7TestRenditions.test_get_renditions_with_filter_instancec                 C   s8   | j d g d}t| j j|  }| || d S )Nwidth-40)zwidth-30rN  zwidth-50)r8   r   r   rJ  keysr=   )r:   specsrenditions_keysr+   r+   r,   test_get_renditions_key_order  s   z,TestRenditions.test_get_renditions_key_orderFdb_queries_expectedprefetch_restrictedprefetch_allc                 C   sv   t j }|r|d}n|r|j| j }|j| jjd}| 	| |j
| j  W d    d S 1 s4w   Y  d S )Nr   id)r   r6   r   r?  rC  SPECSr   r8   rW  r   rJ  )r:   rS  rT  rU  querysetr8   r+   r+   r,    _test_get_renditions_performance  s   
"z/TestRenditions._test_get_renditions_performancec                 C   s4   |  d |  d | j ddd | j ddd d S )Nr  r#   r   TrU  )rT  rZ  r9   r+   r+   r,   ?test_get_renditions_performance_with_rendition_caching_disabled  s   

zNTestRenditions.test_get_renditions_performance_with_rendition_caching_disabledr   rc   z-django.core.cache.backends.locmem.LocMemCacher  c                 C   s&   |  d |  d | j ddd d S )Nr  r   Tr[  r\  r9   r+   r+   r,   >test_get_renditions_performance_with_rendition_caching_enabled  s   

zMTestRenditions.test_get_renditions_performance_with_rendition_caching_enabledc                 C   s  dd | j D }| d | jj| }W d    n1 sw   Y  | D ]\}}| |j|j q'| dd | D dd |D  | d | jj| }W d    n1 s\w   Y  | D ]\}}| |j|j qe| dd | D d	d |D  | d | jj| }W d    n1 sw   Y  | || | jj	j
| j d
 d  | d | jj| }W d    n1 sw   Y  | || | | |  d S )Nc                 S   s   g | ]}t |qS r+   )r   )r   specr+   r+   r,   r     s    z9TestRenditions.test_create_renditions.<locals>.<listcomp>r   c                 S      h | ]}|j qS r+   r_  r   filterr+   r+   r,   	<setcomp>  r   z8TestRenditions.test_create_renditions.<locals>.<setcomp>c                 S   r`  r+   ra  rb  r+   r+   r,   rd    r   r#   c                 S   r`  r+   ra  rb  r+   r+   r,   rd    r   c                 S   r`  r+   ra  rb  r+   r+   r,   rd    r   r   )r(  )rX  r   r8   create_renditionsr  r=   r_  r(  rO  r   rc  rn   r   )r:   filter_listresultrc  r*  second_resultthird_resultr+   r+   r,   test_create_renditions  s:   z%TestRenditions.test_create_renditionsc                 C      | j d}| |jd d S )Nr   r.   )r8   r   r=   altr)  r+   r+   r,   test_alt_attribute  s   z!TestRenditions.test_alt_attributec                 C   s:   | j d}|j}|jjdd }| |d|  d S )Nr4  /r#   zhttp://testserver/media/images/)r8   r   full_urlr4   r   splitr=   )r:   ren_imgro  img_namer+   r+   r,   test_full_url  s   zTestRenditions.test_full_urlc                 C   sz  t j}| jd}| }| ||| d|_||| | 	d | jd}W d    n1 s5w   Y  | |jd t
jdj| jjd}| 	d |d}W d    n1 sbw   Y  | t|d | |j| j tdd| j_| j  | jj   | jd}| t|d td	d| j_| j  | jj   | jd}| t|d d S )
Nz	width-500r4  r   r   r=  _markgreenr0   r/   )r   cache_backendr8   r   get_cache_keyr=   r   rt  setr   r   r6   r?  r>  r@   hasattrr#  r$   r4   rm   r   r   rn   )r:   cacher*  rendition_cache_keynew_renditionfresh_imageprefetched_renditionr+   r+   r,   test_renditions_cache	  s4   

z$TestRenditions.test_renditions_cachec                 C   sP   t ddiddidd | tjtd  W d    d S 1 s!w   Y  d S )Nrc   r  )rb   r   r  r   r   r=   r   rv  r   r9   r+   r+   r,   $test_prefers_rendition_cache_backend;  s   
"z3TestRenditions.test_prefers_rendition_cache_backendc                 C   sJ   t dddiid | tjtd  W d    d S 1 sw   Y  d S )Nrb   rc   r  r  r  r9   r+   r+   r,   0test_uses_default_cache_when_no_renditions_cacheH  s   "z?TestRenditions.test_uses_default_cache_when_no_renditions_cachec                 C   s   d| j _d| j _d| j _d| j _| j   | j d}| |j	 t
dddd	 | |jjjd | |jjjd | |jjd
 | |jjd | |jd d S )NrK   rL   rM   rN   	width-320%   _   ?   i      r   zbackground-position: 15% 41%;)r8   rU   rV   rW   rX   rm   r   r=   focal_pointroundr   centroidxyr&  r'  background_position_styler)  r+   r+   r,   test_focal_pointR  s   
zTestRenditions.test_focal_pointc                 C   rk  )Nr  zbackground-position: 50% 50%;)r8   r   r=   r  r)  r+   r+   r,   &test_background_position_style_defaultf  s   z5TestRenditions.test_background_position_style_defaultc                 C   s&   t `| tt d | t t d S )NWAGTAILIMAGES_RENDITION_STORAGE)r   r  r@   ry  r=   r   r	   r9   r+   r+   r,   %test_rendition_storage_setting_absentn  s   z4TestRenditions.test_rendition_storage_setting_absentz.wagtail.images.tests.test_models.CustomStorage)r  c                 C   s   |  t t d S r<   )rh   r   r'   r9   r+   r+   r,   0test_rendition_storage_setting_given_dotted_patht  s   z?TestRenditions.test_rendition_storage_setting_given_dotted_pathc                 C   s   |  t tj d S r<   )r=   r   r   r  r9   r+   r+   r,   5test_rendition_storage_setting_given_storage_instancez  s   zDTestRenditions.test_rendition_storage_setting_given_storage_instancez+django.core.files.storage.FileSystemStoragez5django.contrib.staticfiles.storage.StaticFilesStorage)rb   staticfilescustom_storager  )rd   r  c                 C   s   |  t ttj  d S r<   )r=   r   r
   r   r  r9   r+   r+   r,   2test_rendition_storage_setting_given_storage_alias  s   zATestRenditions.test_rendition_storage_setting_given_storage_aliasN)FF)%r(   r)   r*   rX  r;   r%  r+  r/  r3  r5  r:  r;  rB  rD  rH  rI  rM  rR  ri   boolrZ  r]  r   r^  rj  rm  rs  r  r  r  r  r  r  r  r'   r  r  r+   r+   r+   r,   r    s    


1
+




r  c                   @   s"   e Zd ZdgZdd Zdd ZdS )TestPrefetchRenditions	test.jsonc                 C   s   g | _ g | _tj d d }t|D ]$\}}tjjdt d |_	}|j
dgd | j | | j|j qdd | j D | _dd | j D | _d S )	Nr  zTest image {i}r2   
feed_image)update_fieldsc                 S      g | ]}| d qS r-  r   r   r+   r+   r,   r         
z0TestPrefetchRenditions.setUp.<locals>.<listcomp>c                 S   r  zmin-300x600r  r   r+   r+   r,   r     r  )imagesevent_pages_pksr   r6   r   	enumerater   r7   r$   r  rm   appendr>  small_renditionslarge_renditions)r:   event_pagesr   pager8   r+   r+   r,   r;     s"   zTestPrefetchRenditions.setUpc                 C   s   t dtjdd}| d ttj|j| j	d}W d    n1 s'w   Y  | d dd |D }W d    n1 sCw   Y  | 
| j| | d d	d |D }W d    n1 sfw   Y  | 
| j| d S )
Nr  r-  )rY  r  )pk__inr   c                 S      g | ]}|j d qS r  r  r   r   r  r+   r+   r,   r         zZTestPrefetchRenditions.test_prefetch_renditions_on_non_image_querysets.<locals>.<listcomp>c                 S   r  r  r  r  r+   r+   r,   r     r  )r   r   r6   rC  r   r   r   r?  rc  r  assertListEqualr  r  )r:   $prefetch_images_and_small_renditionspagesr  r  r+   r+   r,   /test_prefetch_renditions_on_non_image_querysets  s0   	zFTestPrefetchRenditions.test_prefetch_renditions_on_non_image_querysetsN)r(   r)   r*   r   r;   r  r+   r+   r+   r,   r    s    r  c                   @   *   e Zd ZdgZdd Zdd Zdd ZdS )	TestUsageCountr  c                 C   r!  r"  r5   r9   r+   r+   r,   r;        zTestUsageCount.setUpc                 C   s   |  | j  d d S rf   )r=   r8   	get_usagecountr9   r+   r+   r,   test_unused_image_usage_count     z,TestUsageCount.test_unused_image_usage_countc                 C   sp   | j dd tjjdd}t }||_| j|_|  W d    n1 s&w   Y  | | j	 
 d d S )NTexecuterE  rV  r#   )captureOnCommitCallbacksr   r6   r   r    r  r8   rm   r=   r  r  r:   r  event_page_carousel_itemr+   r+   r,   $test_used_image_document_usage_count  s   
z3TestUsageCount.test_used_image_document_usage_countN)r(   r)   r*   r   r;   r  r  r+   r+   r+   r,   r    
    r  c                   @   r  )	TestGetUsager  c                 C   r!  r"  r5   r9   r+   r+   r,   r;     r  zTestGetUsage.setUpc                 C   s   |  t| j g  d S r<   )r=   r   r8   r  r9   r+   r+   r,   test_unused_image_get_usage  r  z(TestGetUsage.test_unused_image_get_usagec                 C   s   | j dd tjjdd}t }||_| j|_|  W d    n1 s&w   Y  | | j	 d t
 | | j	 d d t | | j	 d d t | | j	 d d d t d S )NTr  rE  rV  r   r#   )r  r   r6   r   r    r  r8   rm   rh   r  tupler   r   r   r  r+   r+   r,   "test_used_image_document_get_usage  s   
"z/TestGetUsage.test_used_image_document_get_usageN)r(   r)   r*   r   r;   r  r  r+   r+   r+   r,   r    r  r  c                   @   sB   e Zd ZdgZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dS )TestGetWillowImager  c                 C   r!  r"  r5   r9   r+   r+   r,   r;     r  zTestGetWillowImage.setUpc                 C   s<   | j  }| |t W d    d S 1 sw   Y  d S r<   )r8   get_willow_imagerh   WillowImage)r:   willow_imager+   r+   r,   !test_willow_image_object_returned  s   "z4TestGetWillowImage.test_willow_image_object_returnedc              	   C   s~   t jjdd}| t) |  |   W d    n1 s w   Y  W d    d S W d    d S 1 s8w   Y  d S )Nr#   rV  )r   r6   r   ro   r   r  fail)r:   	bad_imager+   r+   r,   test_with_missing_image  s   

"z*TestGetWillowImage.test_with_missing_imagec                 C   sN   | j   | | j jj W d    n1 sw   Y  | | j jj d S r<   )r8   r  r@   r4   closedrD   r9   r+   r+   r,   test_closes_image  s   z$TestGetWillowImage.test_closes_imagec                 C   s`   z| j   | | j jj td1 sw   Y  W n	 ty%   Y nw | | j jj d S )NzSomething went wrong!)r8   r  r@   r4   r  
ValueErrorrD   r9   r+   r+   r,   test_closes_image_on_exception  s   z1TestGetWillowImage.test_closes_image_on_exceptionc                 C   sX   | j jd | j   W d    n1 sw   Y  | | j jj | j j  d S )Nrb)r8   r4   openr  r@   r  closer9   r+   r+   r,   test_doesnt_close_open_image'  s   z/TestGetWillowImage.test_doesnt_close_open_imageN)
r(   r)   r*   r   r;   r  r  r  r  r  r+   r+   r+   r,   r    s    
r  c                   @   s   e Zd ZdZdd ZdS )TestIssue573z
    This tests for a bug which causes filename limit on Renditions to be reached
    when the Image has a long original filename and a big focal point key
    c                 C   s*   t jjdtdddddd}|d d S )Nr.   zYthisisaverylongfilename-cdefghijklmnopqrstuvwxyzab-supercalifragilisticexpialidocious.pngi  )r3   r4   rU   rV   rW   rX   zfill-800x600)r   r6   r7   r$   r   ru   r+   r+   r,   test_issue_5739  s   zTestIssue573.test_issue_573N)r(   r)   r*   __doc__r  r+   r+   r+   r,   r  3  s    r  elasticsearch) _WAGTAILSEARCH_FORCE_AUTO_UPDATEc                   @   s<   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd ZdS )TestIssue613c                 C   s4   ddl m} ddlm} d|jvrtd|dS )Nr   r   )get_search_backendr  zNo elasticsearch backend active)django.confr   wagtail.search.backendsr  WAGTAILSEARCH_BACKENDSunittestSkipTest)r:   r   r  r+   r+   r,   get_elasticsearch_backendM  s
   

z&TestIssue613.get_elasticsearch_backendc                 C   s   |   | _|   d S r<   )r  search_backendloginr9   r+   r+   r,   r;   W  s   
zTestIssue613.setUpc                 K   s   dt dt j d}|| | jtd|}| |td t	j
jdd}| | d | }| |jd | |jd	 |S )
Nr.   ztest.pngr2   zwagtailimages:addwagtailimages:indexr   r#   rG   rH   )r   r$   r4   getvalueupdateclientpostr   assertRedirectsr   r6   rc  r=   r  rK  r&  r'  )r:   params	post_dataresponser  r8   r+   r+   r,   r   [  s   
zTestIssue613.add_imagec                 K   sv   t jjdt d| _ddi}|| | jtd| jj	fd|}| 
|td t jj| jj	d}| |jd |S )	Nr.   r2   r3   Editedzwagtailimages:edit)argsr  rV  )r   r6   r7   r$   r8   r  r  r  r   rW  r  r   r=   r3   )r:   r  r  r  r8   r+   r+   r,   
edit_images  s   
zTestIssue613.edit_imagec                 C   b   | j   | j t | jdd}| j   | j dt}| t|d | |d j	|j	 d S Nhello)r   r#   r   )
r  reset_indexadd_typer   r   refresh_indexr~   r=   lenrW  r   r+   r+   r,   test_issue_613_on_add     

z"TestIssue613.test_issue_613_on_addc                 C   r  r  )
r  r  r  r   r  r  r~   r=   r  rW  r   r+   r+   r,   test_issue_613_on_edit  r  z#TestIssue613.test_issue_613_on_editN)	r(   r)   r*   r  r;   r   r  r  r  r+   r+   r+   r,   r  K  s    
r  c                   @   s   e Zd Zdd ZdS )TestIssue312c              	   C   s\   t jjdt d}|d}|d}| || | jttjj|j	|j
|j|j|jd d S )Nr.   r2   zfill-100x100)r8   r(  r&  r'  focal_point_key)r   r6   r7   r$   r   r=   ro   r   r   r8   r(  r&  r'  r  )r:   r8   rend1rend2r+   r+   r,   test_duplicate_renditions  s    


z&TestIssue312.test_duplicate_renditionsN)r(   r)   r*   r  r+   r+   r+   r,   r    s    r  c                   @   s    e Zd ZdZdd Zdd ZdS )TestFilenameReductionzi
    This tests for a bug which results in filenames without extensions
    causing an infinite loop
    c                 C   0   t jjdtdd}|  | d|jj d S )Nr.   z^thisisaverylongfilename-defghijklmnopqrstuvwxyzabc-supercalifragilisticexpialidocioussuperlongr2   z^original_images/thisisaverylongfilename-defghijklmnopqrstuvwxyzabc-supercalifragilisticexpialir   r6   r7   r$   rm   r=   r4   r   ru   r+   r+   r,   test_filename_reduction_no_ext  s   z4TestFilenameReduction.test_filename_reduction_no_extc                 C   r   )Nr.   zbthisisaverylongfilename-efghijklmnopqrstuvwxyzabcd-supercalifragilisticexpialidocioussuperlong.pngr2   z^original_images/thisisaverylongfilename-efghijklmnopqrstuvwxyzabcd-supercalifragilisticexp.pngr  ru   r+   r+   r,   test_filename_reduction_ext  s   z1TestFilenameReduction.test_filename_reduction_extN)r(   r)   r*   r  r  r  r+   r+   r+   r,   r    s    r  c                   @   sX   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd ZdS )TestRenditionOrientationa  
    This tests for a bug where images with exif orientations which
    required rotation for display were cropped and sized based on the
    unrotated image dimensions.

    For example images with specified dimensions of 640x450 but an exif orientation of 6
    should appear as a 450x640 portrait, but instead were still cropped to 640x450.

    Actual image files are used so that exif orientation data will exist for the rotation to function correctly.
    c                 C   s   ddl m} | }||}W d   n1 sw   Y  | | d |jdd}| j	|d ddd	 | j	|d
 ddd	 | j	|d ddd	 |jdd}| j	|d ddd	 | j	|d
 ddd	 | j	|d ddd	 dS )z
        Check that the image has the correct colored pixels in the right places
        so that we know the image did not physically rotate.
        r   )PillowImageN)X    RGB)   i     r  )deltar#   &   r   .   )iy  i  U   ]   A   )
willow.plugins.pillowr  r  r  r=   get_sizer8   convertgetpixelassertAlmostEqual)r:   r*  r  r  r8   r1   r+   r+   r,   -assert_orientation_landscape_image_is_correct  s   
zFTestRenditionOrientation.assert_orientation_landscape_image_is_correctc                 C      t dd}tjjdt|d}W d    n1 sw   Y  | |jd | |jd |d}| |jd | |jd | 	| d S )Nz0wagtail/images/tests/image_files/landscape_1.jpgr  r.   r2   r  r  r4  
r  r   r6   r7   r   r=   r&  r'  r   r  r:   fr8   r*  r+   r+   r,   test_jpeg_with_orientation_1     
z5TestRenditionOrientation.test_jpeg_with_orientation_1c                 C   r  )Nz0wagtail/images/tests/image_files/landscape_2.jpgr  r.   r2   r  r  r4  r  r  r+   r+   r,   test_jpeg_with_orientation_2  r  z5TestRenditionOrientation.test_jpeg_with_orientation_2c                 C   r  )Nz0wagtail/images/tests/image_files/landscape_3.jpgr  r.   r2   r  r  r4  r  r  r+   r+   r,   test_jpeg_with_orientation_3-  r  z5TestRenditionOrientation.test_jpeg_with_orientation_3c                 C   r  )Nz0wagtail/images/tests/image_files/landscape_4.jpgr  r.   r2   r  r  r4  r  r  r+   r+   r,   test_jpeg_with_orientation_4;  r  z5TestRenditionOrientation.test_jpeg_with_orientation_4c                 C      t dd}tjjdt|d}W d    n1 sw   Y  | |jd | |jd |d}| |jd | |jd | 	| d S Nz0wagtail/images/tests/image_files/landscape_6.jpgr  r.   r2   r  r  r4  r  r  r+   r+   r,   test_jpeg_with_orientation_5L  r  z5TestRenditionOrientation.test_jpeg_with_orientation_5c                 C   r   r!  r  r  r+   r+   r,   test_jpeg_with_orientation_6Z  r  z5TestRenditionOrientation.test_jpeg_with_orientation_6c                 C   r   )Nz0wagtail/images/tests/image_files/landscape_7.jpgr  r.   r2   r  r  r4  r  r  r+   r+   r,   test_jpeg_with_orientation_7h  r  z5TestRenditionOrientation.test_jpeg_with_orientation_7c                 C   r   )Nz0wagtail/images/tests/image_files/landscape_8.jpgr  r.   r2   r  r  r4  r  r  r+   r+   r,   test_jpeg_with_orientation_8v  r  z5TestRenditionOrientation.test_jpeg_with_orientation_8N)r(   r)   r*   r  r  r  r  r  r  r"  r#  r$  r%  r+   r+   r+   r,   r    s    r  )Gr  r  r   django.contrib.auth.modelsr   r   django.core.cacher   django.core.filesr   django.core.files.storager   r	   r
   django.core.files.uploadedfiler   django.db.modelsr   django.db.utilsr   django.testr   r   r   r   django.urlsr   willow.imager   r  wagtail.images.modelsr   r   r   r   r   r   wagtail.images.rectr   wagtail.modelsr   r   r   r   wagtail.test.testapp.modelsr   r    r!   wagtail.test.utilsr"   utilsr$   r%   r&   r'   r-   r|   r   r   r   r  r  r  r  r  r  r  r  r  r  r  r+   r+   r+   r,   <module>   sZ     zB01V\
   
75
`'