
#dV                 @   sp  d  d l  Z  d  d l 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 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# Gd d   d e  Z$ Gd d   d e$  Z% Gd d   d e$  Z& d S)    N)reduce)HttpResponse)models
connection)QuerySet)never_cache)View)	ungettextugettext)
smart_text)PermissionDenied)prepare_lookup_value)DjangoJSONEncoder)apps)AUTOCOMPLETE_LIMITAUTOCOMPLETE_SEARCH_FIELDSc             C   s&   t  |  d d   r |  j   St |   S)Nrelated_label)getattrr   r   )f r   C/tmp/pip-build-0jahl3lb/django-grappelli/grappelli/views/related.py	get_label   s    
r   c             C   s"   t  |  d | g }  t |  |  S)Nfromlist)
__import__r   )modulenamer   r   r   import_from   s    r   c             C   s   t  t j |  d t d d S)Nclscontent_typezapplication/javascript)r   jsondumpsr   )datar   r   r   ajax_response$   s    r"   c             C   sR   t  |  d  r |  j   Sy t |  j j |  j j SWn t k
 rM d SYn Xd S)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               @   sp   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
 d d    Z d S)RelatedLookupzRelated Lookupc             C   s+   |  j  j j o |  j  j j s' t  n  d  S)N)requestuserZ	is_activeZis_staffr   )selfr   r   r   check_user_permission<   s    z#RelatedLookup.check_user_permissionc             C   s+   d |  j  k o* d |  j  k o* d |  j  k S)N	object_idr&   r'   )GET)r.   r   r   r   request_is_valid@   s    zRelatedLookup.request_is_validc             C   sL   y' t  j |  j d |  j d  |  _ Wn t k
 rD d  |  _ Yn X|  j S)Nr&   r'   )r   	get_modelr1   r)   LookupError)r.   r   r   r   r3   C   s
    'zRelatedLookup.get_modelc             C   s   i  } |  j  j d d   } | r xc | j d  D]O } | j d  \ } } | d k r1 t t |  t |   | t |  <q1 q1 Wn  | j |   S)Nquery_string:=Z	_to_field)r1   getsplitr   r   filter)r.   qsfiltersr5   itemkvr   r   r   get_filtered_querysetJ   s    /z#RelatedLookup.get_filtered_querysetc             C   s%   |  j  j j   } |  j |  } | S)N)r)   Z_default_managerget_querysetr@   )r.   r;   r   r   r   rA   U   s    zRelatedLookup.get_querysetc             C   s   |  j  d } g  } | r y= |  j   j d |  } | j i | d 6t |  d 6 Wq |  j j t f k
 r | j i | d 6t d  d 6 Yq Xn  | S)Nr0   pkvaluelabel?)	r1   rA   r8   appendr   r)   DoesNotExist
ValueError_)r.   obj_idr!   objr   r   r   get_dataZ   s    %)zRelatedLookup.get_datac             O   s   |  j    |  j j |  _ |  j   r` |  j   |  j d  k	 r` |  j   } | r] t |  Sq` n  i d  d 6d d 6g } t |  S)NrC    rD   )r/   r,   r1   r2   r3   r)   rL   r"   )r.   r,   argskwargsr!   r   r   r   r8   e   s    

zRelatedLookup.getN)__name__
__module____qualname____doc__r/   r2   r3   r@   rA   rL   r   r8   r   r   r   r   r+   9   s   r+   c               @   s"   e  Z d  Z d Z d d   Z d S)	M2MLookupz
M2M Lookupc             C   s   |  j  d j d  } g  } x d d   | D D] } y= |  j   j d |  } | j i | d 6t |  d 6 Wq0 |  j j t f k
 r | j i | d 6t	 d  d 6 Yq0 Xq0 W| S)	Nr0   ,c             s   s   |  ] } | r | Vq d  S)Nr   ).0ir   r   r   	<genexpr>{   s    z%M2MLookup.get_data.<locals>.<genexpr>rB   rC   rD   rE   )
r1   r9   rA   r8   rF   r   r)   rG   rH   rI   )r.   Zobj_idsr!   rJ   rK   r   r   r   rL   x   s    %*zM2MLookup.get_dataN)rP   rQ   rR   rS   rL   r   r   r   r   rT   u   s   rT   c                   s^   e  Z d  Z d  Z d d   Z d d   Z   f d d   Z d d   Z e d	 d
    Z	   S)AutocompleteLookupc             C   s+   d |  j  k o* d |  j  k o* d |  j  k S)Ntermr&   r'   )r1   )r.   r   r   r   r2      s    z#AutocompleteLookup.request_is_validc                s   |  j  } |  j d } y | j |  } Wn t k
 r= Yn Xt |  j   } | r x | j   D]b     f d d   | D } t |  } | j j | j _ | j	 t
 t j |   } | | M} q` Wn | j j   } | S)NrZ   c                s2   g  |  ]( } t  j i t    t |  6   q Sr   )r   Qr   )rV   r=   )wordr   r   
<listcomp>   s   	 z<AutocompleteLookup.get_searched_queryset.<locals>.<listcomp>)r)   r1   Zautocomplete_term_adjustAttributeErrorr*   r9   r   queryZselect_relatedr:   r   operatoror_objectsnone)r.   r;   r)   rZ   Zsearch_fieldssearchZ	search_qsr   )r\   r   get_searched_queryset   s     	z(AutocompleteLookup.get_searched_querysetc                s   t  t |   j   } |  j |  } |  j |  } t j d k r t |  j j	 j
  |  j j	 j j g } | j |   j |   S| j   Sd  S)NZ
postgresql)superrY   rA   r@   re   r   vendorlistr)   r%   ZorderingrB   columnZorder_byZdistinct)r.   r;   Zdistinct_columns)	__class__r   r   rA      s    (zAutocompleteLookup.get_querysetc             C   s!   d d   |  j    d  t  D S)Nc             S   s-   g  |  ]# } i | j  d  6t |  d 6 q S)rC   rD   )rB   r   )rV   r   r   r   r   r]      s   	 z/AutocompleteLookup.get_data.<locals>.<listcomp>)rA   r   )r.   r   r   r   rL      s    zAutocompleteLookup.get_datac             O   s   |  j    |  j j |  _ |  j   rN |  j   |  j   } | rN t |  Sn  t d d d  i d d 6} i d  d 6| d 6g } t |  S)Nz%(counter)s resultz%(counter)s resultsr   counterrC   rD   )r/   r,   r1   r2   r3   rL   r"   r	   )r.   r,   rN   rO   r!   rD   r   r   r   r8      s    

zAutocompleteLookup.get)
rP   rQ   rR   rS   r2   re   rA   rL   r   r8   r   r   )rj   r   rY      s   
rY   )'r`   r   	functoolsr   Zdjango.httpr   Z	django.dbr   r   Zdjango.db.models.queryr   Zdjango.views.decorators.cacher   Zdjango.views.genericr   Zdjango.utils.translationr	   r
   rI   Zdjango.utils.encodingr   Zdjango.core.exceptionsr   Zdjango.contrib.admin.utilsr   Zdjango.core.serializers.jsonr   Zdjango.appsr   Zgrappelli.settingsr   r   r   r   r"   r*   r+   rT   rY   r   r   r   r   <module>   s*   <