U
    {hm                     @   sT   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Z
G dd deZd	S )
    )apps)BaseCommand)transaction)ReferenceIndex)#disable_reference_index_auto_updatei  c                   @   sF   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zej	e
fddZdS )Commandc                 O   s   | j dkr| jj|| dS )zK
        Helper function that writes based on verbosity parameter

        r   N)	verbositystdoutwrite)selfargskwargs r   |/var/www/dating/data/www/fatepal.com/env/lib/python3.8/site-packages/wagtail/management/commands/rebuild_references_index.pyr
      s    
zCommand.writec                 C   s   |j dddttdd d S )Nz--chunk_sizestore
chunk_sizezHSet number of records to be fetched at once for inserting into the index)actiondestdefaulttypehelp)add_argumentDEFAULT_CHUNK_SIZEint)r   parserr   r   r   add_arguments   s    zCommand.add_argumentsc              
   K   s   |d | _ |d}d}| d t  t  tj }|j	|j
d W 5 Q R X t D ]j}t|snq^| t| | | |j d|D ]$}|D ]}t| q|t|7 }q|   q^W 5 Q R X | d|  |   d S )Nr   r   r   zRebuilding reference index)usingpkzIndexed %d objects)r   getr
   r   atomicr   r   objectsallZ_raw_deletedbr   Z
get_modelsZ
is_indexedstrprint_iter_progressqueryset_chunksZorder_byZcreate_or_update_for_objectlenprint_newline)r   optionsr   Zobject_countZall_referencesmodelchunkinstancer   r   r   handle   s*    





zCommand.handlec                 C   s   |  d d S )N )r
   )r   r   r   r   r'   A   s    zCommand.print_newlinec                 c   sv   t |ddD ]d\}}|V  | jddd |d dkrL|   | jddd n|d	 dkrf| jd
dd | j  qdS )aq  
        Print a progress meter while iterating over an iterable. Use it as part
        of a ``for`` loop::

            for item in self.print_iter_progress(big_long_list):
                self.do_expensive_computation(item)

        A ``.`` character is printed for every value in the iterable,
        a space every 10 items, and a new line every 50 items.
           )start.r-   )ending(   r   z#                                   
    N)	enumerater
   r'   r	   flush)r   iterableivaluer   r   r   r$   D   s    zCommand.print_iter_progressc                 c   s:   d}t ||| d d| }|s&q6|V  |d7 }qdS )a	  
        Yield a queryset in chunks of at most ``chunk_size``. The chunk yielded
        will be a list, not a queryset. Iterating over the chunks is done in a
        transaction so that the order and count of items in the queryset
        remains stable.
        r   Nr.   )list)r   qsr   r8   itemsr   r   r   r%   \   s    zCommand.queryset_chunksN)__name__
__module____qualname__r
   r   r,   r'   r$   r   r   r   r%   r   r   r   r   r      s   	
"r   N)Zdjango.appsr   Zdjango.core.management.baser   Z	django.dbr   Zwagtail.modelsr   Zwagtail.signal_handlersr   r   r   r   r   r   r   <module>   s   