a
    Sic                     @   s   d Z ddl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 eeZG dd	 d	Zd
d Zdd Zdd Zdd Zdd Zdd ZdS )z0
    Signal handlers to manage FileField files.
    N)post_delete	post_init	post_savepre_save)	on_commit   )cache)cleanup_post_deletecleanup_pre_deletec                   @   s   e Zd ZdZdS )FakeInstancez;A Fake model instance to ensure an instance is not modifiedN)__name__
__module____qualname____doc__ r   r   S/var/www/html/django/DPS/env/lib/python3.9/site-packages/django_cleanup/handlers.pyr      s   r   c                 K   s   t | dS )z?Post_init on all models with file fields, saves original valuesN)r   make_cleanup_cache)senderinstancekwargsr   r   r   cache_original_post_init   s    r   c                 K   sV   |rdS |j rRt|sRz| jj|j d}W n | jyB   Y dS 0 tj||d dS )zEFallback to the database to remake the cleanup cache if there is noneN)pk)source)r   r   	has_cacheobjectsgetDoesNotExistr   )r   r   rawupdate_fieldsusingr   db_instancer   r   r   fallback_pre_save   s    r!   c           
      K   s`   |rdS |sRt |D ]:\}}|du s.||v rt ||}	|	|krt|||	| qt | dS )z;Post_save on all models with file fields, deletes old filesN)r   fields_for_model_instanceget_field_attrdelete_filer   )
r   r   r   createdr   r   r   
field_namenew_fileold_filer   r   r   delete_old_post_save&   s    r)   c                 K   s&   t |D ]\}}t|||| q
dS )z=Post_delete on all models with file fields, deletes all filesN)r   r"   r$   )r   r   r   r   r&   file_r   r   r   delete_all_post_delete7   s    r+   c                    s   j s
dS t _t}tds>t|  _ j_ tjj	sRjj	nj	 }j |krjdS tdst
|  _ fdd}t|| dS )zDeletes a fileNfieldstoragec               
      s`   t jd d zjdd W n. tyL   j} td| j| j  Y n0 t	jd d d S )N)r   fileF)savezAThere was an exception deleting the file `%s` on field `%s.%s.%s`)
r
   senddelete	Exception_metalogger	exception	app_label
model_namer	   )optsr&   r*   r   r   r   run_on_commit^   s    
z"delete_file.<locals>.run_on_commit)namer   r   r   get_model_namehasattr	get_fieldr,   callabledefaultget_field_storager-   r   )r   r&   r*   r   r7   r@   r:   r   r9   r   r$   =   s    



r$   c                  C   sz   t  D ]l} dt | }tjt| |dd tjt| |dd t	jt
| |dd tjt| |dd qdS )z%Connect signals to the cleanup modelsz{{}}_django_cleanup_{}r   )r   dispatch_uidr   r   r   N)r   cleanup_modelsformatr<   r   connectr   r   r!   r   r)   r   r+   )modelkeyr   r   r   rE   l   s    rE   )r   loggingZdjango.db.models.signalsr   r   r   r   Zdjango.db.transactionr    r   signalsr	   r
   	getLoggerr   r4   r   r   r!   r)   r+   r$   rE   r   r   r   r   <module>   s   
/