Ë
    T]jÆ  ã                   ó²   — d dl Z d dlZd dlZd dlmZ d dlm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mZ d d	lmZ  G d
„ d«      Zdd„Z G d„ de«      Zy)é    N)Údate)ÚBaseCommand)ÚdatetimeÚ	timedelta)Úsettings)ÚSource)Úget_storage)Úget_storage_hash)Ústoragesc                   óT   — e Zd ZdZdZdZdZdZdZd„ Z	d„ Z
d„ Zd„ Zd„ Z	 	 dd	„Zd
„ Zy)ÚThumbnailCollectionCleanerzL
    Remove thumbnails and DB references to non-existing source images.
    r   c                 ó    — || _         || _        y ©N)ÚstdoutÚstderr)Úselfr   r   s      úl/var/www/zogu/zoguvenv/lib/python3.12/site-packages/easy_thumbnails/management/commands/thumbnail_cleanup.pyÚ__init__z#ThumbnailCollectionCleaner.__init__   s   € ØˆŒØˆ�ó    c                 óÊ   — t        |d«      r*t        j                  j                  |j                  |«      S t        j                  j                  t
        j                  |«      S )NÚlocation)ÚhasattrÚosÚpathÚjoinr   r   Ú
MEDIA_ROOT)r   r   Ústorages      r   Ú_get_absolute_pathz-ThumbnailCollectionCleaner._get_absolute_path   sB   € Ü�7˜JÔ'Ü—7‘7—<‘< × 0Ñ 0°$Ó7Ð7ä—7‘7—<‘<¤× 3Ñ 3°TÓ:Ð:r   c                 ó^   — t         j                  j                  |t        j                  «      S r   )r   r   Úrelpathr   r   )r   r   s     r   Ú_get_relative_pathz-ThumbnailCollectionCleaner._get_relative_path$   s   € Ü�w‰w�‰˜t¤X×%8Ñ%8Ó9Ð9r   c                 óÜ   — 	 |j                  |«      S # t        $ rO}| j                  j                  dj	                  |«      «       | j                  j                  |«       Y d }~y d }~ww xY w)Nz3Something went wrong when checking existance of {}:)ÚexistsÚ	Exceptionr   ÚwriteÚformat)r   r   r   Úes       r   Ú_check_if_existsz+ThumbnailCollectionCleaner._check_if_exists'   sZ   € ð	!Ø—>‘> $Ó'Ð'øÜò 	!Ø�K‰K×ÑÐS×ZÑZÐ[_Ó`ÔaØ�K‰K×Ñ˜a× Ñ ûð	!ús   ‚ “	A+œAA&Á&A+c                 ó|   — t         j                  j                  «       j                  |¬«      j	                  «        y )N)Úid__in)r   ÚobjectsÚallÚfilterÚdelete)r   Úidss     r   Ú_delete_sources_by_idz0ThumbnailCollectionCleaner._delete_sources_by_id.   s)   € Ü�‰×ÑÓ×#Ñ#¨3Ð#Ó/×6Ñ6Õ8r   Nc                 ó.  — |r| j                   j                  d«       |s
t        «       }t        j                  j                  «       D �ci c]  }t        t        |   «      |“Œ }}g }t        j                  «       }	t        j                  j                  «       }
|dkD  r5t        j                  «       }|
j                  |t        |¬«      z
  |f¬«      }
|r|
j                  |¬«      }
t!        |
«      D �]â  }|j#                  |j$                  «      }|r	t        |   nd}|sE| j                   j                  d|j$                  › d�«       | j                   j                  d	«       Œs| xj&                  d
z  c_        | j)                  |j*                  |«      }| j-                  ||«      �s|dkD  r*| j                   j                  dj/                  |«      «       | xj0                  d
z  c_        |j3                  |j4                  «       |j6                  j                  «       D ]‰  }| xj8                  d
z  c_        | j)                  |j*                  |«      }| j-                  ||«      sŒG|s|j;                  |«       |dkD  sŒ`| j                   j                  dj/                  |«      «       Œ‹ t=        |«      dk\  s�ŒÌ|r�ŒÐ| j?                  |«       g }�Œå |s| j?                  |«       tA        t        j                  «       |	z
  «      | _!        yc c}w )z¶
        Iterate through sources. Delete database references to sources
        not existing, including its corresponding thumbnails (files and
        database references).
        z
