
    +}&iH                         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mZmZmZmZmZmZmZmZ d
dlmZmZ  e j4                  e      Z G d de      Zy)    N)OrderedDictis_form_media_type)
AutoSchema)
is_success   )openapi)SwaggerGenerationError)	filter_noneforce_real_strforce_serializer_instanceget_consumesget_producesguess_response_statusmerge_paramsno_bodyparam_list_to_odict   )ViewInspectorcall_view_methodc                        e Zd Z	 d f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d Zd Zd Zd Zd ZddZd Zd Zd Zd ZddZd Zd Z xZS )SwaggerAutoSchemac                     t         t        |   ||||||       t               | _        || j                  _        || _        y N)superr   __init__r   _schviewoperation_keys)	selfr   pathmethod
componentsrequest	overridesr   	__class__s	           P/usr/local/footviz/venv/lib/python3.12/site-packages/drf_yasg/inspectors/view.pyr   zSwaggerAutoSchema.__init__   s@     	/$
GY	
 L			,    c                 D   |xs | j                   }| j                         }| j                         }| j                  |      }| j	                         }||z   }t        |      }| j                  |      }| j                  |      }| j                         \  }}	| j                         }
|
t        |
t              sJ d       | j                         }| j                  |      }| j                         }t        j                   |t#        |	      t#        |      ||||||
|
      S )Nz7security must be a list of security requirement objects)
operation_iddescriptionsummary	responses
parametersconsumesproducestagssecurity
deprecated)r   r   r   get_request_body_parametersget_query_parametersr   add_manual_parametersget_operation_idget_summary_and_descriptionget_security
isinstancelistis_deprecatedget_tagsget_responsesr	   	Operationr   )r    r   r/   r0   bodyqueryr.   r*   r,   r+   r2   r3   r1   r-   s                 r'   get_operationzSwaggerAutoSchema.get_operation%   s&   '>4+>+>$$&$$&//9))+E\
 ,
//
;
,,^<#??A$$&:h#= 	
E	
= '')
}}^,&&(	  %&{3"7+!!
 	
r(   c                 ,   | j                         }d}|g S t        |t        j                  j                        r|}t        d |D              r|t        d      | j                  |      S || j                  |      }|| j                  |      gS g S )a$  Return the request body parameters for this view. |br|
        This is either:

        -  a list with a single object Parameter with a :class:`.Schema` derived from
            the request serializer
        -  a list of primitive Parameters parsed as form data

        :param list[str] consumes: a list of accepted MIME types as returned by
            :meth:`.get_consumes`
        :return: a (potentially empty) list of :class:`.Parameter`\ s either
            ``in: body`` or ``in: formData``
        :rtype: list[openapi.Parameter]
        Nc              3   2   K   | ]  }t        |        y wr   r   .0encodings     r'   	<genexpr>z@SwaggerAutoSchema.get_request_body_parameters.<locals>.<genexpr>_   s     E!(+E   z$form request body cannot be a Schema)
get_request_serializerr:   r	   SchemaOR_REFanyr
   get_request_form_parametersget_request_body_schemamake_body_parameter)r    r/   
