o
    h)A                     @   s  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 dlmZmZ d dlmZmZ d d	lmZmZ G d
d deZG dd deZG dd deZG dd deZG dd deZG dd dZG dd dZG dd deZG dd dZG dd dZdd ZdS )     N)warn)Extract)ExtractYear)Lookup)QuerySet)NothingNode	WhereNode)class_is_indexedget_indexed_models)	MATCH_ALL	PlainTextc                   @      e Zd ZdS )FilterErrorN__name__
__module____qualname__ r   r   \/var/www/html/ndineBlogger/venv/lib/python3.10/site-packages/wagtail/search/backends/base.pyr          r   c                       s"   e Zd Zdd fdd
Z  ZS )
FieldErrorN
field_namec                   s   || _ t j|i | d S N)r   super__init__)selfr   argskwargs	__class__r   r   r      s   zFieldError.__init__)r   r   r   r   __classcell__r   r   r   r   r      s    r   c                   @   r   )SearchFieldErrorNr   r   r   r   r   r"      r   r"   c                   @   r   )FilterFieldErrorNr   r   r   r   r   r#      r   r#   c                   @   r   )OrderByFieldErrorNr   r   r   r   r   r$       r   r$   c                   @   sl   e Zd ZdZ			dddZdd Zdd	 Zd
d Zdd ZdddZ	dddZ
dd Zdd Zdd ZdS )BaseSearchQueryCompilerorNTc                 C   sP   || _ |d u rtdt t}nt|trt||p| jd}|| _|| _	|| _
d S )Nz7Querying `None` is deprecated, use `MATCH_ALL` instead.)operator)querysetr   DeprecationWarningr   
isinstancestrr   DEFAULT_OPERATORqueryfieldsorder_by_relevance)r   r(   r-   r.   r'   r/   r   r   r   r   '   s   

z BaseSearchQueryCompiler.__init__c                    s&    fdd j j D |d }|S )Nc                    s   i | ]
}|  jj|qS r   )get_attnamer(   model.0fieldr   r   r   
<dictcomp>>   s    zABaseSearchQueryCompiler._get_filterable_field.<locals>.<dictcomp>)r(   r1   get_filterable_search_fieldsget)r   field_attnamer4   r   r5   r   _get_filterable_field<   s   

z-BaseSearchQueryCompiler._get_filterable_fieldc                 C      t r   NotImplementedError)r   r4   lookupvaluer   r   r   _process_lookupE      z'BaseSearchQueryCompiler._process_lookupc                 C   r;   r   r<   r5   r   r   r   _process_match_noneH   rA   z+BaseSearchQueryCompiler._process_match_nonec                 C   r;   r   r<   )r   filters	connectornegatedr   r   r   _connect_filtersK   rA   z(BaseSearchQueryCompiler._connect_filtersFc                 C   s   |  |}|d u rtd| d | d | jjj d |d|s'| |||}|d u rAtd| d | d t| d	 | d
 |S )Nz)Cannot filter search results with field "!". Please add index.FilterField('') to .search_fields.r   z+Could not apply filter on search results: "__z = z". Lookup "z"" not recognised.)r:   r#   r(   r1   r   r@   r   r+   )r   r9   r>   r?   
check_onlyr4   resultr   r   r   _process_filterN   sR   
z'BaseSearchQueryCompiler._process_filterc           
         s  t |trt |jtrt |jtstd|jj d |jjjj}|j}|dkr3t	
t|jdd}n|dkrFt	
t|jd dd}d}nl|dkrYt	
t|jd dd}d}nY|dkrht	
t|jdd}nJ|dkr j|dt	
t|jdd|d	} j|dt	
t|jd dd|d	}|rd S  ||gd
dS td|jj d |jjj}|j}|j}|drd S  j||||d	S t |trˈ  S t |tr|j} fdd|jD }	|sdd |	D }	 |	||jS d S tdtt| )Nz(Cannot apply filter on search results: "z" queries are not supported.gte   gtlteltexactrK   ANDF_ptr_idc                    s   g | ]}  |qS r   )_get_filters_from_where_node)r3   childr5   r   r   
<listcomp>   s    zHBaseSearchQueryCompiler._get_filters_from_where_node.<locals>.<listcomp>c                 S   s   g | ]}|r|qS r   r   )r3   child_filterr   r   r   rY      s
    z>Could not apply filter on search results: Unknown where node: )r*   r   lhsExtractDater   r   lookup_nametargetattnamedatetimedateintrhsrM   rF   endswithr   rB   r   rD   childrenrE   r+   type)
