o
    ›¨Êhs"  ã                   @   sš   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 d dlmZ zd dlmZ W n ey?   d	ZY nw G d
d„ dƒZdd„ Zd	S )é    )ÚObjectDoesNotExist)Ú	force_str)Úcapfirst)ÚDEFAULT_ALIAS)Ú
NotHandledÚSpatialError)Úlog)Úhaystack_get_model)ÚdistanceNc                   @   sÞ   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	e
e	ƒZdd„ Zdd„ Ze
eeƒZdd„ Zdd„ Ze
eeƒZdd„ Zdd„ Ze
eeƒZdd„ Ze
eƒZdd„ Ze
eƒZdd„ Zd d!„ Zd"d#„ Zd$d%„ Zd&d'„ Zd(S ))ÚSearchResultaB  
    A single search result. The actual object is loaded lazily by accessing
    object; until then this object only stores the model, pk, and score.

    Note that iterating over SearchResults and getting the object for each
    result will do O(N) database queries, which may not fit your needs for
    performance.
    c                 K   s”   ||| _ | _|| _|| _d | _d | _d | _g | _| dd ¡| _	| dd ¡| _
d | _|  ¡ | _| ¡ D ]\}}|| jvrG|| j|< | j |¡ q3d S )NÚ_point_of_originÚ	_distance)Ú	app_labelÚ
model_nameÚpkÚscoreÚ_objectÚ_modelÚ_verbose_nameÚ_additional_fieldsÚpopr   r   Ústored_fieldsÚ_get_logr   ÚitemsÚ__dict__Úappend)Úselfr   r   r   r   ÚkwargsÚkeyÚvalue© r    úA/var/www/html/env/lib/python3.10/site-packages/haystack/models.pyÚ__init__   s"   


€ýzSearchResult.__init__c                 C   s
   t  d¡S )NÚhaystack)ÚloggingÚ	getLogger©r   r    r    r!   r   /   ó   
zSearchResult._get_logc                 C   s   d| j | j| jf S )Nz<SearchResult: %s.%s (pk=%r)>)r   r   r   r&   r    r    r!   Ú__repr__2   s
   ýzSearchResult.__repr__c                 C   s   t |  ¡ ƒS ©N)r   r(   r&   r    r    r!   Ú__str__9   s   zSearchResult.__str__c                 C   s   |dkrt ‚| j |d ¡S )NÚ__getnewargs__)ÚAttributeErrorr   Úget)r   Úattrr    r    r!   Ú__getattr__<   s   zSearchResult.__getattr__c                 C   s    ddl m} |t  ¡  | j¡S ©Nr   ©Úconnections)r#   r2   r   Úget_unified_indexÚ	get_indexÚmodel)r   r2   r    r    r!   Ú_get_searchindexB   s   zSearchResult._get_searchindexc                 C   sº   | j d u rZ| jd u r| j d| ¡ d S z1z| j ¡ j| jd�| _ W W | j S  tyD   | j 	d| j
| j¡ | jjj| jd�| _ Y W | j S w  tyY   | j d| ¡ d | _ Y | j S w | j S )Nú/Model could not be found for SearchResult '%s'.)r   z)Model '%s.%s' not handled by the routers.z<Object could not be found in database for SearchResult '%s'.)r   r5   r   ÚerrorÚsearchindexÚread_querysetr-   r   r   Úwarningr   r   Ú_default_managerr   r&   r    r    r!   Ú_get_objectI   s2   

òýòÿúzSearchResult._get_objectc                 C   ó
   || _ d S r)   )r   ©r   Úobjr    r    r!   Ú_set_objectb   r'   zSearchResult._set_objectc                 C   s@   | j d u rzt| j| jƒ| _ W | j S  ty   Y | j S w | j S r)   )r   r	   r   r   ÚLookupErrorr&   r    r    r!   Ú
