a
    Dg                     @   st   d dl Z d dlZd dlZd dlmZ d dlmZ d dlmZ dd Z	dd Z
dd	d
ZdddZdd Zdd ZdS )    N)settings)constant_time_compare)	force_strc                    s$    fdd} fdd}||fS )Nc                    sZ   |   }|  dd}|jj}|j|D ](}|j|j|dd}|j|jd q,d S )NwagtailimagesFilter )filterfilter_specr	   )	get_model
connectionaliasobjectsusingr   updatespec)appsschema_editor	Renditionr   db_aliasZfltZ
renditionsapp_namerendition_model_name S/var/www/lab.imftr.de/x/nb_venv/lib/python3.9/site-packages/wagtail/images/utils.pyfill_filter_spec_forward   s    zAget_fill_filter_spec_migrations.<locals>.fill_filter_spec_forwardc           	         s   |   }|  dd}|jj}|j|jddjddd }|sHq|D ]8}|j|j|d\}}|j|j|dj	|d	 qLq d S )
Nr   r   T)Zfilter__isnullr	   )Zflat)r   r
   )r   )
r   r   r   r   r   r   Zvalues_listZdistinctZget_or_creater   )	r   r   r   r   r   Zunmatched_filter_specsr	   r   _r   r   r   fill_filter_spec_reverse   s(    	
zAget_fill_filter_spec_migrations.<locals>.fill_filter_spec_reverser   )r   r   r   r   r   r   r   get_fill_filter_spec_migrations   s    r   c                 C   s   t | dkrDt| d dd }t| d dd }t| d dd }nLt | dkrt| dd d}t| dd d}t| dd d}ntd	|||fS )
a  
    Parses a string a user typed into a tuple of 3 integers representing the
    red, green and blue channels respectively.

    May raise a ValueError if the string cannot be parsed.

    The colour string must be a CSS 3 or 6 digit hex code without the '#' prefix.
       r                     z:Color string must be either 3 or 6 hexadecimal digits long)lenint
ValueError)Zcolor_stringrgbr   r   r   parse_color_string:   s    	r,   c                 C   sR   |d u rt j}t|tr | }|  d| d}ttt	|| t
j S )N/)r   Z
SECRET_KEY
isinstancestrencoder   base64urlsafe_b64encodehmacnewhashlibsha1digest)image_idr	   keyurlr   r   r   generate_signatureQ   s    
r;   c                 C   s   t | t|||dS )N)r9   )r   r;   )	signaturer8   r	   r9   r   r   r   verify_signaturea   s    r=   c                 C   s$   | |d}|j| jdj| jdS )a  
    Finds all the duplicates of a given image.
    To keep things simple, two images are considered to be duplicates if they have the same `file_hash` value.
    This function also ensures that the `user` can choose one of the duplicate images returned (if any).
    choose)pk)	file_hash)Z!instances_user_has_permission_forexcluder?   r   r@   )imageuserZpermission_policyZ	instancesr   r   r   find_image_duplicatesg   s    rD   c                    s8   t | tr| d} g d  fdd| D }d|S )zJ
    Remove any directives that would require an SVG to be rasterised
    |)maxminwidthheightZscalefilloriginalc                    s&   g | ] t  fd dD r qS )c                 3   s   | ]}  |V  qd S )N)
startswith).0prefixxr   r   	<genexpr>       z.to_svg_safe_spec.<locals>.<listcomp>.<genexpr>)any)rM   Zsvg_preserving_specsrO   r   
<listcomp>   s   z$to_svg_safe_spec.<locals>.<listcomp>)r.   r/   splitjoin)Zfilter_specsZ
safe_specsr   rT   r   to_svg_safe_specr   s    

	
rX   )N)N)r1   r5   r3   Zdjango.confr   Zdjango.utils.cryptor   Zdjango.utils.encodingr   r   r,   r;   r=   rD   rX   r   r   r   r   <module>   s   .