r   
where_noderK   r9   r>   r?   filter1filter2rD   child_filtersr   r5   r   rW   q   s   






	
z4BaseSearchQueryCompiler._get_filters_from_where_nodec                 C   s   |  | jjjS r   )rW   r(   r-   wherer5   r   r   r   _get_filters_from_queryset   s   z2BaseSearchQueryCompiler._get_filters_from_querysetc                 c   s    | j rd S | jjjD ]4}d}|drd}|dd  }| |}|d u r:td| d | d | jjj d |d	||fV  qd S )
NF-TrO   z'Cannot sort search results with field "rG   rH   rI   r   )	r/   r(   r-   order_by
startswithr:   r$   r1   r   )r   r   reverser4   r   r   r   _get_order_by   s8   

z%BaseSearchQueryCompiler._get_order_byc                 C   s~   | j r-dd | jj D }| j D ]}||vr,td| d | d | jjj d |dq| j| jjjdd	 t	| 
  d S )
Nc                 S   s   h | ]}|j qS r   r   r2   r   r   r   	<setcomp>   s    z0BaseSearchQueryCompiler.check.<locals>.<setcomp>zCannot search with field "z!". Please add index.SearchField('rH   rI   r   TrT   )r.   r(   r1   get_searchable_search_fieldsr"   r   rW   r-   rk   listrq   )r   allowed_fieldsr   r   r   r   check   s2   

zBaseSearchQueryCompiler.checkNNT)F)r   r   r   r,   r   r:   r@   rB   rF   rM   rW   rl   rq   rv   r   r   r   r   r%   $   s    
	

#ar%   c                   @   s|   e Zd ZdZdddZdddZdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd ZdS )BaseSearchResultsFNc                 C   s4   || _ || _|| _d| _d | _d | _d | _d | _d S Nr   )backendquery_compilerprefetch_relatedstartstop_results_cache_count_cache_score_field)r   rz   r{   r|   r   r   r   r     s   
zBaseSearchResults.__init__c                 C   sr   |d ur| j d urt| j | j| | _ n| j| | _ |d ur7| j d ur/t| j | j| | _d S | j| | _d S d S r   )r~   minr}   )r   r}   r~   r   r   r   _set_limits  s   

zBaseSearchResults._set_limitsc                 C   s6   | j }|| j| j| jd}| j|_| j|_| j|_|S )N)r|   )r    rz   r{   r|   r}   r~   r   )r   klassnewr   r   r   _clone(  s   zBaseSearchResults._clonec                 C   r;   r   r<   r5   r   r   r   
_do_search2  rA   zBaseSearchResults._do_searchc                 C   r;   r   r<   r5   r   r   r   	_do_count5  rA   zBaseSearchResults._do_countc                 C   s   | j d u rt|  | _ | j S r   )r   rt   r   r5   r   r   r   results8  s   
zBaseSearchResults.resultsc                 C   s6   | j d u r| jd urt| j| _ | j S |  | _ | j S r   )r   r   lenr   r5   r   r   r   count=  s   


zBaseSearchResults.countc                 C   s   |   }t|tr4|jd urt|jnd }|jd urt|jnd }||| | jd ur2| j| |_|S | jd ur>| j| S | j| |_| j| d |_t|d S )NrO   r   )	r   r*   slicer}   rb   r~   r   r   rt   )r   keyr   r}   r~   r   r   r   __getitem__E  s   



zBaseSearchResults.__getitem__c                 C      t |  S r   )iterr   r5   r   r   r   __iter__[     zBaseSearchResults.__iter__c                 C   r   r   )r   r   r5   r   r   r   __len__^  r   zBaseSearchResults.__len__c                 C   s,   t | d d }t|dkrd|d< d| S )N      z$...(remaining elements truncated)...z<SearchResults %r>)rt   r   )r   datar   r   r   __repr__a  s   zBaseSearchResults.__repr__c                 C   s   |   }||_|S r   )r   r   )r   r   cloner   r   r   annotate_scoreg  s   z BaseSearchResults.annotate_scorec                 C   s   t d)Nz-This search backend does not support facetingr<   )r   r   r   r   r   facetl     zBaseSearchResults.facetr   )NN)r   r   r   supports_facetr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rx     s    



