ó
¸9—^c           @  sÎ   d  d l  m 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 e f d	 „  ƒ  YZ d
 e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   iÿÿÿÿ(   t   unicode_literalsN(   t   serializers(   t   ImproperlyConfigured(   t   DjangoJSONEncoder(   t   HttpResponset   HttpResponseBadRequest(   t   sixt   JSONResponseMixinc           B  sG   e  Z d  Z d Z d Z e Z d „  Z d „  Z	 d d „ Z
 d „  Z RS(   uf   
    A mixin that allows you to easily serialize simple data such as a dict or
    Django models.
    c         C  sY   |  j  d  k	 rL t |  j  t j t j f ƒ rL t d j |  j j	 ƒ ƒ ‚ n  |  j  pX d S(   Nu[   {0} is missing a content type. Define {0}.content_type, or override {0}.get_content_type().u   application/json(
   t   content_typet   Nonet
   isinstanceR   t   string_typest	   text_typeR   t   formatt	   __class__t   __name__(   t   self(    (    s4   /tmp/pip-unpacked-wheel-20cZHT/braces/views/_ajax.pyt   get_content_type   s    	c         C  s5   |  j  d  k r i  |  _  n  |  j  j d t ƒ |  j  S(   Nu   ensure_ascii(   t   json_dumps_kwargsR	   t
   setdefaultt   False(   R   (    (    s4   /tmp/pip-unpacked-wheel-20cZHT/braces/views/_ajax.pyt   get_json_dumps_kwargs   s    iÈ   c         C  sF   t  j | d |  j |  j ƒ  j d ƒ } t | d |  j ƒ  d | ƒS(   u‚   
        Limited serialization for shipping plain data. Do not use for models
        or other complex or custom objects.
        t   clsu   utf-8R   t   status(   t   jsont   dumpst   json_encoder_classR   t   encodeR   R   (   R   t   context_dictR   t   json_context(    (    s4   /tmp/pip-unpacked-wheel-20cZHT/braces/views/_ajax.pyt   render_json_response$   s    	c         K  s+   t  j d | |  } t | d |  j ƒ  ƒS(   u¦   
        Serializes objects using Django's builtin JSON serializer. Additional
        kwargs can be used the same way for django.core.serializers.serialize.
        u   jsonR   (   R   t	   serializeR   R   (   R   t   objectst   kwargst	   json_data(    (    s4   /tmp/pip-unpacked-wheel-20cZHT/braces/views/_ajax.pyt   render_json_object_response1   s    N(   R   t
   __module__t   __doc__R	   R   R   R   R   R   R   R   R#   (    (    (    s4   /tmp/pip-unpacked-wheel-20cZHT/braces/views/_ajax.pyR      s   	
	t   AjaxResponseMixinc           B  s;   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   u´   
    Mixin allows you to define alternative methods for ajax requests. Similar
    to the normal get, post, and put methods, you can use get_ajax, post_ajax,
    and put_ajax.
    c         O  s   | j  j ƒ  } | j ƒ  rs | |  j k rs t |  d j | ƒ |  j ƒ } | |  _ | |  _ | |  _	 | | | | Ž St
 t |  ƒ j | | | Ž S(   Nu   {0}_ajax(   t   methodt   lowert   is_ajaxt   http_method_namest   getattrR   t   http_method_not_allowedt   requestt   argsR!   t   superR&   t   dispatch(   R   R-   R.   R!   t   request_methodt   handler(    (    s4   /tmp/pip-unpacked-wheel-20cZHT/braces/views/_ajax.pyR0   @   s    			c         O  s   |  j  | | | Ž S(   N(   t   get(   R   R-   R.   R!   (    (    s4   /tmp/pip-unpacked-wheel-20cZHT/braces/views/_ajax.pyt   get_ajaxN   s    c         O  s   |  j  | | | Ž S(   N(   t   post(   R   R-   R.   R!   (    (    s4   /tmp/pip-unpacked-wheel-20cZHT/braces/views/_ajax.pyt	   post_ajaxQ   s    c         O  s   |  j  | | | Ž S(   N(   R3   (   R   R-   R.   R!   (    (    s4   /tmp/pip-unpacked-wheel-20cZHT/braces/views/_ajax.pyt   put_ajaxT   s    c         O  s   |  j  | | | Ž S(   N(   R3   (   R   R-   R.   R!   (    (    s4   /tmp/pip-unpacked-wheel-20cZHT/braces/views/_ajax.pyt   delete_ajaxW   s    (   R   R$   R%   R0   R4   R6   R7   R8   (    (    (    s4   /tmp/pip-unpacked-wheel-20cZHT/braces/views/_ajax.pyR&   :   s   				t   JsonRequestResponseMixinc           B  sB   e  Z d  Z e Z i d g d 6Z d d „ Z d „  Z d „  Z	 RS(   uõ  
    Extends JSONResponseMixin.  Attempts to parse request as JSON.  If request
    is properly formatted, the json is saved to self.request_json as a Python
    object.  request_json will be None for imparsible requests.
    Set the attribute require_json to True to return a 400 "Bad Request" error
    for requests that don't contain JSON.

    Note: To allow public access to your view, you'll need to use the
    csrf_exempt decorator or CsrfExemptMixin.

    Example Usage:

        class SomeView(CsrfExemptMixin, JsonRequestResponseMixin):
            def post(self, request, *args, **kwargs):
                do_stuff_with_contents_of_request_json()
                return self.render_json_response(
                    {'message': 'Thanks!'})
    u   Improperly formatted requestu   errorsc         C  sX   | d  k r |  j } n  t j | d |  j |  j ƒ  j d ƒ } t | d |  j ƒ  ƒS(   NR   u   utf-8R   (	   R	   t   error_response_dictR   R   R   R   R   R   R   (   R   t
   error_dictR   (    (    s4   /tmp/pip-unpacked-wheel-20cZHT/braces/views/_ajax.pyt   render_bad_request_responseq   s    	c         C  s9   y  t  j |  j j j d ƒ ƒ SWn t k
 r4 d  SXd  S(   Nu   utf-8(   R   t   loadsR-   t   bodyt   decodet
   ValueErrorR	   (   R   (    (    s4   /tmp/pip-unpacked-wheel-20cZHT/braces/views/_ajax.pyt   get_request_json|   s     c         O  sz   | |  _  | |  _ | |  _ |  j ƒ  |  _ t | j d k |  j |  j d  k g ƒ r^ |  j	 ƒ  St
 t |  ƒ j | | | Ž S(   Nu   OPTIONS(   R-   R.   R!   RA   t   request_jsont   allR'   t   require_jsonR	   R<   R/   R9   R0   (   R   R-   R.   R!   (    (    s4   /tmp/pip-unpacked-wheel-20cZHT/braces/views/_ajax.pyR0   ‚   s    			
N(
   R   R$   R%   R   RD   R:   R	   R<   RA   R0   (    (    (    s4   /tmp/pip-unpacked-wheel-20cZHT/braces/views/_ajax.pyR9   [   s   	t   JSONRequestResponseMixinc           B  s   e  Z RS(    (   R   R$   (    (    (    s4   /tmp/pip-unpacked-wheel-20cZHT/braces/views/_ajax.pyRE   ’   s   (   t
   __future__R    R   t   django.coreR   t   django.core.exceptionsR   t   django.core.serializers.jsonR   t   django.httpR   R   t   django.utilsR   t   objectR   R&   R9   RE   (    (    (    s4   /tmp/pip-unpacked-wheel-20cZHT/braces/views/_ajax.pyt   <module>   s   /!7