
^Q\                 @   s   d  Z  d d l 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 Gd	 d
   d
 e  Z Gd d   d e e  Z d S)z$Base views for autocomplete widgets.    N)http)get_permission_codename)ImproperlyConfigured)HttpResponseBadRequestHttpResponseNotAllowed)six)BaseListViewc                   s.   e  Z d  Z d Z d Z   f d d   Z   S)	ViewMixina  Common methods for autocomplete views.

    It is assumed this view will be used in conjunction with a Django
    :py:class:`View` based class that will that will implement OPTIONS.

    .. py:attribute:: forwarded

        Dict of field values that were forwarded from the form, may be used to
        filter autocompletion results based on the form state. See
        ``linked_data`` example for reference.

    .. py:attribute:: q

        Query string as typed by the user in the autocomplete field.
    GETPOSTc                s   | j  j   |  j k r% t |  j  Sy. t j t | | j   j d d   |  _ Wn t	 k
 rq t
 d  SYn Xt |  j t  s t
 d  S| j j d d  |  _ t t |   j | | |  S)z*Set :py:attr:`forwarded` and :py:attr:`q`.Zforwardz{}zInvalid JSON datazNot a JSON objectq )methodupperhttp_method_allowedr   jsonloadsgetattrgetZ	forwarded
ValueErrorr   
isinstancedictr
   r   superr	   dispatch)selfrequestargskwargs)	__class__ >/var/www/dbchiro/venv/lib/python3.4/site-packages/dal/views.pyr       s    (
zViewMixin.dispatch)zGETzPOST)__name__
__module____qualname____doc__r   r   r   r   )r   r    r	      s   r	   c                   s   e  Z d  Z d Z d Z d Z d Z d d   Z d d   Z d	 d
   Z	   f d d   Z
 d d   Z d d   Z d d   Z   S)BaseQuerySetViewa  Base view to get results from a QuerySet.

    .. py:attribute:: create_field

        Name of the field to use to create missing values. For example, if
        create_field='title', and the user types in "foo", then the
        autocomplete view will propose an option 'Create "foo"' if it can't
        find any value matching "foo". When the user does click 'Create "foo"',
        the autocomplete script should POST to this view to create the object
        and get back the newly created object id.
    
   resultsNc             C   s   | d j    S)z.For widgets that have infinite-scroll feature.Zpage_obj)Zhas_next)r   contextr   r   r    has_moreD   s    zBaseQuerySetView.has_morec             C   s   t  | j  S)zReturn the value of a result.)strpk)r   resultr   r   r    get_result_valueH   s    z!BaseQuerySetView.get_result_valuec             C   s   t  j |  S)zReturn the label of a result.)r   	text_type)r   r,   r   r   r    get_result_labelL   s    z!BaseQuerySetView.get_result_labelc                s:   t  t |   j   } |  j r6 | j d |  j  } n  | S)z"Filter the queryset with GET['q'].Zname__icontains)r   r%   get_querysetr   filter)r   qs)r   r   r    r0   P   s    	zBaseQuerySetView.get_querysetc             C   s   |  j    j i | |  j 6  S)zCreate an object given a text.)r0   createcreate_field)r   textr   r   r    create_objectY   s    zBaseQuerySetView.create_objectc             C   sQ   | j  j   s d S|  j   j j } t d |  } | j  j d | j | f  S)z:Return True if the user has the permission to add a model.Faddz%s.%s)userZis_authenticatedr0   ZmodelZ_metar   Zhas_permZ	app_label)r   r   optsZcodenamer   r   r    has_add_permission]   s
    z#BaseQuerySetView.has_add_permissionc             C   s   |  j  |  s t j   S|  j s1 t d   n  | j j d d  } | d k r\ t j   S|  j |  } t j	 i | j
 d 6t j |  d 6 S)z9Create an object given a text after checking permissions.zMissing "create_field"r5   Nid)r:   r   ZHttpResponseForbiddenr4   r   r   r   r   r6   ZJsonResponser+   r   r.   )r   r   r5   r,   r   r   r    postf   s    
	
	
zBaseQuerySetView.post)r!   r"   r#   r$   Zpaginate_byZcontext_object_namer4   r)   r-   r/   r0   r6   r:   r<   r   r   )r   r    r%   3   s   		r%   )r$   r   Zdjangor   Zdjango.contrib.authr   Zdjango.core.exceptionsr   Zdjango.httpr   r   Zdjango.utilsr   Zdjango.views.generic.listr   objectr	   r%   r   r   r   r    <module>   s   &