Dry run...r   )Údays)Úmodified__range)Úname__startswithNzSource storage hash (z) not found in STORAGESz/Can't determine source storage, skipping sourceé   zSource not present: {}zDeleting thumbnail: {}éè  )"r   r%   r	   r   ÚSTORAGESÚkeysr
   r   Útimer   r+   r,   r   Útodayr-   r   Úqueryset_iteratorÚgetÚstorage_hashÚsourcesr   Únamer(   r&   Úsource_refs_deletedÚappendÚidÚ
thumbnailsÚthumbnails_deletedr.   Úlenr0   ÚroundÚexecution_time)r   Údry_runÚ	verbosityÚlast_n_daysÚcleanup_pathr   ÚaliasÚstorage_hash_mapÚsources_to_deleteÚ
time_startÚqueryr:   ÚsourceÚsource_storage_aliasÚsource_storageÚabs_source_pathÚthumbÚabs_thumbnail_paths                     r   Úclean_upz#ThumbnailCollectionCleaner.clean_up1   s   € ñ Ø�K‰K×Ñ˜lÔ+áÜ!“mˆGäRZ×RcÑRc×RhÑRhÓRjÖkÈÔ,¬X°e©_Ó=¸uÑDÐkÐÐkØÐÜ—Y‘Y“[ˆ
ä—‘×"Ñ"Ó$ˆØ˜Š?Ü—J‘J“LˆEØ—L‘LØ!&¬¸Ô)DÑ!DÀeÐ Lð !ó NˆEáØ—L‘L°,�LÓ?ˆEä'¨Ó.ó 	'ˆFØ#3×#7Ñ#7¸×8KÑ8KÓ#LÐ Ù?SœXÐ&:Ò;ÐY]ˆNÙ!Ø—‘×!Ñ!Ð$9¸&×:MÑ:MÐ9NÐNeÐ"fÔgØ—‘×!Ñ!Ð"SÔTØà�LŠL˜AÑ�LØ"×5Ñ5°f·k±kÀ>ÓRˆOà×(Ñ(¨¸ÕIØ˜q’=Ø—K‘K×%Ñ%Ð&>×&EÑ&EÀoÓ&VÔWØ×(Ò(¨AÑ-Õ(Ø!×(Ñ(¨¯©Ô3à#×.Ñ.×2Ñ2Ó4ò c�EØ×+Ò+¨qÑ0Õ+Ø)-×)@Ñ)@ÀÇÁÈWÓ)UÐ&à×,Ñ,¨WÐ6HÕIÙ&Ø#ŸN™NÐ+=Ô>Ø$ q›=Ø ŸK™K×-Ñ-Ð.F×.MÑ.MÐN`Ó.aÕbðcô Ð$Ó%¨Ô-³gØ×*Ñ*Ð+<Ô=Ø$&Ò!ð;	'ñ> Ø×&Ñ&Ð'8Ô9Ü#¤D§I¡I£K°*Ñ$<Ó=ˆÕùò[ ls   Á
Lc                 ó@  — | j                   j                  dj                  t        j                  «       j                  d«      «      «       | j                   j                  dj                  d| j                  «      «       | j                   j                  dj                  d| j                  «      «       | j                   j                  dj                  d| j                  «      «       | j                   j                  dj                  | j                  «      «       y)	z?
        Print statistics about the cleanup performed.
        z{0:-<48}z%Y-%m-%d %H:%M z{0:<40} {1:>7}zSources checked:z"Source references deleted from DB:zThumbnails deleted from disk:z(Completed in {} seconds)
