
O'^c           @   sF  d  d l  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 d  d l m Z d  d	 l m Z m Z d  d
 l m Z m Z m Z d  d l m Z m Z m Z 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' d  d l( m) Z) e  j* d  Z+ e% i
 d e j, 6d e j- 6d e j. 6d e j/ 6d e j0 6d e j1 6d e j2 6d e j3 6d e j4 6d e j5 6 Z6 d   Z7 d   Z8 d   Z9 d   Z: d   Z; d    Z< d!   Z= d" e> f d#     YZ? d$ e> f d%     YZ@ eA eA eA d&  ZB d S('   iN(   t   OrderedDict(   t   import_module(   t   settings(   t   simplify_regex(   t   PermissionDenied(   t   Http404(   t   six(   t
   force_textt
   smart_text(   t
   exceptionst	   rendererst   serializers(   t   RegexURLPatternt   RegexURLResolvert   coreapit   uritemplatet   urlparse(   t   clone_request(   t   Response(   t   api_settings(   t
   formatting(   t   ClassLookupDict(   t   _get_pk(   t   APIViews   ^[a-zA-Z][0-9A-Za-z_]*:t   stringt   integert   numbert   booleant   filet   arrayt   objectc         C   s   g  |  D] } | j  d  j d  ^ q } t |  } t |  } | } x8 t |  D]* \ } } | | | k rV | |  } PqV qV Wd d j |  S(   Nt   /(   t   stript   splitt   mint   maxt	   enumeratet   join(   t   pathst   patht   split_pathst   s1t   s2t   commont   it   c(    (    s8   /tmp/pip-unpacked-wheel-62FVgP/rest_framework/schemas.pyt   common_path)   s    +
c         C   s   |  j  j j  } t |  j S(   N(   t   _metat   concrete_modelR   t   name(   t   modelt   meta(    (    s8   /tmp/pip-unpacked-wheel-62FVgP/rest_framework/schemas.pyt   get_pk_name5   s    c         C   s+   t  |  d d  } | d k	 o* t | t  S(   sT   
    Return `True` if the given view callback is a REST framework view/viewset.
    t   clsN(   t   getattrt   Nonet
   issubclassR   (   t   callbackR5   (    (    s8   /tmp/pip-unpacked-wheel-62FVgP/rest_framework/schemas.pyt   is_api_view:   s    c         C   sJ   x5 | d  D]) } | |  k r* i  |  | <n  |  | }  q W| |  | d <d S(   s   
    Nested dictionary insertion.

    >>> example = {}
    >>> insert_into(example, ['a', 'b', 'c'], 123)
    >>> example
    {'a': {'b': {'c': 123}}}
    iN(    (   t   targett   keyst   valuet   key(    (    s8   /tmp/pip-unpacked-wheel-62FVgP/rest_framework/schemas.pyt   insert_intoB   s
    	c         C   s"   |  t  d d d d d d g  k S(   Nt   retrievet   listt   createt   updatet   partial_updatet   destroy(   t   set(   t   action(    (    s8   /tmp/pip-unpacked-wheel-62FVgP/rest_framework/schemas.pyt   is_custom_actionR   s    c         C   sh   t  | d  r | j d k S| j   d k r2 t S|  j d  j d  } | rd d | d k rd t St S(   sP   
    Return True if the given path/method appears to represent a list view.
    RG   RA   t   getR   t   {i(   t   hasattrRG   t   lowert   FalseR    R!   t   True(   R'   t   methodt   viewt   path_components(    (    s8   /tmp/pip-unpacked-wheel-62FVgP/rest_framework/schemas.pyt   is_list_viewX   s    c         C   sN   |  \ } } } i d d 6d d 6d d 6d d 6d	 d
 6j  | d  } | | f S(   Ni    t   GETi   t   POSTi   t   PUTi   t   PATCHi   t   DELETEi   (   RI   (   t   endpointR'   RO   R9   t   method_priority(    (    s8   /tmp/pip-unpacked-wheel-62FVgP/rest_framework/schemas.pyt   endpoint_orderingh   s    
t   EndpointInspectorc           B   sG   e  Z d  Z d d d  Z d d d  Z d   Z d   Z d   Z RS(   sR   
    A class to determine the available API endpoints that a project exposes.
    c         C   sd   | d  k rW | d  k r$ t j } n  t | t j  rE t |  } n | } | j } n  | |  _ d  S(   N(	   R7   R   t   ROOT_URLCONFt
   isinstanceR   t   string_typesR   t   urlpatternst   patterns(   t   selfR`   t   urlconft   urls(    (    s8   /tmp/pip-unpacked-wheel-62FVgP/rest_framework/schemas.pyt   __init__x   s    t    c         C   s  | d k r |  j } n  g  } x | D] } | | j j } t | t  r |  j |  } | j } |  j | |  r x6 |  j	 |  D]" } | | | f }	 | j
 |	  q Wq q% t | t  r% |  j d | j d |  }
 | j |
  q% q% Wt | d t } | S(   sZ   
        Return a list of all available API endpoints by inspecting the URL conf.
        R`   t   prefixR>   N(   R7   R`   t   regext   patternR]   R   t   get_path_from_regexR9   t   should_include_endpointt   get_allowed_methodst   appendR   t   get_api_endpointst   url_patternst   extendt   sortedRZ   (   Ra   R`   Rf   t   api_endpointsRh   t
   path_regexR'   R9   RO   RX   t   nested_endpoints(    (    s8   /tmp/pip-unpacked-wheel-62FVgP/rest_framework/schemas.pyRm      s&    				c         C   s.   t  |  } | j d d  j d d  } | S(   sG   
        Given a URL conf regex, return a URI template string.
        t   <RJ   t   >t   }(   R   t   replace(   Ra   Rr   R'   (    (    s8   /tmp/pip-unpacked-wheel-62FVgP/rest_framework/schemas.pyRi      s    c         C   s6   t  |  s t S| j d  s. | j d  r2 t St S(   sI   
        Return `True` if the given endpoint should be included.
        s	   .{format}s
   .{format}/(   R:   RM   t   endswithRN   (   Ra   R'   R9   (    (    s8   /tmp/pip-unpacked-wheel-62FVgP/rest_framework/schemas.pyRj      s
    c         C   sa   t  | d  r5 g  | j j   D] } | j   ^ q Sg  | j   j D] } | d k rE | ^ qE S(   sL   
        Return a list of the valid HTTP methods for this endpoint.
        t   actionst   OPTIONSt   HEAD(   Rz   R{   (   RK   Ry   R<   t   upperR5   t   allowed_methods(   Ra   R9   RO   (    (    s8   /tmp/pip-unpacked-wheel-62FVgP/rest_framework/schemas.pyRk      s    &N(	   t   __name__t
   __module__t   __doc__R7   Rd   Rm   Ri   Rj   Rk   (    (    (    s8   /tmp/pip-unpacked-wheel-62FVgP/rest_framework/schemas.pyR[   t   s   		t   SchemaGeneratorc           B   s   e  Z i d  d 6d d 6d d 6d d 6d d	 6Z e Z d Z d Z d d d d d
  Z d d  Z	 d d  Z
 d   Z d d  Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z RS(   R@   RI   RB   t   postRC   t   putRD   t   patchRE   t   deletec         C   s~   t  s t d   | r5 | j d  r5 | d 7} n  t j |  _ t j |  _ | |  _ | |  _	 | |  _
 | |  _ d  |  _ d  S(   Ns/   `coreapi` must be installed for schema support.R   (   R   t   AssertionErrorRx   R   t   SCHEMA_COERCE_METHOD_NAMESt   coerce_method_namest   SCHEMA_COERCE_PATH_PKt   coerce_path_pkR`   Rb   t   titlet   urlR7   t	   endpoints(   Ra   R   R   R`   Rb   (    (    s8   /tmp/pip-unpacked-wheel-62FVgP/rest_framework/schemas.pyRd      s    				c         C   st   |  j  d k r9 |  j |  j |  j  } | j   |  _  n  |  j |  } | sR d St j d |  j	 d |  j
 d |  S(   sL   
        Generate a `coreapi.Document` representing the API schema.
        R   R   t   contentN(   R   R7   t   endpoint_inspector_clsR`   Rb   Rm   t	   get_linksR   t   DocumentR   R   (   Ra   t   requestt	   inspectort   links(    (    s8   /tmp/pip-unpacked-wheel-62FVgP/rest_framework/schemas.pyt
   get_schema   s    c         C   s3  t    } g  } g  } x |  j D]t \ } } } |  j | | |  } t | d t  r[ q n  |  j | | |  } | j |  | j | | | f  q W| s d S|  j |  }	 x| | D]t \ } } } |  j	 | | |  s q n  |  j
 | | |  }
 | t |	  } |  j | | |  } t | | |
  q W| S(   sq   
        Return a dictionary containing all the links that should be
        included in the API schema.
        t   exclude_from_schemaN(   R    R   t   create_viewR6   RM   t   coerce_pathRl   R7   t   determine_path_prefixt   has_view_permissionst   get_linkt   lent   get_keysR?   (   Ra   R   R   R&   t   view_endpointsR'   RO   R9   RP   Rf   t   linkt   subpathR<   (    (    s8   /tmp/pip-unpacked-wheel-62FVgP/rest_framework/schemas.pyR      s*    	c         C   s   g  } x | D] } | j  d  j d  } g  } x+ | D]# } d | k rN Pn  | j |  q8 Wd j | d   } | s| d S| j d | d  q Wt |  S(   s  
        Given a list of all paths, return the common prefix which should be
        discounted when generating a schema structure.

        This will be the longest common string that does not include that last
        component of the URL, or the last component before a path parameter.

        For example:

        /api/v1/users/
        /api/v1/users/{pk}/

        The path prefix is '/api/v1/'
        R   RJ   i(   R    R!   Rl   R%   R.   (   Ra   R&   t   prefixesR'   t
   componentst   initial_componentst	   componentRf   (    (    s8   /tmp/pip-unpacked-wheel-62FVgP/rest_framework/schemas.pyR     s    c         C   s   | j    } x6 t | d i   j   D] \ } } t | | |  q% Wd | _ i  | _ d | _ d | _ t | d d  | _	 t | d d  } | d k	 r | d k r d | _
 q | j | j    | _
 n  | d k	 r t | |  | _ n  | S(   sC   
        Given a callback, return an actual view instance.
        t
   initkwargsRy   Rz   t   metadata(    N(   R5   R6   t   itemst   setattrt   argst   kwargsR7   t   format_kwargR   t
   action_mapRG   RI   RL   R   (   Ra   R9   RO   R   RP   t   attrt   valRy   (    (    s8   /tmp/pip-unpacked-wheel-62FVgP/rest_framework/schemas.pyR   8  s     %				c         C   sL   | j  d k r t Sy | j | j   Wn t j t t f k
 rG t SXt S(   sY   
        Return `True` if the incoming request has the correct view permissions.
        N(	   R   R7   RN   t   check_permissionsR	   t   APIExceptionR   R   RM   (   Ra   R'   RO   RP   (    (    s8   /tmp/pip-unpacked-wheel-62FVgP/rest_framework/schemas.pyR   Q  s    c         C   sg   |  j  s d | k r | St t | d d  d d  } | rM t |  } n d } | j d d |  S(   s   
        Coerce {pk} path arguments into the name of the model field,
        where possible. This is cleaner for an external representation.
        (Ie. "this is an identifier", not "this is a database primary key")
        s   {pk}t   querysetR2   t   ids   {%s}N(   R   R6   R7   R4   Rw   (   Ra   R'   RO   RP   R2   t
   field_name(    (    s8   /tmp/pip-unpacked-wheel-62FVgP/rest_framework/schemas.pyR   ^  s    c         C   s#  |  j  | | |  } | |  j | | |  7} | |  j | | |  7} | |  j | | |  7} | r t g  | D] } | j d
 k ^ qp  r |  j | | |  } n d } |  j | | |  } |  j	 r | j
 d  r | d } n  t j d t j |  j	 |  d | j   d | d | d	 |  S(   sJ   
        Return a `coreapi.Link` instance for the given endpoint.
        t   formt   bodyR   i   R   RG   t   encodingt   fieldst   description(   R   R   N(   t   get_path_fieldst   get_serializer_fieldst   get_pagination_fieldst   get_filter_fieldst   anyt   locationt   get_encodingR7   t   get_descriptionR   t
   startswithR   t   LinkR   t   urljoinRL   (   Ra   R'   RO   RP   R   t   fieldR   R   (    (    s8   /tmp/pip-unpacked-wheel-62FVgP/rest_framework/schemas.pyR   o  s     .	c         C   sc  t  | d | j    } t  | | d  j } | rF t j t |   S| j   } g  | j   D] } | j	   ^ q_ } d }	 i d d 6}
 x\ | D]T } t
 j |  r | j d  \ }	 } } | j	   |
 |	 <q |
 |	 c | d 7<q Wt  | d | j    } | |
 k r|
 | j	   S| |  j k rU|  j | |
 k rU|
 |  j | j	   Sn  |
 d j	   S(   s   
        Determine a link description.

        This will be based on the method docstring if one exists,
        or else the class docstring.
        RG   Re   t   :s   
N(   R6   RL   R7   R   R   t   dedentR   t   get_view_descriptiont
   splitlinesR    t   header_regext   matcht	   partitionR   (   Ra   R'   RO   RP   t   method_namet   method_docstringR   t   linet   linest   current_sectiont   sectionst	   seperatort   leadt   header(    (    s8   /tmp/pip-unpacked-wheel-62FVgP/rest_framework/schemas.pyR     s(    %c         C   se   t  d  } t | d g   } x@ | D]8 } t | d d	  } | | k rM | S| d k r% d Sq% Wd	 S(
   sN   
        Return the 'encoding' parameter to use for a given endpoint.
        s   application/jsons!   application/x-www-form-urlencodeds   multipart/form-datat   parser_classest
   media_types   */*s   application/octet-stream(   s   application/jsons!   application/x-www-form-urlencodeds   multipart/form-dataN(   RF   R6   R7   (   Ra   R'   RO   RP   t   supported_media_typesR   t   parser_classR   (    (    s8   /tmp/pip-unpacked-wheel-62FVgP/rest_framework/schemas.pyR     s      	c         C   sO   g  } xB t  j |  D]1 } t j d | d d d t  } | j |  q W| S(   ss   
        Return a list of `coreapi.Field` instances corresponding to any
        templated path variables.
        R1   R   R'   t   required(   R   t	   variablesR   t   FieldRN   Rl   (   Ra   R'   RO   RP   R   t   variableR   (    (    s8   /tmp/pip-unpacked-wheel-62FVgP/rest_framework/schemas.pyR     s
    c   	      C   s2  | d k r g  St  | d  s# g  S| j   } t | t j  rf t j d d d d d	 t d
 d  g St | t j  s| g  Sg  } x | j	 j
   D] } | j s t | t j  r q n  | j o | d k } | j r t | j  n d } t j d | j d d d	 | d | d
 t |  } | j |  q W| S(   s   
        Return a list of `coreapi.Field` instances corresponding to any
        request body input, as determined by the serializer class.
        RU   RV   RT   t   get_serializerR1   t   dataR   R   R   t   typeR   Re   R   R   (   RU   RV   RT   (   RK   R   R]   R   t   ListSerializerR   R   RN   t
   SerializerR   t   valuest	   read_onlyt   HiddenFieldR   t	   help_textR   R   t   types_lookupRl   (	   Ra   R'   RO   RP   t
   serializerR   R   R   R   (    (    s8   /tmp/pip-unpacked-wheel-62FVgP/rest_framework/schemas.pyR     s6    	
		c         C   sE   t  | | |  s g  St | d d   s, g  S| j   } | j |  S(   Nt   pagination_class(   RR   R6   R7   R   t   get_schema_fields(   Ra   R'   RO   RP   t	   paginator(    (    s8   /tmp/pip-unpacked-wheel-62FVgP/rest_framework/schemas.pyR     s    c         C   s`   t  | | |  s g  St | d d   s, g  Sg  } x' | j D] } | |   j |  7} q< W| S(   Nt   filter_backends(   RR   R6   R7   R   R   (   Ra   R'   RO   RP   R   t   filter_backend(    (    s8   /tmp/pip-unpacked-wheel-62FVgP/rest_framework/schemas.pyR     s    c         C   s  t  | d  r | j } n. t | | |  r6 d } n |  j | j   } g  | j d  j d  D] } d | k rb | ^ qb } t |  r t | j	  d k r |  j | j   } | |  j
 k r |  j
 | } n  | | g S| d  | g Sn  | |  j
 k r|  j
 | } n  | | g S(   s  
        Return a list of keys that should be used to layout a link within
        the schema document.

        /users/                   ("users", "list"), ("users", "create")
        /users/{pk}/              ("users", "read"), ("users", "update"), ("users", "delete")
        /users/enabled/           ("users", "enabled")  # custom viewset list action
        /users/{pk}/star/         ("users", "star")     # custom viewset detail action
        /users/{pk}/groups/       ("users", "groups", "list"), ("users", "groups", "create")
        /users/{pk}/groups/{pk}/  ("users", "groups", "read"), ("users", "groups", "update"), ("users", "groups", "delete")
        RG   RA   R   RJ   i   i(   RK   RG   RR   t   default_mappingRL   R    R!   RH   R   R   R   (   Ra   R   RO   RP   RG   R   t   named_path_components(    (    s8   /tmp/pip-unpacked-wheel-62FVgP/rest_framework/schemas.pyR     s$    	N(   R~   R   R   R[   R   R7   R   R   Rd   R   R   R   R   R   R   R   R   R   R   R   R   R   R   (    (    (    s8   /tmp/pip-unpacked-wheel-62FVgP/rest_framework/schemas.pyR      s0   
"					!			,	
	c            s   t  d |  d |    | d k rW t j t j k rH t j t j g  q] t j g  n |  d t f    f d     Y} | j   S(   s   
    Return a schema view.
    R   R   t
   SchemaViewc              s)   e  Z e Z e Z  Z   f d    Z RS(   c            s4     j  |  } | d  k r* t j    n  t |  S(   N(   R   R7   R	   R   R   (   Ra   R   R   R   t   schema(   t	   generator(    s8   /tmp/pip-unpacked-wheel-62FVgP/rest_framework/schemas.pyRI   P  s    (   R~   R   RN   t   _ignore_model_permissionsR   t   renderer_classesRI   (    (   R   t   rclasses(    s8   /tmp/pip-unpacked-wheel-62FVgP/rest_framework/schemas.pyR   K  s   N(	   R   R7   R
   t   BrowsableAPIRendererR   t   DEFAULT_RENDERER_CLASSESt   CoreJSONRendererR   t   as_view(   R   R   R   R   (    (   R   R   s8   /tmp/pip-unpacked-wheel-62FVgP/rest_framework/schemas.pyt   get_schema_view>  s    (C   t   ret   collectionsR    t	   importlibR   t   django.confR   t   django.contrib.admindocs.viewsR   t   django.core.exceptionsR   t   django.httpR   t   django.utilsR   t   django.utils.encodingR   R   t   rest_frameworkR	   R
   R   t   rest_framework.compatR   R   R   R   R   t   rest_framework.requestR   t   rest_framework.responseR   t   rest_framework.settingsR   t   rest_framework.utilsR   t"   rest_framework.utils.field_mappingR   t   rest_framework.utils.model_metaR   t   rest_framework.viewsR   t   compileR   R   t   IntegerFieldt
   FloatFieldt   DecimalFieldt   BooleanFieldt	   FileFieldt   MultipleChoiceFieldt   ManyRelatedFieldR   R   R   R.   R4   R:   R?   RH   RR   RZ   R   R[   R   R7   R   (    (    (    s8   /tmp/pip-unpacked-wheel-62FVgP/rest_framework/schemas.pyt   <module>   sP   (








							R y