serializerschemas       r'   r4   z-SwaggerAutoSchema.get_request_body_parametersI   s     002
Ij'.."7"78FEHEE!,-STT33J??~55jA9?9KD,,V45SQSSr(   c                 .    t        | j                  d      S )zReturn the serializer as defined by the view's ``get_serializer()`` method.

        :return: the view's ``Serializer``
        :rtype: rest_framework.serializers.Serializer
        get_serializer)r   r   r    s    r'   get_view_serializerz%SwaggerAutoSchema.get_view_serializerh   s      		+;<<r(   c                 D   | j                   j                  dd      }||t        u rt        S | j                  | j                  vr*t        ddj                  | j                        z   dz         t        |t        j                  j                        r|S t        |      S |S )zWParse the request_body key in the override dict. This method is not public
        API.request_bodyNz%request_body can only be applied to (,z=); are you looking for query_serializer or manual_parameters?)r%   getr   r"   body_methodsr
   joinr:   r	   rK   rL   r   r    body_overrides     r'   _get_request_body_overridez,SwaggerAutoSchema._get_request_body_overridep   s     **>4@$'{{$"3"33,;hht0012UV 
 -)>)>?$$,];;r(   c                     | j                         }|(| j                  | j                  v r| j                         S |t        u ry|S )a  Return the request serializer (used for parsing the request payload) for this
        endpoint.

        :return: the request serializer, or one of :class:`.Schema`,
            :class:`.SchemaRef`, ``None``
        :rtype: rest_framework.serializers.Serializer
        N)r_   r"   implicit_body_methodsrV   r   r]   s     r'   rJ   z(SwaggerAutoSchema.get_request_serializer   sI     779 T[[D4N4N%N++--G#r(   c                 D    | j                  |t        j                        S )zGiven a Serializer, return a list of ``in: formData``
        :class:`.Parameter`\ s.

        :param serializer: the view's request serializer as returned by
            :meth:`.get_request_serializer`
        :rtype: list[openapi.Parameter]
        in_)serializer_to_parametersr	   IN_FORMr    rQ   s     r'   rN   z-SwaggerAutoSchema.get_request_form_parameters   s     ,,ZW__,MMr(   c                 $    | j                  |      S )a  Return the :class:`.Schema` for a given request's body data. Only applies to
        PUT, PATCH and POST requests.

        :param serializer: the view's request serializer as returned by
            :meth:`.get_request_serializer`
        :rtype: openapi.Schema
        )serializer_to_schemarg   s     r'   rO   z)SwaggerAutoSchema.get_request_body_schema   s     ((44r(   c                 P    t        j                  dt         j                  d|      S )zGiven a :class:`.Schema` object, create an ``in: body`` :class:`.Parameter`.

        :param openapi.Schema schema: the request body schema
        :rtype: openapi.Parameter
        dataT)namerd   requiredrR   )r	   	ParameterIN_BODY)r    rR   s     r'   rP   z%SwaggerAutoSchema.make_body_parameter   s$       W__tF
 	
r(   c                    | j                   j                  dd      xs g }t        d |D              rt        d      t        d |D              rt        d |D              }|s t        d | j	                         D              st        d      | j
                  | j                  vr*t        d	d
j                  | j                        z   dz         t        ||      S )a   Add/replace parameters from the given list of automatically generated request
        parameters.

        :param list[openapi.Parameter] parameters: generated parameters
        :return: modified parameters
        :rtype: list[openapi.Parameter]
        manual_parametersNc              3   V   K   | ]!  }|j                   t        j                  k(   # y wr   rd   r	   ro   rF   params     r'   rH   z:SwaggerAutoSchema.add_manual_parameters.<locals>.<genexpr>   "      
-2EII(
   ')zDspecify the body parameter as a Schema or Serializer in request_bodyc              3   V   K   | ]!  }|j                   t        j                  k(   # y wr   )rd   r	   rf   rt   s     r'   rH   z:SwaggerAutoSchema.add_manual_parameters.<locals>.<genexpr>   rv   rw   c              3   V   K   | ]!  }|j                   t        j                  k(   # y wr   rs   rt   s     r'   rH   z:SwaggerAutoSchema.add_manual_parameters.<locals>.<genexpr>   s"      %16		W__,%rw   c              3   2   K   | ]  }t        |        y wr   r   rE   s     r'   rH   z:SwaggerAutoSchema.add_manual_parameters.<locals>.<genexpr>   s      -19"8,-rI   z~cannot add form parameters when the request has a request body; did you forget to set an appropriate parser class on the view?z(form parameters can only be applied to (rY   z) HTTP methods)	r%   rZ   rM   r
   r   r"   r[   r\   r   )r    r.   rq   has_body_parameters       r'   r6   z'SwaggerAutoSchema.add_manual_parameters   s    !NN../BDIOR 
6G
 
 )V   
6G
 
 "% %:D% " " -=A=N=N=P- * -U  {{$"3"33,((4#4#4568HI 
 J(9::r(   c                 l    | j                         }t        j                  | j                  |            S )zGet the possible responses for this view as a swagger :class:`.Responses`
        object.

        :return: the documented responses
        :rtype: openapi.Responses
        )r-   )get_response_serializersr	   	Responsesget_response_schemas)r    response_serializerss     r'   r>   zSwaggerAutoSchema.get_responses   s5      $<<>  //0DE
 	
r(   c                 Z    | j                         }|r
|t        ur|S | j                         S )a  Return the default response serializer for this endpoint. This is derived
        from either the ``request_body`` override or the request serializer
        (:meth:`.get_view_serializer`).

        :return: response serializer, :class:`.Schema`, :class:`.SchemaRef`, ``None``
        )r_   r   rV   r]   s     r'   get_default_response_serializerz1SwaggerAutoSchema.get_default_response_serializer   s1     779]'9  ''))r(   c                    | j                   j                         }t        |      }d}|dv r| j                         }|xs d}|r/t	        |t
        j                        s| j                  |      xs d}|rZ| j                         r%t        j                  t
        j                  |      }| j                         r| j                  |      xs |}t        t        |      |i      S )zGet the default responses determined for this view from the request
        serializer and request method.

        :type: dict[str, openapi.Schema]
         )rZ   postputpatch)typeitems)r"   lowerr   r   r:   r	   rK   ri   has_list_response