N)
r   r%   r&   r   ÚnowÚstrftimer>   r@   rD   rG   )r   s    r   Úprint_statsz&ThumbnailCollectionCleaner.print_statsm   sÙ   € ð 	�‰×ÑØ×ÑœhŸl™l›n×5Ñ5Ð6GÓHÓIô	Kà�‰×ÑÐ*×1Ñ1Ð2DÀdÇlÁlÓSÔTØ�‰×ÑÐ*×1Ñ1Ø0°$×2JÑ2JóLô 	Mà�‰×ÑÐ*×1Ñ1Ð2QØ$(×$;Ñ$;ó=ô 	>à�‰×ÑÐ7×>Ñ>¸t×?RÑ?RÓSÕTr   )Fr5   r   NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r>   rC   rD   r@   rG   r   r   r!   r(   r0   rW   r[   © r   r   r   r      sS   „ ñð €GØ€JØÐØÐØ€Nòò;ò:ò!ò9ð @AØ,0ó:>óxUr   r   c              #   ó$  K  — | j                  «       r|d}| j                  d«      d   j                  }| j                  d«      } ||k  rE| j                  |¬«      d| D ]  }|j                  }|–— Œ t	        j
                  «        ||k  rŒDyyy­w)z‰
    The queryset iterator helps to keep the memory consumption down.
    And also making it easier to process for weaker computers.
    r   z-pkÚpk)Úpk__gtN)r#   Úorder_byrb   r-   ÚgcÚcollect)ÚquerysetÚ	chunksizeÚprimary_keyÚlast_pkÚrows        r   r;   r;   {   s“   è ø€ ð
 ‡�ÔØˆØ×#Ñ# EÓ*¨1Ñ-×0Ñ0ˆØ×$Ñ$ TÓ*ˆØ˜GÒ#Ø—‘¨k�Ó:¸:¸IÐFò �Ø!Ÿf™f�Ø“	ðô �J‰JŒLð	 ˜GÕ#ð	 ùs   ‚B
BÂBc                   ó   — e Zd ZdZd„ Zd„ Zy)ÚCommandz: Deletes thumbnails that no longer have an original file. c                 óš   — |j                  ddddd¬«       |j                  ddd	d
t        d¬«       |j                  dddt        d¬«       y )Nz	--dry-runÚ
store_truerH   FzDry run the execution.)ÚactionÚdestÚdefaultÚhelpz--last-n-daysÚstorerJ   r   z8The number of days back in time to clean thumbnails for.)rp   rq   rr   Útypers   z--pathrK   zSpecify a path to clean up.)rp   rq   ru   rs   )Úadd_argumentÚintÚstr)r   Úparsers     r   Úadd_argumentszCommand.add_argumentsŽ   sp   € Ø×ÑØØØØØ)ð 	ô 	+ð 	×ÑØØØØÜØKð 	ô 	Mð 	×ÑØØØÜØ.ð 	õ 	0r   c           
      ó0  — t        | j                  | j                  «      }|j                  |j	                  dd«      t        |j	                  dd«      «      t        |j	                  dd«      «      |j	                  d«      ¬«       |j                  «        y )	NrH   FrI   r5   rJ   r   rK   )rH   rI   rJ   rK   )r   r   r   rW   r<   rw   r[   )r   ÚargsÚoptionsÚtccs       r   ÚhandlezCommand.handle£   ss   € Ü(¨¯©°d·k±kÓBˆØ�‰Ø—K‘K 	¨5Ó1Ü˜'Ÿ+™+ k°1Ó5Ó6Ü˜GŸK™K¨°qÓ9Ó:Ø Ÿ™ ^Ó4ð	 	ô 	6ð
 	�‰Õr   N)r\   r]   r^   rs   rz   r   r`   r   r   rm   rm   ‹   s   „ ØK€Dò0ó*r   rm   )r6   )re   r   r9   r   r   Údjango.core.management.baser   Údjango.utils.timezoner   Úeasy_thumbnails.confr   Úeasy_thumbnails.modelsr   Úeasy_thumbnails.storager	   Úeasy_thumbnails.utilsr
   Údjango.core.files.storager   r   r;   rm   r`   r   r   ú<module>r‡      sF   ðÛ 	Û 	Û Ý å 3ß 5å )Ý )Ý /Ý 2Ý .÷hUñ hUóVô ˆkõ r   