ó
¶qëcc           @   sÌ   d  d l  Z  d  d l Z d  d l Z d  d l m Z 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 d e f d	 „  ƒ  YZ d
 d „ Z d e f d „  ƒ  YZ d S(   iÿÿÿÿN(   t   datetimet   datet	   timedelta(   t   make_option(   t   get_storage_class(   t   BaseCommand(   t   settings(   t   Sourcet   ThumbnailCollectionCleanerc           B   sq   e  Z d  Z d Z d Z d Z d Z d Z d „  Z d „  Z	 d „  Z
 d „  Z e d d d	 d	 d „ Z d „  Z RS(
   sL   
    Remove thumbnails and DB references to non-existing source images.
    i    c         C   s   t  j j t j | ƒ S(   N(   t   ost   patht   joinR   t
   MEDIA_ROOT(   t   selfR
   (    (    s“   /Users/danaskwith/.local/share/virtualenvs/svtgbi-git-reqaUha4/lib/python2.7/site-packages/easy_thumbnails/management/commands/thumbnail_cleanup.pyt   _get_absolute_path   s    c         C   s   t  j j | t j ƒ S(   N(   R	   R
   t   relpathR   R   (   R   R
   (    (    s“   /Users/danaskwith/.local/share/virtualenvs/svtgbi-git-reqaUha4/lib/python2.7/site-packages/easy_thumbnails/management/commands/thumbnail_cleanup.pyt   _get_relative_path   s    c         C   s?   y | j  | ƒ SWn' t k
 r: } d | GHt | ƒ GHn Xd  S(   Ns3   Something went wrong when checking existance of %s:(   t   existst	   Exceptiont   str(   R   t   storageR
   t   e(    (    s“   /Users/danaskwith/.local/share/virtualenvs/svtgbi-git-reqaUha4/lib/python2.7/site-packages/easy_thumbnails/management/commands/thumbnail_cleanup.pyt   _check_if_exists   s
    	c         C   s#   t  j j ƒ  j d | ƒ j ƒ  d  S(   Nt   id__in(   R   t   objectst   allt   filtert   delete(   R   t   ids(    (    s“   /Users/danaskwith/.local/share/virtualenvs/svtgbi-git-reqaUha4/lib/python2.7/site-packages/easy_thumbnails/management/commands/thumbnail_cleanup.pyt   _delete_sources_by_id$   s    i   c         C   s  | r d GHn  | s) t  t j ƒ ƒ  } n  g  } t j ƒ  } t j j ƒ  } | d k rŠ t j ƒ  }	 | j	 d |	 t
 d | ƒ |	 f ƒ } n  | r¥ | j	 d | ƒ } n  x2t | ƒ D]$}
 |  j d 7_ |  j |
 j ƒ } |  j | | ƒ s§| d k rd | f GHn  |  j d 7_ | j |
 j ƒ x€ |
 j j ƒ  D]l } |  j d 7_ |  j | j ƒ } |  j | | ƒ r4| sƒ| j | ƒ n  | d k r d | f GHq q4q4Wn  t | ƒ d	 k r² | r² |  j | ƒ g  } q² q² W| sð|  j | ƒ n  t t j ƒ  | ƒ |  _ d
 S(   s¶   
        Iterate through sources. Delete database references to sources
        not existing, including its corresponding thumbnails (files and
        database references).
        s
   Dry run...i    t   modified__ranget   dayst   name__startswithi   s   Source not present:s   Deleting thumbnail:iè  N(   R   R   t   THUMBNAIL_DEFAULT_STORAGEt   timeR   R   R   R   t   todayR   R   t   queryset_iteratort   sourcesR   t   nameR   t   source_refs_deletedt   appendt   idt
   thumbnailst   thumbnails_deletedR   t   lenR   t   roundt   execution_time(   R   t   dry_runt	   verbosityt   last_n_dayst   cleanup_pathR   t   sources_to_deletet
   time_startt   queryR#   t   sourcet   abs_source_patht   thumbt   abs_thumbnail_path(    (    s“   /Users/danaskwith/.local/share/virtualenvs/svtgbi-git-reqaUha4/lib/python2.7/site-packages/easy_thumbnails/management/commands/thumbnail_cleanup.pyt   clean_up'   sF    	c         C   so   d j  t t j ƒ  j d ƒ ƒ ƒ GHd j  d |  j ƒ GHd j  d |  j ƒ GHd j  d |  j ƒ GHd |  j GHd S(	   s?   
        Print statistics about the cleanup performed.
        s   {0:-<48}s   %Y-%m-%d %H:%M s   {0:<40} {1:>7}s   Sources checked:s"   Source references deleted from DB:s   Thumbnails deleted from disk:s   (Completed in %s seconds)
N(	   t   formatR   R    t   nowt   strftimeR%   R'   R+   R.   (   R   (    (    s“   /Users/danaskwith/.local/share/virtualenvs/svtgbi-git-reqaUha4/lib/python2.7/site-packages/easy_thumbnails/management/commands/thumbnail_cleanup.pyt   print_stats[   s    #	N(   t   __name__t
   __module__t   __doc__R%   R*   R+   R'   R.   R   R   R   R   t   Falset   NoneR:   R>   (    (    (    s“   /Users/danaskwith/.local/share/virtualenvs/svtgbi-git-reqaUha4/lib/python2.7/site-packages/easy_thumbnails/management/commands/thumbnail_cleanup.pyR      s   					3iè  c         c   sŠ   |  j  ƒ  r† d } |  j d ƒ d j } |  j d ƒ }  xL | | k  r‚ x, |  j d | ƒ |  D] } | j } | Vq] Wt j ƒ  q: Wn  d S(   s‰   
    The queryset iterator helps to keep the memory consumption down.
    And also making it easier to process for weaker computers.
    i    s   -pkt   pkt   pk__gtN(   R   t   order_byRD   R   t   gct   collect(   t   querysett	   chunksizet   primary_keyt   last_pkt   row(    (    s“   /Users/danaskwith/.local/share/virtualenvs/svtgbi-git-reqaUha4/lib/python2.7/site-packages/easy_thumbnails/management/commands/thumbnail_cleanup.pyR$   i   s    		t   Commandc           B   sŸ   e  Z d  Z e e d d ƒ e d d d d d d e d d	 ƒe d
 d d d d d d d e d d ƒe d d d d d d e d d ƒf Z	 d „  Z
 d „  Z RS(   s:    Deletes thumbnails that no longer have an original file. t   option_lists	   --dry-runt   actiont
   store_truet   destR/   t   defaultt   helps   Dry run the execution.s   --last-n-dayst   storeR1   i    t   types8   The number of days back in time to clean thumbnails for.s   --pathR2   s   Specify a path to clean up.c         C   sy   | j  d d d d d d t d d ƒ| j  d	 d d
 d d d d d t d d ƒ| j  d d d
 d d d t d d ƒd  S(   Ns	   --dry-runRP   RQ   RR   R/   RS   RT   s   Dry run the execution.s   --last-n-daysRU   R1   i    RV   s8   The number of days back in time to clean thumbnails for.s   --pathR2   s   Specify a path to clean up.(   t   add_argumentRB   t   intR   (   R   t   parser(    (    s“   /Users/danaskwith/.local/share/virtualenvs/svtgbi-git-reqaUha4/lib/python2.7/site-packages/easy_thumbnails/management/commands/thumbnail_cleanup.pyt   add_arguments“   s&    c      
   O   sr   t  ƒ  } | j d | j d t ƒ d t | j d d ƒ ƒ d t | j d d ƒ ƒ d | j d ƒ ƒ | j ƒ  d  S(   NR/   R0   i   R1   i    R2   (   R   R:   t   getRB   RX   R>   (   R   t   argst   optionst   tcc(    (    s“   /Users/danaskwith/.local/share/virtualenvs/svtgbi-git-reqaUha4/lib/python2.7/site-packages/easy_thumbnails/management/commands/thumbnail_cleanup.pyt   handle¨   s    		(    (   R?   R@   RT   t   getattrR   R   RB   RX   R   RO   RZ   R_   (    (    (    s“   /Users/danaskwith/.local/share/virtualenvs/svtgbi-git-reqaUha4/lib/python2.7/site-packages/easy_thumbnails/management/commands/thumbnail_cleanup.pyRN   y   s.   	(   RG   R	   R"   R    R   R   t   optparseR   t   django.core.files.storageR   t   django.core.management.baseR   t   easy_thumbnails.confR   t   easy_thumbnails.modelsR   t   objectR   R$   RN   (    (    (    s“   /Users/danaskwith/.local/share/virtualenvs/svtgbi-git-reqaUha4/lib/python2.7/site-packages/easy_thumbnails/management/commands/thumbnail_cleanup.pyt   <module>   s   \