TYPE_ARRAYshould_pageget_paginated_responser   str)r    r"   default_statusdefault_schemas       r'   get_default_responsesz'SwaggerAutoSchema.get_default_responses   s     ""$.v644!AACN'-2*^W^^"L!66~FL"N%%'!( ++>" !//?Q>  C/@AAr(   c                 *   | j                   j                  dd      xs i }t        d |j                         D              }t               }t	        d |D              s| j                         }|j                  d |j                         D               |S )a  Return the response codes that this view is expected to return, and the
        serializer for each response body. The return value should be a dict where the
        keys are possible status codes, and values are either strings,
        ``Serializer``\ s, :class:`.Schema`, :class:`.SchemaRef` or :class:`.Response`
        objects. See
        :func:`@swagger_auto_schema <.swagger_auto_schema>` for more details.

        :return: the response serializers
        :rtype: dict
        r-   Nc              3   <   K   | ]  \  }}t        |      |f  y wr   r   rF   scresps      r'   rH   z=SwaggerAutoSchema.get_response_serializers.<locals>.<genexpr>  s!      '
 (DSWdO'
   c              3   P   K   | ]  }|d k7  s	t        t        |               yw)defaultN)r   int)rF   r   s     r'   rH   z=SwaggerAutoSchema.get_response_serializers.<locals>.<genexpr>   s     U2R9_:c"g&Us   
&&c              3   <   K   | ]  \  }}t        |      |f  y wr   r   r   s      r'   rH   z=SwaggerAutoSchema.get_response_serializers.<locals>.<genexpr>#  s     RXR#b'4Rr   )r%   rZ   r   r   rM   r   update)r    manual_responsesr-   s      r'   r}   z*SwaggerAutoSchema.get_response_serializers  s      >>--k4@FB& '
,<,B,B,D'
 
  M	U1AUU224IR9I9O9O9QRRr(   c                    t               }|j                         D ]T  \  }}t        |t              r!t	        j
                  t        |            }n|s;t        |t        j
                        rh|}t        |d      rt        |j                  t        j                  j                        st        |j                        }| j                  |      |_        nt        |t        j                  j                        rt	        j
                  d|      }nNt        |t        j                        r|}n1t        |      }t	        j
                  d| j                  |            }||t        |      <   W |S )aC  Return the :class:`.openapi.Response` objects calculated for this view.

        :param dict response_serializers: response serializers as returned by
            :meth:`.get_response_serializers`
        :return: a dictionary of status code to :class:`.Response` object
        :rtype: dict[str, openapi.Response]
        )r+   rR   r   )r+   rR   )r   r   r:   r   r	   Responser   hasattrrR   rK   rL   r   ri   _Ref)r    r   r-   r   rQ   responses         r'   r   z&SwaggerAutoSchema.get_response_schemas&  s)     M	288: 	*NB
*c*"++z8RSJ(8(89%8X.zOOW^^%:%:8 ";8??!KJ&*&?&?
&KHOJ(=(=>"++ "% J5%6zB
"++ "44Z@
 "*Ic"g5	*8 r(   c                 X    | j                   j                  dd      }|t        |      }|S )zReturn the query serializer (used for parsing query parameters) for this
        endpoint.

        :return: the query serializer, or ``None``
        query_serializerN)r%   rZ   r   )r    r   s     r'   get_query_serializerz&SwaggerAutoSchema.get_query_serializerM  s4      >>--.@$G'89IJr(   c                 l   | j                         | j                         z   }| j                         }g }|{| j                  |t        j
                        }t        t        t        |            t        t        |            z        dk7  r%t        d| j                  d| j                        ||z   S )zdReturn the query parameters accepted by this view.

        :rtype: list[openapi.Parameter]
        rc   r   zmyour query_serializer contains fields that conflict with the filter_backend or paginator_class on the view -  )get_filter_parametersget_pagination_parametersr   re   r	   IN_QUERYlensetr   r
   r"   r!   )r    natural_parametersr   serializer_parameterss       r'   r5   z&SwaggerAutoSchema.get_query_parametersX  s     &&(4+I+I+KK 	  446 "'$($A$A g&6&6 %B %!
 +,>?@-.CDEF 	 - {{DII/  "$999r(   c                     |xs | j                   }| j                  j                  dd      }|sdj                  |      }|S )a}  Return an unique ID for this operation. The ID must be unique across
        all :class:`.Operation` objects in the API.

        :param tuple[str] operation_keys: an array of keys derived from the path
            describing the hierarchical layout of this view in the API; e.g.
            ``('snippets', 'list')``, ``('snippets', 'retrieve')``, etc.
        :rtype: str
        r*   r   _)r   r%   rZ   r\   )r    r   r*   s      r'   r7   z"SwaggerAutoSchema.get_operation_idw  sA     (>4+>+>~~))."=88N3Lr(   c                     d}d}|j                  dd      }t        |      dk(  r<|d   j                         |d<   t        |d         |k  r|\  }}|j                         }||fS )a[  Decide if and how to split a summary out of the given description. The
        default implementation uses the first paragraph of the description as a summary
        if it is less than 120 characters long.

        :param description: the full description to be analyzed
        :return: summary and description
        :rtype: (str,str)
        Nx   z

