o
    ›¨Êh  ã                   @   s    d dl mZ d dlmZmZ d dlmZ d dlmZ d dl	m
Z
mZ d dlmZ eeddƒZG d	d
„ d
ƒZefdd„ZG dd„ deƒZddedddfdd„ZdS )é    )Úsettings)ÚInvalidPageÚ	Paginator)ÚHttp404)Úrender)ÚFacetedSearchFormÚModelSearchForm)ÚEmptySearchQuerySetÚ HAYSTACK_SEARCH_RESULTS_PER_PAGEé   c                   @   s€   e Zd ZdZi ZdZeƒ ZdZdZ	e
Z					ddd„Z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S )Ú
SearchViewúsearch/search.htmlÚ NTc                 C   s@   || _ || _|| _|d u rt| _|d ur|| _|r|| _d S d S ©N)Úload_allÚ
form_classÚsearchquerysetr   Úresults_per_pageÚtemplate)Úselfr   r   r   r   r   © r   ú@/var/www/html/env/lib/python3.10/site-packages/haystack/views.pyÚ__init__   s   
ÿzSearchView.__init__c                 C   s,   || _ |  ¡ | _|  ¡ | _|  ¡ | _|  ¡ S )zŠ
        Generates the actual response to the search.

        Relies on internal, overridable methods to construct the response.
        )ÚrequestÚ
build_formÚformÚ	get_queryÚqueryÚget_resultsÚresultsÚcreate_response)r   r   r   r   r   Ú__call__*   s
   


zSearchView.__call__c                 C   sV   d}d| j i}|r| |¡ t| jjƒr| jj}| jdur"| j|d< | j|fi |¤ŽS )zY
        Instantiates the form the class should use to process the search query.
        Nr   r   )r   ÚupdateÚlenr   ÚGETr   r   )r   Úform_kwargsÚdataÚkwargsr   r   r   r   8   s   



zSearchView.build_formc                 C   s   | j  ¡ r| j jd S dS )zs
        Returns the query provided by the user.

        Returns an empty string if the query is invalid.
        Úqr   )r   Úis_validÚcleaned_data©r   r   r   r   r   I   s   
zSearchView.get_queryc                 C   s
   | j  ¡ S )zv
        Fetches the results via the form.

        Returns an empty list if there's no query to search with.
        )r   Úsearchr+   r   r   r   r   T   s   
zSearchView.get_resultsc              	   C   s    zt | jj dd¡ƒ}W n ttfy   tdƒ‚w |dk r"tdƒ‚|d | j }| j||| j …  t	| j| jƒ}z
| 
|¡}W ||fS  tyO   tdƒ‚w )zã
        Paginates the results appropriately.

        In case someone does not want to use Django's built-in pagination, it
        should be a simple matter to override this method to do what they would
        like.
        Úpageé   zNot a valid number for page.zPages should be 1 or greater.zNo such page!)Úintr   r$   ÚgetÚ	TypeErrorÚ
ValueErrorr   r   r   r   r-   r   )r   Úpage_noÚstart_offsetÚ	paginatorr-   r   r   r   Ú
build_page\   s    ÿýÿzSearchView.build_pagec                 C   s   i S )zm
        Allows the addition of more context variables as needed.

        Must return a dictionary.
        r   r+   r   r   r   Úextra_contextx   s   zSearchView.extra_contextc                 C   sX   |   ¡ \}}| j| j||d dœ}t| jdƒr#| jjjjr#| j ¡ |d< | |  	¡ ¡ |S )N)r   r   r-   r5   Ú
suggestionr   r8   )
r6   r   r   Úhasattrr   ÚbackendÚinclude_spellingÚget_suggestionr"   r7   )r   r5   r-   Úcontextr   r   r   Úget_context€   s   û
	ÿ
þzSearchView.get_contextc                 C   s   |   ¡ }t| j| j|ƒS )zM
        Generates the actual HttpResponse to send back to the user.
        )r>   r   r   r   )r   r=   r   r   r   r    •   s   zSearchView.create_response)NTNNNr   )Ú__name__Ú
__module__Ú__qualname__r   r7   r   r	   r   r   r   ÚRESULTS_PER_PAGEr   r   r!   r   r   r   r6   r>   r    r   r   r   r   r      s,    
ú
r   c                    s   ‡ ‡‡fdd„}|S )Nc                    s   ˆˆ i ˆ¤Ž| ƒS r   r   )r   ©Úargsr'   Ú
view_classr   r   Úsearch_view    s   z(search_view_factory.<locals>.search_viewr   )rE   rD   r'   rF   r   rC   r   Úsearch_view_factoryŸ   s   rG   c                       s6   e Zd Z‡ fdd„Zd‡ fdd„	Z‡ fdd„Z‡  ZS )	ÚFacetedSearchViewc                    s,   |  d¡d u rt|d< tƒ j|i |¤Ž d S )Nr   )r0   r   Úsuperr   )r   rD   r'   ©Ú	__class__r   r   r   §   s   zFacetedSearchView.__init__Nc                    s*   |d u ri }| j j d¡|d< tƒ  |¡S )NÚselected_facets)r   r$   ÚgetlistrI   r   )r   r%   rJ   r   r   r   ®   s   zFacetedSearchView.build_formc                    s&   t ƒ  ¡ }| j|d< | j ¡ |d< |S )Nr   Úfacets)rI   r7   r   r   Úfacet_counts)r   ÚextrarJ   r   r   r7   ¸   s   

zFacetedSearchView.extra_contextr   )r?   r@   rA   r   r   r7   Ú__classcell__r   r   rJ   r   rH   ¦   s    
rH   r   TNc                 C   sÐ   d}t ƒ }| j d¡r!|| j||d�}	|	 ¡ r |	jd }|	 ¡ }n|||d�}	t||p,tƒ}
z|
 t	| j dd¡ƒ¡}W n t
yG   tdƒ‚w |	||
|ddœ}|jjjr[|	 ¡ |d	< |rb| |¡ t| ||ƒS )
aJ  
    A more traditional view that also demonstrate an alternative
    way to use Haystack.

    Useful as an example of for basing heavily custom views off of.

    Also has the benefit of thread-safety, which the ``SearchView`` class may
    not be.

    Template:: ``search/search.html``
    Context::
        * form
          An instance of the ``form_class``. (default: ``ModelSearchForm``)
        * page
          The current page of search results.
        * paginator
          A paginator instance for the results.
        * query
          The query received by the form.
    r   r(   )r   r   r-   r.   zNo such page of results!N)r   r-   r5   r   r8   r8   )r	   r$   r0   r)   r*   r,   r   rB   r-   r/   r   r   r   r:   r;   r<   r"   r   )r   r   r   r   r   r7   r   r   r   r   r5   r-   r=   r   r   r   Úbasic_search¿   s4   
€ÿû

rR   )Údjango.confr   Údjango.core.paginatorr   r   Údjango.httpr   Údjango.shortcutsr   Úhaystack.formsr   r   Úhaystack.queryr	   ÚgetattrrB   r   rG   rH   rR   r   r   r   r   Ú<module>   s$     ù