o
    hu                     @   sd   d dl 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Zdd	 ZG d
d deZdS )    N)settings)BaseCommand)transaction)get_search_backend)get_indexed_modelsi  c                    sl   i  t  }|D ]}| |}|r' |j| ||jg  ||j | qt  fdd| D S )a;  
    This takes a search backend and a list of models. By calling the
    get_index_for_model method on the search backend, it groups the models into
    the indices that they will be indexed into.

    It returns an ordered mapping of indices to lists of models within each
    index.

    For example, Elasticsearch 2 requires all page models to be together, but
    separate from other content types (eg, images and documents) to prevent
    field mapping collisions:

    >>> group_models_by_index(elasticsearch2_backend, [
    ...     wagtailcore.Page,
    ...     myapp.HomePage,
    ...     myapp.StandardPage,
    ...     wagtailimages.Image
    ... ])
    {
        <Index wagtailcore_page>: [wagtailcore.Page, myapp.HomePage, myapp.StandardPage],
        <Index wagtailimages_image>: [wagtailimages.Image],
    }
    c                    s   g | ]
\}} | |fqS  r   ).0
index_nameindex_modelsindicesr   o/var/www/html/ndineBlogger/venv/lib/python3.10/site-packages/wagtail/search/management/commands/update_index.py
<listcomp>1   s    
z)group_models_by_index.<locals>.<listcomp>)collectionsOrderedDictget_index_for_model
setdefaultnameappenditems)backendmodelsmodels_by_indexmodelindexr   r   r   group_models_by_index   s   

r   c                   @   sT   e Zd Zdd Zdef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 )z6Helper function that respects verbosity when printing.r   N)	verbositystdoutwrite)selfargskwargsr   r   r   r   9   s   
zCommand.writeFc              	   C   s6  |  d|  t|}|js|  d|  d S t|t  }|s(|  |d  |D ]n\}}|  |d|j   ||}| }|D ]}	||	 qCd}
|s|D ]5}	| j d	||	j
j|	jddd	 | | |	 d
|D ]}||	| |
t|7 }
qs|   qQ|  |  |d|
   |   q*d S )NzUpdating backend: z'Backend '%s' doesn't require rebuildingz: No indices to rebuildz: Rebuilding index %sr   z
{}: {}.{} #    endingpkz: indexed %d objects)r   r   rebuilder_classr   r   r   r   start	add_modelformat_meta	app_label__name__ljustprint_iter_progressqueryset_chunksget_indexed_objectsorder_by	add_itemslenprint_newlinefinish)r    backend_nameschema_only
chunk_sizer   models_grouped_by_indexr   r   	rebuilderr   object_countchunkr   r   r   update_backend>   sP   


zCommand.update_backendc                 C   sB   |j dddd dd |j dddd	d
d |j dddttdd d S )Nz	--backendstorer8   zSpecify a backend to update)actiondestdefaulthelpz--schema-only
store_truer9   Fz(Prevents loading any data into the indexz--chunk_sizer:   zHSet number of records to be fetched at once for inserting into the index)rA   rB   rC   typerD   )add_argumentDEFAULT_CHUNK_SIZEint)r    parserr   r   r   add_argumentsv   s,   
zCommand.add_argumentsc                 K   sf   |d | _ |d r|d g}nttdrtj }ndg}|D ]}| j||dd|dd qd S )	Nr   r8   WAGTAILSEARCH_BACKENDSrC   r9   Fr:   )r9   r:   )r   hasattrr   rL   keysr?   get)r    optionsbackend_namesr8   r   r   r   handle   s   


zCommand.handlec                 C   s   |  d d S )Nr$   )r   )r    r   r   r   r6      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.
           )r)   .r$   r%   (   r   z#                                   
    N)	enumerater   r6   r   flush)r    iterableivaluer   r   r   r0      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   TNrS   )list)r    qsr:   r[   r   r   r   r   r1      s   zCommand.queryset_chunksN)r.   
__module____qualname__r   rH   r?   rK   rR   r6   r0   r   atomicr1   r   r   r   r   r   8   s    
8r   )r   django.confr   django.core.management.baser   	django.dbr   wagtail.search.backendsr   wagtail.search.indexr   rH   r   r   r   r   r   r   <module>   s    +