
    Nwgs"                         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 	 d dlmZ  G d
 d      Zd Zy	# e$ r d	ZY w xY w)    )ObjectDoesNotExist)	force_str)capfirst)DEFAULT_ALIAS)
NotHandledSpatialError)log)haystack_get_model)distanceNc                       e Zd ZdZd Zd Zd Zd Zd Zd Z	 e
e	      Zd Zd	 Z e
ee      Zd
 Zd Z e
ee      Zd Zd Z e
ee      Zd Z e
e      Zd Z e
e      Zd Zd Zd Zd Zd Zy)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                    ||c| _         | _        || _        || _        d | _        d | _        d | _        g | _        |j                  dd       | _	        |j                  dd       | _
        d | _        | j                         | _        |j                         D ]>  \  }}|| j                  vs|| j                  |<   | j                  j!                  |       @ y )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values           F/var/www/horilla/myenv/lib/python3.12/site-packages/haystack/models.py__init__zSearchResult.__init__   s    *3Z'
!"$ &

+=t DK6!==? ,,. 	4JC$--'%*c"''..s3	4    c                 ,    t        j                  d      S )Nhaystack)logging	getLoggerr   s    r#   r   zSearchResult._get_log/   s      ,,r%   c                 V    d| j                   d| j                  d| j                  dS )Nz<SearchResult: .z (pk=z)>)r   r   r   r*   s    r#   __repr__zSearchResult.__repr__2   s!    NNOOGG
 	
r%   c                 4    t        | j                               S N)r   r-   r*   s    r#   __str__zSearchResult.__str__9   s    ))r%   c                 P    |dk(  rt         | j                  j                  |d       S )N__getnewargs__)AttributeErrorr   get)r   attrs     r#   __getattr__zSearchResult.__getattr__<   s(    ##  }}  t,,r%   c                 n    ddl m} |t           j                         j	                  | j
                        S Nr   connections)r'   r:   r   get_unified_index	get_indexmodel)r   r:   s     r#   _get_searchindexzSearchResult._get_searchindexB   s(    (=);;=GG

SSr%   c                    | j                   p| j                  | j                  j                  d|        y 	 	 | j                  j                         j                  | j                        | _         | j                   S | j                   S # t        $ rt | j                  j                  d| j                  | j                         | j                  j                  j                  | j                        | _         Y | j                   S w xY w# t        $ r1 | j                  j                  d|        d | _         Y | j                   S w xY w)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   r=   r	   errorsearchindexread_querysetr4   r   r   warningr   r   _default_managerr   r*   s    r#   _get_objectzSearchResult._get_objectI   s   <<zz!PRVW$	O#'#3#3#A#A#C#G#G477#G#SDL ||t|| " OHH$$C $(::#>#>#B#Bdgg#B#NDL ||O & $RTX  $||$s*   9B	 	A/D8D	 DD	 	,EEc                     || _         y r/   )r   r   objs     r#   _set_objectzSearchResult._set_objectb   s	    r%   c                     | j                   2	 t        | j                  | j                        | _         | j                   S | j                   S # t        $ r Y | j                   S w xY wr/   )r   r
   r   r   LookupErrorr*   s    r#   
_get_modelzSearchResult._get_modelg   s\    ;;0Q {{t{{   {{s   %A 	A"!A"c                     || _         y r/   )r   rH   s     r#   
_set_modelzSearchResult._set_models   s	    r%   c                    ddl m} | j                  t        t	        d      | j
                  st	        d      t        | | j
                  d         st	        d| j
                  d   z        | j
                  d   j                  \  }}t        | | j
                  d         }|y |j                  \  }} |t        j                  ||f||f      j                        | _        | j                  S )	Nr   )DistancezThe 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.measurerQ   r   geopy_distancer   r   hasattrcoordsgetattrr   rT   )r   rQ   po_lngpo_latlocation_fieldlf_lnglf_lats          r#   _get_distancezSearchResult._get_distancex   s    7>>!
 %" c  (("#IJJ4!6!6w!?@"q++G45 
 "227;BBNFF$T4+@+@+IJN%+22NFF%!**FF+;ff=MNQQDN ~~r%   c                     || _         y r/   )r   )r   dists     r#   _set_distancezSearchResult._set_distance   s	    r%   c                     | j                   | j                  j                  d|        yt        t	        | j                   j
                  j                              S Nr@    )r=   r	   rA   r   r   _metaverbose_namer*   s    r#   _get_verbose_namezSearchResult._get_verbose_name   sB    ::HHNNLdS$**"2"2"?"?@AAr%   c                     | j                   | j                  j                  d|        yt        t	        | j                   j
                  j                              S rd   )r=   r	   rA   r   r   rf   verbose_name_pluralr*   s    r#   _get_verbose_name_pluralz%SearchResult._get_verbose_name_plural   sB    ::HHNNLdS$**"2"2"F"FGHHr%   c                     | j                   | j                  j                  d|        yt        | j                   j                        S )z9Returns the content type for the result's model instance.r@   re   )r=   r	   rA   strrf   r*   s    r#   content_typezSearchResult.content_type   s7    ::HHNNLdS4::##$$r%   c                 J    i }| j                   D ]  }t        | |      ||<    |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   rY   )r   additional_fields	fieldnames      r#   get_additional_fieldsz"SearchResult.get_additional_fields   s;     00 	DI+24+Ci(	D ! r%   c                 f   | j                   ddlm} 	 |t           j	                         j                  | j                        }i | _         |j                  j                         D ].  \  }}|j                  du st        | |d      | j                   |<   0 | j                   S # t        $ r i cY S w xY w)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'.
        r   r9   Tre   )_stored_fieldsr'   r:   r   r;   r<   r=   r   fieldsr   storedrY   )r   r:   indexrq   rR   s        r#   get_stored_fieldszSearchResult.get_stored_fields   s     &,.@@BLLTZZX  #%D %*LL$6$6$8 R 	5<<4'5<T9b5QD''	2R """  	s   0B" "B0/B0c                 @    | j                   j                         }|d= |S )zp
        Returns a dictionary representing the ``SearchResult`` in order to
        make it pickleable.
        r	   )r   copy)r   ret_dicts     r#   __getstate__zSearchResult.__getstate__   s"     ==%%'UOr%   c                 d    | j                   j                  |       | j                         | _        y)zU
        Updates the object's attributes according to data passed by pickle.
        N)r   updater   r	   )r   	data_dicts     r#   __setstate__zSearchResult.__setstate__   s#     	Y'==?r%   N)__name__
__module____qualname____doc__r$   r   r-   r0   r6   r>   propertyrB   rF   rJ   objectrM   rO   r=   r_   rb   r   rh   rg   rk   rj   rn   rr   rx   r|   r    r%   r#   r   r      s    4$-
*-T
 +,K2 k;/F
 Z,E#J }5HB -.LI ##;<%!#8	#r%   r   c                 z    ddl m} |j                         D ]"  }|j                         }|j	                          $ y r8   )r'   r:   allr;   reset)senderargsr    r:   connuis         r#   reload_indexesr      s4    $! ##% 	
	r%   )django.core.exceptionsr   django.utils.encodingr   django.utils.textr   haystack.constantsr   haystack.exceptionsr   r   haystack.utilsr	   r(   haystack.utils.app_loadingr
   geopyr   rV   ImportErrorr   r   r   r%   r#   <module>r      sK    5 + & , 8 ) 90d# d#N[  Ns   A AA