U
    ¿|e¿  ã                   @   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d1dd„Zd2d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d3d'd(„Zd)d*„ Zd+d,„ Zd-d.„ Z d/d0„ Z!dS )4zF 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   úQ/var/www/website-v5/atlas_env/lib/python3.8/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   sf   t rdS t ¡ D ]P}t|| ƒr qt|ƒ}t|ƒr2q|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)Úselect_modeÚ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k	r0t|tƒs&t‚| |¡}|D ]
}|V  q4dS )z-Get the filefields for a model if it has themN)r   r   r   r   ÚAssertionErrorÚ
difference)r!   ÚexcludeÚfieldsr"   r   r   r	   Úget_fields_for_model:   s    
r'   c                 c   sV   |dk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    r0   c                 C   s   t t|  | ƒS )z&Restore a storage from its dotted path)r   r    r/   r   r   r	   Úget_field_storagea   s    r1   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©r4   r   ©r   r   r   r	   r   n   s    r   c                 C   s   dj | jd�S )zOreturns a mangled attribute name specific to the model for ignore functionalityz1_{opt.model_name}__{opt.app_label}_cleanup_ignorer5   r7   r8   r   r   r	   Úget_mangled_ignores   s    r9   c                 C   s   dj | jd�S )zOreturns a mangled attribute name specific to the model for select functionalityz1_{opt.model_name}__{opt.app_label}_cleanup_selectr5   r7   r8   r   r   r	   Úget_mangled_selectx   s    r:   c                 C   s   | t kS )zCheck if a model has filefields)r   ©r!   r   r   r	   r   €   s    r   c                 C   s"   |rt | t| ƒƒ S t | t| ƒƒS )z"Check if a model should be ignored)Úhasattrr:   r9   )r   r   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    rA   c                 C   s(   |dk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
    
ÿrE   c                 C   s
   t | tƒS )z&Check if an instance has a cache on it)r<   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    rF   c                  c   s   t D ]} t | ¡V  qdS )z.Get all the models we have in the FIELDS cacheN)r   r   Ú	get_modelr;   r   r   r	   Úcleanup_models­   s    rH   c                   C   s   t  ¡ S )zGet a copy of the FIELDS cache)r   Úcopyr   r   r   r	   Úcleanup_fields³   s    rJ   )N)N)N)"Ú__doc__Úcollectionsr   Údjango.appsr   Ú	django.dbr   Údjango.utils.module_loadingr   r?   r
   r   r   r   r    r3   r   r   r'   r.   r0   r1   r   r   r9   r:   r   r   rA   rE   r=   rF   rH   rJ   r   r   r   r	   Ú<module>   s:   


