U
    8ÄT_p  ã                   @   s0   d dl mZmZ d dlmZ G dd„ deƒZdS )é    )ÚHttp404ÚJsonResponse)ÚBaseListViewc                   @   s:   e Zd ZdZdZdZdd„ Zdd„ Zdd	„ Zdd
d„Z	dS )ÚAutocompleteJsonViewz3Handle AutocompleteWidget's AJAX requests for data.é   Nc                 O   s„   | j  |¡s tdt| j ƒj ƒ‚|  |¡s:tddidd�S |j dd¡| _	|  
¡ | _|  ¡ }tdd	„ |d
 D ƒd|d  ¡ idœƒS )z±
        Return a JsonResponse with search results of the form:
        {
            results: [{id: "123" text: "foo"}],
            pagination: {more: true}
        }
        z5%s must have search_fields for the autocomplete_view.Úerrorz403 Forbiddeni“  )ÚstatusÚtermÚ c                 S   s    g | ]}t |jƒt |ƒd œ‘qS ))ÚidÚtext)ÚstrÚpk)Ú.0Úobj© r   úK/tmp/pip-unpacked-wheel-dv63caxw/django/contrib/admin/views/autocomplete.pyÚ
<listcomp>   s   ÿz,AutocompleteJsonView.get.<locals>.<listcomp>Úobject_listZmoreZpage_obj)ÚresultsZ
pagination)Úmodel_adminZget_search_fieldsr   ÚtypeÚ__name__Úhas_permr   ÚGETÚgetr	   Úget_querysetr   Zget_context_dataZhas_next)ÚselfÚrequestÚargsÚkwargsÚcontextr   r   r   r   
   s"    
ÿÿ

þûzAutocompleteJsonView.getc                 O   s   | j j| jf|ž|ŽS )zUse the ModelAdmin's paginator.)r   Úget_paginatorr   )r   r   r    r   r   r   r"   %   s    z"AutocompleteJsonView.get_paginatorc                 C   s6   | j  | j¡}| j  | j|| j¡\}}|r2| ¡ }|S )z9Return queryset based on ModelAdmin.get_search_results().)r   r   r   Zget_search_resultsr	   Zdistinct)r   ÚqsZsearch_use_distinctr   r   r   r   )   s
    z!AutocompleteJsonView.get_querysetc                 C   s   | j j||d�S )z9Check if user has permission to access the related model.)r   )r   Zhas_view_permission)r   r   r   r   r   r   r   1   s    zAutocompleteJsonView.has_perm)N)
r   Ú
__module__Ú__qualname__Ú__doc__Zpaginate_byr   r   r"   r   r   r   r   r   r   r      s   r   N)Zdjango.httpr   r   Zdjango.views.generic.listr   r   r   r   r   r   Ú<module>   s   