U
    |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   ).0Z
index_nameZindex_modelsindicesr   w/var/www/dating/data/www/fatepal.com/env/lib/python3.8/site-packages/wagtail/search/management/commands/update_index.py
<listcomp>1   s   z)group_models_by_index.<locals>.<listcomp>)collectionsOrderedDictZget_index_for_model
setdefaultnameappenditems)backendmodelsZ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|| 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   s8  |  d|  t|}|js.|  d|  d S t|t  }|sP|  |d  |D ]\}}|  |d|j   ||}| }|D ]}	||	 qd}
|s|D ]j}	| j d	||	j
j|	jddd	 | | |	 d
|D ]}||	| |
t|7 }
q|   q|  |  |d|
   |   qTd 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   Zrebuilder_classr   r   r   r   startZ	add_modelformat_metaZ	app_label__name__ljustprint_iter_progressqueryset_chunksZget_indexed_objectsZorder_byZ	add_itemslenprint_newlinefinish)r   backend_nameschema_only
chunk_sizer   Zmodels_grouped_by_indexr   r   Z	rebuilderr   Zobject_countchunkr   r   r   update_backend>   sT     
   
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storer.   zSpecify a backend to update)actiondestdefaulthelpz--schema-only
store_truer/   Fz(Prevents loading any data into the indexz--chunk_sizer0   zHSet number of records to be fetched at once for inserting into the index)r4   r5   r6   typer7   )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r4tj }ndg}|D ]"}| j||dd|dd q>d S )	Nr   r.   WAGTAILSEARCH_BACKENDSr6   r/   Fr0   )r/   r0   )r   hasattrr   r?   keysr2   get)r   optionsZbackend_namesr.   r   r   r   handle   s    


zCommand.handlec                 C   s   |  d d S )Nr    )r   )r   r   r   r   r,      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.
           )r$   .r    r!   (   r   z#                                   
    N)	enumerater   r,   r   flush)r   iterableivaluer   r   r   r)      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   NrE   )list)r   qsr0   rM   r   r   r   r   r*      s    zCommand.queryset_chunksN)r'   
__module____qualname__r   r;   r2   r>   rD   r,   r)   r   Zatomicr*   r   r   r   r   r   8   s    
8r   )r   Zdjango.confr   Zdjango.core.management.baser   Z	django.dbr   Zwagtail.search.backendsr   Zwagtail.search.indexr   r;   r   r   r   r   r   r   <module>   s   +