r   r   r   )splitr   strip)r    r+   r,   summary_max_lensectionss        r'   split_summary_from_descriptionz0SwaggerAutoSchema.split_summary_from_description  s{      	 $$VQ/x=A"1+++-HQK8A;/1'/$)//1##r(   c                 V   | j                   j                  dd      }| j                   j                  dd      }|l| j                  j                  | j                  | j
                        xs d}|j                         j                  dd      }|r|| j                  |      \  }}||fS )zReturn an operation summary and description determined from the view's
        docstring.

        :return: summary and description
        :rtype: (str,str)
        operation_descriptionNoperation_summaryr   )	r%   rZ   r   get_descriptionr!   r"   r   replacer   )r    r+   r,   s      r'   r8   z-SwaggerAutoSchema.get_summary_and_description  s     nn(()@$G..$$%8$?))33DIIt{{KQrK%++-55dB?K'+'J'J;'W$##r(   c                 :    | j                   j                  dd      S )a\  Return a list of security requirements for this operation.

        Returning an empty list marks the endpoint as unauthenticated (i.e. removes all
        accepted authentication schemes). Returning ``None`` will inherit the top-level
        security requirements.

        :return: security requirements
        :rtype: list[dict[str,list[str]]]r2   Nr%   rZ   rU   s    r'   r9   zSwaggerAutoSchema.get_security  s     ~~!!*d33r(   c                 :    | j                   j                  dd      S )zReturn ``True`` if this operation is to be marked as deprecated.

        :return: deprecation status
        :rtype: bool
        r3   Nr   rU   s    r'   r<   zSwaggerAutoSchema.is_deprecated  s     ~~!!,55r(   c                 l    |xs | j                   }| j                  j                  d      }|s|d   g}|S )a  Get a list of tags for this operation. Tags determine how operations relate
        with each other, and in the UI each tag will show as a group containing the
        operations that use it. If not provided in overrides, tags will be inferred
        from the operation url.

        :param tuple[str] operation_keys: an array of keys derived from the path
            describing the hierarchical layout of this view in the API; e.g.
            ``('snippets', 'list')``, ``('snippets', 'retrieve')``, etc.
        :rtype: list[str]
        r1   r   )r   r%   rZ   )r    r   r1   s      r'   r=   zSwaggerAutoSchema.get_tags  s=     (>4+>+>~~!!&)"1%&Dr(   c                 n    | j                   j                  d      xs t        | j                               S )zTReturn the MIME types this endpoint can consume.

        :rtype: list[str]
        r/   )r%   rZ   r   get_parser_classesrU   s    r'   r   zSwaggerAutoSchema.get_consumes  s,    
 ~~!!*-Xd>U>U>W1XXr(   c                 n    | j                   j                  d      xs t        | j                               S )zTReturn the MIME types this endpoint can produce.

        :rtype: list[str]
        r0   )r%   rZ   r   get_renderer_classesrU   s    r'   r   zSwaggerAutoSchema.get_produces  s3    
 ~~!!*- 
%%'2
 	
r(   r   )__name__
__module____qualname__r   rB   r4   rV   r_   rJ   rN   rO   rP   r6   r>   r   r   r}   r   r   r5   r7   r   r8   r9   r<   r=   r   r   __classcell__)r&   s   @r'   r   r      s    QU-"
HT>=($N5
#;J

*B:.%N	 :> $.$&	46&Y
r(   r   )loggingcollectionsr   rest_framework.requestr   rest_framework.schemasr   rest_framework.statusr   r   r	   errorsr
   utilsr   r   r   r   r   r   r   r   r   baser   r   	getLoggerr   loggerr    r(   r'   <module>r      sN     # 5 - ,  +
 
 
 2			8	$K
 K
r(   