o
    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|i | dS dS )zK
        Helper function that writes based on verbosity parameter

        r   N)	verbositystdoutwrite)selfargskwargs r   t/var/www/html/ndineBlogger/venv/lib/python3.10/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 a t  tj }|j	|j
d W d    n1 s0w   Y  t D ]5}t|sAq9| t| | | |j d|D ]}|D ]}t| q[|t|7 }qW|   q9W d    n1 syw   Y  | d|  |   d S )Nr   r   r   zRebuilding reference index)usingpkzIndexed %d objects)r   getr
   r   atomicr   r   objectsall_raw_deletedbr   
get_models
is_indexedstrprint_iter_progressqueryset_chunksorder_bycreate_or_update_for_objectlenprint_newline)r   optionsr   object_countall_referencesmodelchunkinstancer   r   r   handle   s0   






zCommand.handlec                 C   s   |  d d S )N )r
   )r   r   r   r   r,   A   s   zCommand.print_newlinec                 c   sx    t |ddD ]2\}}|V  | jddd |d dkr'|   | jddd n|d	 dkr4| 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.r4   )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dS |V  |d7 }q)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   TNr5   )list)r   qsr   r?   itemsr   r   r   r(   \   s   zCommand.queryset_chunksN)__name__
__module____qualname__r
   r   r3   r,   r'   r   r   r   r(   r   r   r   r   r      s    	
"r   N)django.appsr   django.core.management.baser   	django.dbr   wagtail.modelsr   wagtail.signal_handlersr   r   r   r   r   r   r   <module>   s    