
    !Wh                         d dl Z d dlZd dlmZmZ d dlmZ d dlmZ d dl	m
Z
 d dlZ ej                  e      ZddgZd Zd	 Zd
 Z ee      d        Z ee      d        Zy)    N)
pre_deletepre_save)receiver)CKEditor5Field)get_django_storage_class!cleanup_ckeditor_images_on_delete(cleanup_unused_ckeditor_images_on_updatec                 6    | sg S t        j                  d|       S )zN
    Extracts image paths from the HTML content stored in CKEditor5Field.
    z"<img[^>]*\ssrc=["\']([^"\']+)["\'])refindall)html_contents    ]/var/www/html/grow-django/grow/venv/lib/python3.12/site-packages/django_ckeditor_5/signals.pyextract_image_pathsr      s     	::;\JJ    c                 R   t        | d      r|D ]  }t        j                  j                  |      }t        j                  j	                  | j
                  |      }t        j                  j                  |      slt        j                  |        yt        j                  d       y)z?
    Deletes images from disk based on the provided paths.
    locationzCStorage does not have 'location' attribute, skipping file deletion.N)
hasattrospathbasenamejoinr   existsremoveloggerwarning)storageimage_pathsimg_path	file_nameabs_paths        r   delete_imagesr!      sx     w
## 	$H((2Iww||G$4$4i@Hww~~h'		(#		$ 	Q	
r   c                      	 t               }  |        S # t        $ r"}t        j                  d|        Y d}~yd}~ww xY w)z;Safely returns a storage instance based on Django settings.z$Could not initialize storage class: N)r   	Exceptionr   error)storage_classes     r   get_safe_storager'   -   s>    02 ;A3?@s    	>9>c           
         t        d |j                  j                  D              sy	 t               }|syg }|j                  j                  D ]B  }t	        |t
              s|j                  t        t        ||j                  d                   D 	 t        ||       y# t        $ r"}t        j                  d|        Y d}~yd}~ww xY w# t        $ r"}t        j                  d|        Y d}~yd}~ww xY w)z
    Removes images from disk when an object is deleted.
    If an error occurs, it is logged, but the deletion process continues.
    c              3   <   K   | ]  }t        |t                y wN
isinstancer   .0fs     r   	<genexpr>z4cleanup_ckeditor_images_on_delete.<locals>.<genexpr>=        Lz!^,L   N z,Failed to delete images on object deletion: z,Error in cleanup_ckeditor_images_on_delete: )any_metafieldsr'   r,   r   extendr   getattrnamer!   r#   r   r   r$   )senderinstancekwargsr   images_to_deletefieldr&   s          r   r   r   7   s     Lhnn6K6KLLI"$ ^^** 	E%0 '''%**b(IJ		O'#34 	ONNI!MNN	O ICA3GHHIsF   C +C "1C B! !	C*CC CC 	C:C55C:c           
         t        d |j                  j                  D              sy	 t               }|sy|j                  j                  D ]  }t	        |t
              s	 | j                  j                  |j                        }t        t        t        ||j                  d                  }t        t        t        ||j                  d                  }||z
  }	 t        ||        y# | j                  $ r t               }Y Yw xY w# t        $ r"}	t         j#                  d|	        Y d}	~	d}	~	ww xY w# t        $ r"}	t         j%                  d|	        Y d}	~	yd}	~	ww xY w)z
    Removes unused images when an object is updated.
    If any unexpected error occurs, it will be logged, but the deletion process won't break the update.
    c              3   <   K   | ]  }t        |t                y wr*   r+   r-   s     r   r0   z;cleanup_unused_ckeditor_images_on_update.<locals>.<genexpr>Z   r1   r2   N)pkr3   z Failed to delete unused images: z3Error in cleanup_unused_ckeditor_images_on_update: )r4   r5   r6   r'   r,   r   objectsgetrA   setr   r8   r9   DoesNotExistr!   r#   r   r   r$   )
r:   r;   r<   r   r>   old_instance
old_images
new_imagesunused_imagesr&   s
             r   r	   r	   T   sA    Lhnn6K6KLLP"$^^** 	KE%0'#)>>#5#5#5#EL!$+GL%**b,QR"J !!4WXuzzSU5V!WX
 *Z 7K!'=9	K ** '!$J' ! KNN%EaS#IJJK PJ1#NOOPsk   D; )D; !AC.0.D; D+D; .D
D; 	D

D; 	D8D3.D; 3D88D; ;	E&E!!E&)r   r   django.db.models.signalsr   r   django.dispatchr   django_ckeditor_5.fieldsr   django_ckeditor_5.storage_utilsr   logging	getLogger__name__r   __all__r   r!   r'   r   r	    r   r   <module>rS      s    	 	 9 $ 3 D 			8	$ (.K
  
*I I8 
(P Pr   