o
    \"<f"                     @   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
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 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 Z!dd Z"dd Z#dd Z$dd Z%G dd deZ&G dd de&Z'G dd  d e&Z(dS )!    N)apps)prepare_lookup_value)PermissionDenied)DjangoJSONEncoder)
connectionmodels)
LOOKUP_SEP)HttpResponse)method_decorator)	smart_str)SafeText)gettext)ngettext)never_cache)View)AUTOCOMPLETE_LIMITAUTOCOMPLETE_SEARCH_FIELDSc                 C   s   t | dd r
|  S t| S )Nrelated_label)getattrr   r   f r   N/var/www/html/kck/venv/lib/python3.10/site-packages/grappelli/views/related.py	get_label   s   r   c                 C   s"   t | dd rt|  trdS dS )Nr   TF)r   
isinstancer   r   r   r   r   r   get_label_safe   s   r   c                 C   s   t | |gd} t| |S )N)fromlist)
__import__r   )modulenamer   r   r   import_from$   s   
r    c                 C   s   t tj| tdddS )N)clszapplication/javascript)content_type)r	   jsondumpsr   )datar   r   r   ajax_response)   s   r&   c                 C   s>   t | dr	|  S zt| jj | jj W S  ty   Y dS w )a?  
    Returns the fields to be used for autocomplete of the given model,
    first using the autocomplete_search_fields() static method when defined on
    the model.
    If the staticmethod is not declared, looks for the fields value in the
    GRAPPELLI_AUTOCOMPLETE_SEARCH_FIELDS setting for the given app/model.
    autocomplete_search_fieldsN)hasattrr'   r   _meta	app_label
model_nameKeyError)modelr   r   r   get_autocomplete_search_fields-   s   
r.   c                   @   sX   e 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
eedd ZdS )RelatedLookupzRelated Lookupc                 C   s   | j jjr
| j jjstd S N)requestuser	is_activeis_staffr   selfr   r   r   check_user_permissionA   s   z#RelatedLookup.check_user_permissionc                 C      d| j v od| j v od| j v S )N	object_idr*   r+   GETr5   r   r   r   request_is_validE      zRelatedLookup.request_is_validc                 C   s@   zt | jd | jd | _W | jS  ty   d | _Y | jS w )Nr*   r+   )r   	get_modelr;   r-   LookupErrorr5   r   r   r   r>   H   s   zRelatedLookup.get_modelc                 C   sf   i }| j dd }|r+|dD ]}|d\}}|dkr*tt|t||t|< q|jdi |S )Nquery_string:=	_to_fieldr   )r;   getsplitr   r   filter)r6   qsfiltersr@   itemkvr   r   r   get_filtered_querysetV   s   z#RelatedLookup.get_filtered_querysetc                 C   s   | j j }| |}|S r0   )r-   _default_managerget_querysetrL   )r6   rG   r   r   r   rN   a   s   
zRelatedLookup.get_querysetc                 C   sB   | j dd }|d urt||}t|tst|ts|j}|S |S )Nto_field)r;   rD   r   r   strintpk)r6   objobj_idrO   return_valuer   r   r   get_return_valuei   s   
zRelatedLookup.get_return_valuec              	   C   s   | j d }| j dd }g }|rYz/|d ur"|  jdi ||i}n|  j|d}|d| || t|t|d W |S  | jjt	fyX   ||t
ddd Y |S w |S )	Nr9   rO   rR   z%svaluelabelsafe?Fr   )r;   rD   rN   appendrV   r   r   r-   DoesNotExist
ValueError_)r6   rT   rO   r%   rS   r   r   r   get_datar   s   
(zRelatedLookup.get_datac                 O   sT   |    | jj| _|  r |   | jd ur |  }|r t|S d ddg}t|S )N rY   rZ   )r7   r1   r;   r<   r>   r-   ra   r&   )r6   r1   argskwargsr%   r   r   r   rD      s   

zRelatedLookup.getN)__name__
__module____qualname____doc__r7   r<   r>   rL   rN   rV   ra   r
   r   rD   r   r   r   r   r/   >   s    	r/   c                   @   s   e Zd ZdZdd ZdS )	M2MLookupz