rx   c                       s4   e Zd Z fddZdd Zdd Zdd Z  ZS )	EmptySearchResultsc                    s   t  d d  d S r   )r   r   r5   r   r   r   r   q  s   zEmptySearchResults.__init__c                 C   s   |   S r   r   r5   r   r   r   r   t  r   zEmptySearchResults._clonec                 C   s   g S r   r   r5   r   r   r   r   w  rA   zEmptySearchResults._do_searchc                 C   s   dS ry   r   r5   r   r   r   r   z  rA   zEmptySearchResults._do_count)r   r   r   r   r   r   r   r!   r   r   r   r   r   p  s
    r   c                   @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )	NullIndexz
    Index class that provides do-nothing implementations of the indexing operations required by
    BaseSearchBackend. Use this for search backends that do not maintain an index, such as the
    database backend.
    c                 C      d S r   r   r   r1   r   r   r   	add_model  rA   zNullIndex.add_modelc                 C   r   r   r   r5   r   r   r   refresh  rA   zNullIndex.refreshc                 C   r   r   r   r   itemr   r   r   add_item  rA   zNullIndex.add_itemc                 C   r   r   r   )r   r1   itemsr   r   r   	add_items  rA   zNullIndex.add_itemsc                 C   r   r   r   r   r   r   r   delete_item  rA   zNullIndex.delete_itemN)	r   r   r   __doc__r   r   r   r   r   r   r   r   r   r   ~  s    r   c                   @   s   e Zd ZdZdZdZdZdZdd Zdd Z	dd Z
d	d
 Zdd Zdd Zdd Zdd Zdd Zdd Z			dddZ			dddZdS )BaseSearchBackendNFc                 C   r   r   r   )r   paramsr   r   r   r     rA   zBaseSearchBackend.__init__c                 C   s   t  S r   )r   r   r   r   r   get_index_for_model  s   z%BaseSearchBackend.get_index_for_modelc                 C   r   r   r   r5   r   r   r   get_rebuilder  rA   zBaseSearchBackend.get_rebuilderc                 C   r;   r   r<   r5   r   r   r   reset_index  rA   zBaseSearchBackend.reset_indexc                 C   s   |  || d S r   )r   r   r   r   r   r   add_type  s   zBaseSearchBackend.add_typec                 C   s8   g }t  D ]}| |}||vr|  || qd S r   )r
   r   r   append)r   refreshed_indexesr1   indexr   r   r   refresh_index  s   


zBaseSearchBackend.refresh_indexc                 C      |  t|| d S r   )r   rf   r   r   objr   r   r   add     zBaseSearchBackend.addc                 C   s   |  ||| d S r   )r   r   )r   r1   obj_listr   r   r   add_bulk  s   zBaseSearchBackend.add_bulkc                 C   r   r   )r   rf   r   r   r   r   r   delete  r   zBaseSearchBackend.deletec                 K   sf   t |tr|j}|}n|}|j }t|st S |dkr t S |||fi |}|  | | |S )N )	r*   r   r1   objectsallr	   r   rv   results_class)r   query_compiler_classr-   model_or_querysetr   r1   r(   search_query_compilerr   r   r   _search  s   

zBaseSearchBackend._searchTc                 C   s   | j | j|||||dS )Nr.   r'   r/   )r   r   r   r-   r   r.   r'   r/   r   r   r   search  s   zBaseSearchBackend.searchc                 C   s*   | j d u r	td| j| j |||||dS )Nz9This search backend does not support the autocomplete APIr   )!autocomplete_query_compiler_classr=   r   r   r   r   r   autocomplete  s   
zBaseSearchBackend.autocompleterw   )r   r   r   r   r   r   rebuilder_classcatch_indexing_errorsr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r     s0    
r   c                 C   s,   | j jrt| j j d d }t|S | S )a  
    This function finds the root model for any given model. The root model is
    the highest concrete model that it descends from. If the model doesn't
    descend from another concrete model then the model is it's own root model so
    it is returned.

    Examples:
    >>> get_model_root(wagtailcore.Page)
    wagtailcore.Page

    >>> get_model_root(myapp.HomePage)
    wagtailcore.Page

    >>> get_model_root(wagtailimages.Image)
    wagtailimages.Image
    r   )_metaparentsrt   r   get_model_root)r1   parent_modelr   r   r   r     s   r   ) r`   warningsr   #django.db.models.functions.datetimer   r\   r   django.db.models.lookupsr   django.db.models.queryr   django.db.models.sql.wherer   r   wagtail.search.indexr	   r
   wagtail.search.queryr   r   	Exceptionr   r   r"   r#   r$   r%   rx   r   r   r   r   r   r   r   r   <module>   s*     kbh