B
    @Dbz
                 @   sf   d Z ddlmZ ddlZddlmZ ddlmZmZ G dd deZ	dd	d
Z
dd Zdd ZeZdS )z
Django's JsonResponse and JsonpResponse
class JsonResponse(data, encoder=DjangoJSONEncoder, safe=True, **kwargs) is New in Django 1.7
Ref: https://docs.djangoproject.com/en/1.11/ref/request-response/#jsonresponse-objects
    )unicode_literalsN)DjangoJSONEncoder)HttpResponseJsonResponsec                   s$   e Zd ZdZef fdd	Z  ZS )JsonpResponsez! Docstring for JsonpHttpResponse c          
      s   y"d |tj|f|d|i}W n2 ttfk
rT } zd ||}W d d }~X Y nX |dd tt| jf d|i| d S )Nz	{0}({1});Zcalsz"{0} can't be jsonlized, due to {1}Zcontent_typezapplication/jsoncontent)	formatjsondumps
ValueError	TypeError
setdefaultsuperr   __init__)selfcallbackdataencoderargskwargsr   err)	__class__ 3G:\easytimepro\master/mysite/admin\json_response.pyr      s    "zJsonpResponse.__init__)__name__
__module____qualname____doc__r   r   __classcell__r   r   )r   r   r      s   r   Fc                s    fdd}|S )Nc                s    fdd}|S )zP
        A decorator that takes a view response and turns it into json.
        c                s0    | f||}t |tttfr$|S t|dS )N)safe)
isinstancer   r   r   )requestr   r   objects)funcr   r   r   	decorator+   s    z6json_response.<locals>.json_wrapper.<locals>.decoratorr   )r#   r$   )r   )r#   r   json_wrapper'   s    z#json_response.<locals>.json_wrapperr   )r   r%   r   )r   r   json_response&   s    r&   c                s    fdd}|S )zJ
    A decorator thats takes a view response and turns it into jsonp.
    c                s8    | f||}t |tttfr$|S t| jdd|S )Nr    )r    r   r   r   GETget)r!   r   r   r"   )r#   r   r   r$   :   s    z!jsonp_response.<locals>.decoratorr   )r#   r$   r   )r#   r   jsonp_response5   s    r*   c                s    fdd}|S )z
    A decorator thats takes a view response and turns it into json.
    If a callback is added through GET or POST the response is JSONP.
    c                sJ    | f||}t |tttfr$|S d| jkrBt| jdd|S t|S )Nr   r'   )r    r   r   r   r(   r)   )r!   r   r   r"   )r#   r   r   r$   I   s
     z auto_response.<locals>.decoratorr   )r#   r$   r   )r#   r   auto_responseC   s    r+   )F)r   Z
__future__r   r	   Zdjango.core.serializers.jsonr   Zdjango.httpr   r   r   r&   r*   r+   ZLazableJSONEncoderr   r   r   r   <module>   s   
