3
(hN                 @   s8   d dl Z d dlmZ d dlmZ d dlmZ dd ZdS )    N)HttpResponse)
force_text)registryc                s   | j dkr(d| jkrtdS | jd }nd| jkr:tdS | jd }tj| t drb j|  t|t	 ddkr j
|| }ng }tj fdd|D }t|d	d
}d|d< |S )a  Load the named lookup channel and lookup matching models.

    GET or POST should contain 'term'

    Returns:
        HttpResponse - JSON: `[{pk: value: match: repr:}, ...]`
    Raises:
        PermissionDenied - depending on the LookupChannel's implementation of check_auth
    GETZterm 
check_authZ
min_length   c                s8   g | ]0}t t|d d j| j| j|dqS )pkN)r	   valuematchrepr)r   getattrZ
get_resultZformat_matchZformat_item_display).0item)lookup B/tmp/pip-install-q3hcpn_q/django-ajax-selects/ajax_select/views.py
<listcomp>)   s   zajax_lookup.<locals>.<listcomp>zapplication/json)content_typez/max-age=0, must-revalidate, no-store, no-cache;zCache-Control)methodr   r   POSTr   gethasattrr   lenr   Z	get_queryjsondumps)requestZchannelqueryZ	instancesresultsresponser   )r   r   ajax_lookup   s&    








r    )r   Zdjango.httpr   Zdjango.utils.encodingr   Zajax_selectr   r    r   r   r   r   <module>   s   