o
    ʷ6i*                     @   s\   d dl Z d dlmZ d dlmZmZmZ d dlmZm	Z	 dddZ
dd	 ZG d
d dZdS )    N)settings)ThumbnailErrordeserialize	serialize)deserialize_image_fileserialize_image_fileimagec                 C   s   d tj|| gS )z"
    Adds prefixes to the key
    ||)joinr   THUMBNAIL_KEY_PREFIX)keyidentity r   a/var/www/hoanhtaovolam_webdjango/env/lib/python3.10/site-packages/sorl/thumbnail/kvstores/base.py
add_prefix   s   r   c                 C   s   |  dd S )z'
    Removes prefixes from the key
    r	   )split)r   r   r   r   
del_prefix   s   r   c                   @   s   e Zd Zdd Zdd Zd&ddZdd	 Zd'ddZdd Zdd Z	dd Z
dd Zd(ddZd(ddZd(ddZd(ddZdd Zd d! Zd"d# Zd$d% ZdS ))KVStoreBasec                 C   s$   t | ddstjdtdd d S d S )N_cached_db_kvstoreFzUsing any other KVStore than Cached Db KVStore is deprecated. Please configure the cache that suits your use case at Django level and set this cache alias in THUMBNAIL_CACHE.   )
stacklevel)getattrwarningswarnDeprecationWarning)selfr   r   r   __init__   s   
zKVStoreBase.__init__c                 C   s   |  |jS )zT
        Gets the ``image_file`` from store. Returns ``None`` if not found.
        )_getr   )r   
image_filer   r   r   get!   s   zKVStoreBase.getNc                 C   s|   |   | |j| |dur<| |std|j | j|jddp$g }t|}||j | j|jt	|dd dS dS )zi
        Updates store for the `image_file`. Makes sure the `image_file` has a
        size set.
        Nz>Cannot add thumbnails for source: `%s` that is not in kvstore.
thumbnailsr   )
set_size_setr   r    r   namer   setaddlist)r   r   sourcer!   r   r   r   r&   '   s   
zKVStoreBase.setc                 C   s$   |  |}|d ur|S | | |S N)r    r&   )r   r   cachedr   r   r   
get_or_set;   s
   

zKVStoreBase.get_or_setTc                 C   s   |r|  | | |j dS )z
        Deletes the reference to the ``image_file`` and deletes the references
        to thumbnails as well as thumbnail files if ``delete_thumbnails`` is
        `True``. Does not delete the ``image_file`` is self.
        N)delete_thumbnails_deleter   )r   r   r-   r   r   r   deleteB   s   
zKVStoreBase.deletec                 C   sX   | j |jdd}|r*|D ]}|  |}|r| |d |  q| j|jdd dS dS )zX
        Deletes references to thumbnails as well as thumbnail ``image_files``.
        r!   r"   FN)r   r   r/   r.   )r   r   thumbnail_keysr   	thumbnailr   r   r   r-   L   s   
zKVStoreBase.delete_thumbnailsc                 C   sH   | j ddD ]}| j|dd}|r!|D ]}| |}|r |  qqd S )Nr!   r"   )
_find_keysr   r/   )r   r   r0   r1   r   r   r   delete_all_thumbnail_files^   s   
z&KVStoreBase.delete_all_thumbnail_filesc                 C   s   | j ddD ]}| |}|r| s| | q| j ddD ];}| |}|rS| j|ddp0g }t|}|D ]}| |sC|| q7t|}|rS| j||dd q| j|dd qdS )a  
        Cleans up the key value store. In detail:
        1. Deletes all key store references for image_files that do not exist
           and all key references for its thumbnails *and* their image_files.
        2. Deletes or updates all invalid thumbnail keys
        r   r"   r!   N)	r2   r   existsr/   r&   remover(   r$   r.   )r   r   r   r0   thumbnail_keys_setthumbnail_keyr   r   r   cleanupg   s(   




zKVStoreBase.cleanupc                 C   s"   |  tj}|r| j|  dS dS )z
        Brutely clears the key value store for keys with THUMBNAIL_KEY_PREFIX
        prefix. Use this in emergency situations. Normally you would probably
        want to use the ``cleanup`` method instead.
        N)_find_keys_rawr   r   _delete_raw)r   all_keysr   r   r   clear   s   zKVStoreBase.clearr   c                 C   s0   |  t||}|sdS |dkrt|S t|S )z<
        Deserializing, prefix wrapper for _get_raw
        Nr   )_get_rawr   r   r   )r   r   r   valuer   r   r   r      s   zKVStoreBase._getc                 C   s0   |dkr	t |}nt|}| t||| dS )z:
        Serializing, prefix wrapper for _set_raw
        r   N)r   r   _set_rawr   )r   r   r>   r   sr   r   r   r$      s   
zKVStoreBase._setc                 C   s   |  t|| dS )z0
        Prefix wrapper for _delete_raw
        N)r:   r   )r   r   r   r   r   r   r.      s   zKVStoreBase._deletec                 c   s2    t d|}| |pg }|D ]}t|V  qdS )z:
        Finds and returns all keys for identity,
         N)r   r9   r   )r   r   prefixraw_keysraw_keyr   r   r   r2      s   
zKVStoreBase._find_keysc                 C      t  )zL
        Gets the value from keystore, returns `None` if not found.
        NotImplementedError)r   r   r   r   r   r=         zKVStoreBase._get_rawc                 C   rE   )z
        Sets value associated to key. Key is expected to be shorter than 200
        chars. Value is a ``unicode`` object with an unknown (reasonable)
        length.
        rF   )r   r   r>   r   r   r   r?      s   zKVStoreBase._set_rawc                 G   rE   )zD
        Deletes the keys. Silent failure for missing keys.
        rF   )r   keysr   r   r   r:      rH   zKVStoreBase._delete_rawc                 C   rE   )z,
        Finds all keys with prefix
        rF   )r   rB   r   r   r   r9      rH   zKVStoreBase._find_keys_rawr*   )Tr   )__name__
__module____qualname__r   r    r&   r,   r/   r-   r3   r8   r<   r   r$   r.   r2   r=   r?   r:   r9   r   r   r   r   r      s$    



	'





r   rJ   )r   sorl.thumbnail.confr   sorl.thumbnail.helpersr   r   r   sorl.thumbnail.imagesr   r   r   r   r   r   r   r   r   <module>   s    
