a
    Sick                     @   s   d Z ddlmZ ddlmZ ddlmZ ddlmZ dZ	dd Z
ee
Zd	d
 ZeeZeeZdZdd Zdd Zd/ddZd0d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d1d%d&Zd'd( Zd)d* Zd+d, Zd-d. Z dS )2zF Our local cache of filefields, everything is private to this package.    )defaultdict)apps)models)import_stringZ_django_cleanup_original_cachec                   C   s   t  S N)set r   r   P/var/www/html/django/DPS/env/lib/python3.9/site-packages/django_cleanup/cache.pyfields_default   s    r
   c                   C   s   i S r   r   r   r   r   r	   fields_dict_default   s    r   z'{klass.__module__}.{klass.__qualname__}c                  C   sd   t rdS t D ]N} t| rqt| }t|r0q| j}| D ]}t|t	j
r>t||j| q>qdS )z/Prepare the cache for all models, non-reentrantN)FIELDSr   
get_modelsignore_modelget_model_namemodel_has_filefields_meta
get_fields
isinstancer   	FileFieldadd_field_for_modelname)modelr   optsfieldr   r   r	   prepare   s    r   c                 C   s4   t |  | t|t|  |< t|jt|  |< dS )z2Centralized function to make all our local caches.N)r   addget_dotted_pathFIELDS_FIELDSstorageFIELDS_STORAGE)
model_name
field_namer   r   r   r	   r   ,   s    r   Nc                 c   sD   t | r@t|  }|dur0t|ts&J ||}|D ]
}|V  q4dS )z-Get the filefields for a model if it has themN)r   r   r   r   
difference)r    excludefieldsr!   r   r   r	   get_fields_for_model:   s    
r%   c                 c   sV   |du r| }t | }|  }t||dD ](}t| |d}||||j|jfV  q(dS )z
        Yields (name, descriptor) for each file field given an instance

        Can use the `using` kwarg to change the instance that the `FieldFile`
        will receive.
    N)r#   )r   get_deferred_fieldsr%   getattr	__class__r   r   )instanceusingr    deferred_fieldsr!   Z	fieldfiler   r   r	   fields_for_model_instanceG   s    r,   c                 C   s   t t|  | S )z$Restore a field from its dotted path)r   r   r    r!   r   r   r	   	get_field\   s    r.   c                 C   s   t t|  | S )z&Restore a storage from its dotted path)r   r   r-   r   r   r	   get_field_storagea   s    r/   c                 C   s   t j| jdS )z!get the dotted path for an object)klass)DOTTED_PATHformatr(   )Zobject_r   r   r	   r   i   s    r   c                 C   s   dj | jdS )zreturns a unique model namez {opt.app_label}.{opt.model_name}optr2   r   r   r   r   r	   r   n   s    r   c                 C   s   dj | jdS )z6returns a mangled attribute name specific to the modelz1_{opt.model_name}__{opt.app_label}_cleanup_ignorer3   r5   r6   r   r   r	   get_mangled_ignores   s    r7   c                 C   s   | t v S )zCheck if a model has filefields)r   r    r   r   r	   r   {   s    r   c                 C   s   t | t| S )z"Check if a model should be ignored)hasattrr7   r6   r   r   r	   r      s    r   c                 C   s   t | rt| t dS )z!Remove the cache from an instanceN)	has_cachedelattr
CACHE_NAMEr)   r   r   r	   remove_instance_cache   s    r>   c                 C   s(   |du r| }t | ttt|| d dS )z~
        Make the cleanup cache for an instance.

        Can also change the source of the data with the `source` kwarg.
    N)r*   )setattrr<   dictr,   )r)   sourcer   r   r	   make_cleanup_cache   s
    
rB   c                 C   s
   t | tS )z&Check if an instance has a cache on it)r9   r<   r=   r   r   r	   r:      s    r:   c                 C   s   t | t| S )z)Get a value from the cache on an instance)r'   r<   )r)   r!   r   r   r	   get_field_attr   s    rC   c                  c   s   t D ]} t| V  qdS )z.Get all the models we have in the FIELDS cacheN)r   r   	get_modelr8   r   r   r	   cleanup_models   s    rE   c                   C   s   t  S )zGet a copy of the FIELDS cache)r   copyr   r   r   r	   cleanup_fields   s    rG   )N)N)N)!__doc__collectionsr   django.appsr   	django.dbr   django.utils.module_loadingr   r<   r
   r   r   r   r   r1   r   r   r%   r,   r.   r/   r   r   r7   r   r   r>   rB   r:   rC   rE   rG   r   r   r   r	   <module>   s8   