M2M Lookupc              
   C   s   | j d d}g }dd |D D ]1}z|  j|d}||t|t|d W q | jjt	fyB   ||t
ddd Y qw |S )	Nr9   ,c                 s   s    | ]}|r|V  qd S r0   r   ).0ir   r   r   	<genexpr>   s    z%M2MLookup.get_data.<locals>.<genexpr>rW   rX   r\   F)r;   rE   rN   rD   r]   r   r   r-   r^   r_   r`   )r6   obj_idsr%   rT   rS   r   r   r   ra      s   zM2MLookup.get_dataN)rf   rg   rh   ri   ra   r   r   r   r   rj      s    rj   c                       sR   e Zd Zd Zdd Zdd ZdddZ fdd	Zd
d Ze	e
dd Z  ZS )AutocompleteLookupc                 C   r8   )Ntermr*   r+   r:   r5   r   r   r   r<      r=   z#AutocompleteLookup.request_is_validc           	   	   C   s   | j }| jd }z||}W n	 ty   Y nw t| j }|rOt }| D ]}t }|D ]}|tjdi t|t|iO }q0||M }q(|	|}|S |j
 }|S )Nrq   r   )r-   r;   autocomplete_term_adjustAttributeErrorr.   r   QrE   r   rF   objectsnone)	r6   rG   r-   rq   search_fieldssearchword
term_querysearch_fieldr   r   r   get_searched_queryset   s*   






z(AutocompleteLookup.get_searched_querysetNc                 C   s   g }|j jD ]9}|j }|dtD ]}||}|jr!|jj }q|dur,|t | }|jr:|| 	|j
| q|| q|S )a  
        This recursive function returns the final lookups
        for the default ordering of a model.

        Considering the models below, `get_final_ordering(Book)` will return
        `['-type__name', 'name']` instead of the simple `['-type', 'name']`
        one would get using `Book._meta.ordering`.

            class BookType(Model):
                name = CharField(max_length=50)

                class Meta:
                    ordering = ['name']

            class Book(Model):
                name = CharField(max_length=50)
                type = ForeignKey(BookType)

                class Meta:
                    ordering = ['-type', 'name']
        -N)r)   orderinglstriprE   r   	get_fieldis_relationrelated_modelextendget_final_orderingr-   r]   )r6   r-   previous_lookup_partsr~   lookupoptspartfieldr   r   r   r      s   
z%AutocompleteLookup.get_final_orderingc                    sr   t t|  }| |}tjdkr5| | j}dd |D }| jjj	j
}||vr-|| |j| j| S | S )N
postgresqlc                 S   s   g | ]}| d qS )r}   )r   )rl   or   r   r   
<listcomp>   s    z3AutocompleteLookup.get_queryset.<locals>.<listcomp>)superrp   rN   r|   r   vendorr   r-   r)   rR   r   r]   order_bydistinct)r6   rG   r~   distinct_columnspk_name	__class__r   r   rN      s   


zAutocompleteLookup.get_querysetc                    s    fdd   d t D S )Nc                    s$   g | ]}  ||jt|d qS )rc   )rV   rR   r   )rl   r   r5   r   r   r      s   $ z/AutocompleteLookup.get_data.<locals>.<listcomp>)rN   r   r5   r   r5   r   ra      r=   zAutocompleteLookup.get_datac                 O   s^   |    | jj| _|  r|   |  }|rt|S tdddddi }d |dg}t|S )Nz%(counter)s resultz%(counter)s resultsr   counterrc   )r7   r1   r;   r<   r>   ra   r&   r   )r6   r1   rd   re   r%   rZ   r   r   r   rD      s   
zAutocompleteLookup.getr0   )rf   rg   rh   ri   r<   r|   r   rN   ra   r
   r   rD   __classcell__r   r   r   r   rp      s    
%rp   ))r#   django.appsr   django.contrib.admin.utilsr   django.core.exceptionsr   django.core.serializers.jsonr   	django.dbr   r   django.db.models.constantsr   django.httpr	   django.utils.decoratorsr
   django.utils.encodingr   django.utils.safestringr   django.utils.translationr   r`   r   django.views.decorators.cacher   django.views.genericr   grappelli.settingsr   r   r   r   r    r&   r.   r/   rj   rp   r   r   r   r   <module>   s0   S