_get_modelg   s   
úúzSearchResult._get_modelc                 C   r>   r)   )r   r?   r    r    r!   Ú
_set_models   r'   zSearchResult._set_modelc                 C   s°   ddl m} | jd u rUtd u rtdƒ‚| jstdƒ‚t| | jd ƒs+td| jd  ƒ‚| jd j\}}t| | jd ƒ}|d u rAd S |j\}}|t 	||f||f¡j
d�| _| jS )	Nr   )ÚDistancezŒThe backend doesn't have 'DISTANCE_AVAILABLE' enabled & the 'geopy' library could not be imported, so distance information is not available.z$The original point is not available.Úfieldz[The field '%s' was not included in search results, so the distance could not be calculated.Úpoint)Úkm)Údjango.contrib.gis.measurerE   r   Úgeopy_distancer   r   ÚhasattrÚcoordsÚgetattrr
   rH   )r   rE   Úpo_lngÚpo_latÚlocation_fieldÚlf_lngÚlf_latr    r    r!   Ú_get_distancex   s.   
ÿÿÿ
ÿzSearchResult._get_distancec                 C   r>   r)   )r   )r   Údistr    r    r!   Ú_set_distance�   r'   zSearchResult._set_distancec                 C   ó.   | j d u r| j d| ¡ dS tt| j jjƒƒS ©Nr7   Ú )r5   r   r8   r   r   Ú_metaÚverbose_namer&   r    r    r!   Ú_get_verbose_name¢   ó   
zSearchResult._get_verbose_namec                 C   rV   rW   )r5   r   r8   r   r   rY   Úverbose_name_pluralr&   r    r    r!   Ú_get_verbose_name_plural«   r\   z%SearchResult._get_verbose_name_pluralc                 C   s(   | j du r| j d| ¡ dS t| j jƒS )z9Returns the content type for the result's model instance.Nr7   rX   )r5   r   r8   ÚstrrY   r&   r    r    r!   Úcontent_type´   s   
zSearchResult.content_typec                 C   s"   i }| j D ]	}t| |ƒ||< q|S )zü
        Returns a dictionary of all of the fields from the raw result.

        Useful for serializing results. Only returns what was seen from the
        search engine, so it may have extra fields Haystack's indexes aren't
        aware of.
        )r   rM   )r   Úadditional_fieldsÚ	fieldnamer    r    r!   Úget_additional_fields¼   s   
z"SearchResult.get_additional_fieldsc                 C   s‚   | j du r>ddlm} z|t  ¡  | j¡}W n ty"   i  Y S w i | _ |j 	¡ D ]\}}|j
du r=t| |dƒ| j |< q+| j S )zÔ
        Returns a dictionary of all of the stored fields from the SearchIndex.

        Useful for serializing results. Only returns the fields Haystack's
        indexes are aware of as being 'stored'.
        Nr   r1   TrX   )Ú_stored_fieldsr#   r2   r   r3   r4   r5   r   Úfieldsr   ÚstoredrM   )r   r2   Úindexrb   rF   r    r    r!   Úget_stored_fieldsË   s   
ÿþ
€zSearchResult.get_stored_fieldsc                 C   s   | j  ¡ }|d= |S )zp
        Returns a dictionary representing the ``SearchResult`` in order to
        make it pickleable.
        r   )r   Úcopy)r   Úret_dictr    r    r!   Ú__getstate__ç   s   
zSearchResult.__getstate__c                 C   s   | j  |¡ |  ¡ | _dS )zU
        Updates the object's attributes according to data passed by pickle.
        N)r   Úupdater   r   )r   Ú	data_dictr    r    r!   Ú__setstate__ò   s   zSearchResult.__setstate__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r"   r   r(   r*   r/   r6   Úpropertyr9   r=   rA   ÚobjectrC   rD   r5   rS   rU   r
   r[   rZ   r^   r]   r`   rc   rh   rk   rn   r    r    r    r!   r      s6    	

%
r   c                 O   s.   ddl m} | ¡ D ]
}| ¡ }| ¡  q
d S r0   )r#   r2   Úallr3   Úreset)ÚsenderÚargsr   r2   ÚconnÚuir    r    r!   Úreload_indexesú   s
   
ür{   )Údjango.core.exceptionsr   Údjango.utils.encodingr   Údjango.utils.textr   Úhaystack.constantsr   Úhaystack.exceptionsr   r   Úhaystack.utilsr   r$   Úhaystack.utils.app_loadingr	   Úgeopyr
   rJ   ÚImportErrorr   r{   r    r    r    r!   Ú<module>   s   ÿ h