B
    ÒWvgj  ã               @   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   úC/tmp/pip-install-4y76y3dr/django-cleanup/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 |¡sRy| jj|j d�}W n | jk
rB   dS X tj||d� dS )zEFallback to the database to remake the cleanup cache if there is noneN)Úpk)Úsource)r   r   Z	has_cacheZobjectsÚgetZDoesNotExistr   )r   r   ÚrawÚupdate_fieldsÚusingr   Zdb_instancer   r   r   Úfallback_pre_save   s    r   c       
      K   sh   |rdS |sZxLt  |¡D ]>\}}|dks0||krt  ||¡}	|	|krt| |||	|dƒ qW t  |¡ dS )z;Post_save on all models with file fields, deletes old filesNÚupdated)r   Úfields_for_model_instanceZget_field_attrÚdelete_filer   )
r   r   r   Úcreatedr   r   r   Ú
field_nameZnew_fileZold_filer   r   r   Údelete_old_post_save&   s    r#   c             K   s.   x(t  |¡D ]\}}t| ||||dƒ qW dS )z=Post_delete on all models with file fields, deletes all filesÚdeletedN)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ƒs„t 
|ˆ¡ƒ ˆ_|dk|ˆˆj |ˆˆ|dkdœ‰ ‡ ‡‡‡‡fdd„}t||ƒ dS )	zDeletes a fileNÚfieldÚstorager$   r   )r$   Ú
model_namer"   Ú	file_nameZdefault_file_nameÚfiler   r   c           
      s�   t jf dˆiˆ —Ž d} d }yˆjdd� d} W nB tk
rr } z$|}ˆj}t dˆ|j|jˆ¡ W d d }~X Y nX t	jf ˆ|| dœˆ —Ž d S )Nr   F)ÚsaveTzAThere was an exception deleting the file `%s` on field `%s.%s.%s`)r   ÚerrorÚsuccess)
r
   ÚsendÚdeleteÚ	ExceptionZ_metaÚloggerÚ	exceptionZ	app_labelr)   r	   )r.   r-   ÚexÚopts)Úeventr"   r%   r   r   r   r   Úrun_on_commiti   s    "z"delete_file.<locals>.run_on_commit)Únamer   r   r   Úget_model_nameÚhasattrÚ	get_fieldr'   ÚcallableÚdefaultZget_field_storager(   r   )	r   r   r"   r%   r   Úreasonr)   r=   r7   r   )r6   r"   r%   r   r   r   r    =   s,    



r    c              C   s~   xxt  ¡ 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
W dS )z%Connect signals to the cleanup modelsz{{}}_django_cleanup_{}r   )r   Zdispatch_uidr   r   r   N)r   Zcleanup_modelsÚformatr9   r   Úconnectr   r   r   r   r#   r   r&   )ÚmodelÚkeyr   r   r   r@   {   s    r@   )r   ÚloggingZdjango.db.models.signalsr   r   r   r   Zdjango.db.transactionr   Ú r   Zsignalsr	   r
   Ú	getLoggerr   r2   r   r   r   r#   r&   r    r@   r   r   r   r   Ú<module>   s   
>