o
    MfD                     @   s   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 eZG dd deZdS )    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                       s   e Zd Zd4 fdd	Zd4ddZdd Zdd	 Zd
d Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd4d$d%Zd&d' Zd(d) Zd*d+ Zd,d- Zd4d.d/Zd0d1 Zd2d3 Z  ZS )5SwaggerAutoSchemaNc                    s4   t t| |||||| t | _|| j_|| _d S N)superr   __init__r   _schviewoperation_keys)selfr   pathmethod
componentsrequest	overridesr   	__class__ Z/var/www/html/analyze/labelStudio/lib/python3.10/site-packages/drf_yasg/inspectors/view.pyr      s   
zSwaggerAutoSchema.__init__c                 C   s   |p| j }|  }|  }| |}|  }|| }t|}| |}| |}|  \}}	| 	 }
|
d u s?t
|
ts?J d|  }| |}|  }tj|t|	t|||||||
|d
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,   r-   bodyqueryr+   r'   r)   r(   r/   r0   r.   r*   r%   r%   r&   get_operation   s6   




zSwaggerAutoSchema.get_operationc                 C   s   |   }d}|du rg S t|tjjr|}tdd |D r+|dur&td| |S |du r4| |}|dur>| 	|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                 s       | ]}t |V  qd S r   r   .0encodingr%   r%   r&   	<genexpr>O       z@SwaggerAutoSchema.get_request_body_parameters.<locals>.<genexpr>z$form request body cannot be a Schema)
get_request_serializerr7   r   SchemaOR_REFanyr	   get_request_form_parametersget_request_body_schemamake_body_parameter)r   r,   
serializerschemar%   r%   r&   r1   <   s   

z-SwaggerAutoSchema.get_request_body_parametersc                 C   s   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   r%   r%   r&   get_view_serializerX   s   z%SwaggerAutoSchema.get_view_serializerc                 C   sd   | j dd}|dur0|tu rtS | j| jvr#tdd| j d t|tj	j
r,|S t|S |S )zOParse 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	   joinr7   r   rG   rH   r   r   body_overrider%   r%   r&   _get_request_body_override`   s   z,SwaggerAutoSchema._get_request_body_overridec                 C   s4   |   }|du r| j| jv r|  S |tu rdS |S )zReturn 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)rY   r   implicit_body_methodsrQ   r   rW   r%   r%   r&   rF   p   s   z(SwaggerAutoSchema.get_request_serializerc                 C   s   | j |tjd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   rM   r%   r%   r&   rJ      s   z-SwaggerAutoSchema.get_request_form_parametersc                 C   s
   |  |S )zReturn 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_schemar_   r%   r%   r&   rK      s   
z)SwaggerAutoSchema.get_request_body_schemac                 C   s   t jdt jd|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)namer\   requiredrN   )r   	ParameterIN_BODY)r   rN   r%   r%   r&   rL      s   z%SwaggerAutoSchema.make_body_parameterc                 C   s   | j ddpg }tdd |D rtdtdd |D rKtdd |D }|s5tdd |  D s9td	| j| jvrKtd
d| j d t||S )zAdd/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                 s       | ]	}|j tjkV  qd S r   r\   r   re   rB   paramr%   r%   r&   rD          z:SwaggerAutoSchema.add_manual_parameters.<locals>.<genexpr>zDspecify the body parameter as a Schema or Serializer in request_bodyc                 s   rg   r   )r\   r   r^   ri   r%   r%   r&   rD      rk   c                 s   rg   r   rh   ri   r%   r%   r&   rD      rk   c                 s   r@   r   r   rA   r%   r%   r&   rD      rE   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 (rS   z) HTTP methods)	r"   rT   rI   r	   r   r   rU   rV   r   )r   r+   rf   has_body_parameterr%   r%   r&   r3      s   

z'SwaggerAutoSchema.add_manual_parametersc                 C   s   |   }tj| |d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_serializersr%   r%   r&   r;      s   zSwaggerAutoSchema.get_responsesc                 C   s    |   }|r|tur|S |  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``
        )rY   r   rQ   rW   r%   r%   r&   get_default_response_serializer   s   z1SwaggerAutoSchema.get_default_response_serializerc                 C   s   | j  }t|}d}|dv r|  }|pd}|r&t|tjs&| |p%d}|r?|  r4tjtj	|d}| 
 r?| |p>|}tt||iS )zGet the default responses determined for this view from the request serializer and request method.

        :type: dict[str, openapi.Schema]
         )rT   postputpatch)typeitems)r   lowerr   rq   r7   r   rG   r`   has_list_response
TYPE_ARRAYshould_pageget_paginated_responser   str)r   r   default_statusdefault_schemar%   r%   r&   get_default_responses   s   
z'SwaggerAutoSchema.get_default_responsesc                 C   sd   | j ddpi }tdd | D }t }tdd |D s$|  }|dd | 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                 s        | ]\}}t ||fV  qd S r   r}   rB   screspr%   r%   r&   rD          z=SwaggerAutoSchema.get_response_serializers.<locals>.<genexpr>c                 s   s$    | ]}|d krt t|V  qdS )defaultN)r   int)rB   r   r%   r%   r&   rD      s   " c                 s   r   r   r   r   r%   r%   r&   rD      r   )r"   rT   r   rw   rI   r   update)r   manual_responsesr*   r%   r%   r&   rm      s   	z*SwaggerAutoSchema.get_response_serializersc                 C   s   t  }| D ]b\}}t|trtjt|d}nJ|sqt|tjr=|}t|dr<t|jtj	j
s<t|j}| ||_n&t|tj	j
rLtjd|d}nt|tjrU|}nt|}tjd| |d}||t|< q|S )a7  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(   rN   rr   )r(   rN   )r   rw   r7   r}   r   Responser   hasattrrN   rG   rH   r   r`   _Ref)r   rp   r*   r   rM   responser%   r%   r&   ro      s8   

z&SwaggerAutoSchema.get_response_schemasc                 C   s"   | j dd}|durt|}|S )zReturn the query serializer (used for parsing query parameters) for this endpoint.

        :return: the query serializer, or ``None``
        query_serializerN)r"   rT   r   )r   r   r%   r%   r&   get_query_serializer  s   z&SwaggerAutoSchema.get_query_serializerc                 C   sp   |   |   }|  }g }|dur4| j|tjd}ttt|tt|@ dkr4t	d| j
| jf || S )zdReturn the query parameters accepted by this view.

        :rtype: list[openapi.Parameter]
        Nr[   r   zryour query_serializer contains fields that conflict with the filter_backend or paginator_class on the view - %s %s)get_filter_parametersget_pagination_parametersr   r]   r   IN_QUERYlensetr   r	   r   r   )r   natural_parametersr   serializer_parametersr%   r%   r&   r2      s    
z&SwaggerAutoSchema.get_query_parametersc                 C   s*   |p| j }| jdd}|sd|}|S )aq  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'   rr   _)r   r"   rT   rV   )r   r   r'   r%   r%   r&   r4   4  s
   

z"SwaggerAutoSchema.get_operation_idc                 C   sX   d}d}| dd}t|dkr(|d  |d< t|d |k r(|\}}| }||fS )aS  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sectionsr%   r%   r&   split_summary_from_descriptionC  s   	z0SwaggerAutoSchema.split_summary_from_descriptionc                 C   sl   | j dd}| j dd}|du r2| j| j| jpd}| dd}|r2|du r2| |\}}||fS )zReturn an operation summary and description determined from the view's docstring.

        :return: summary and description
        :rtype: (str,str)
        operation_descriptionNoperation_summaryrr   )	r"   rT   r   get_descriptionr   r   r   replacer   )r   r(   r)   r%   r%   r&   r5   W  s   z-SwaggerAutoSchema.get_summary_and_descriptionc                 C      | j ddS )aT  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]]]r/   Nr"   rT   rP   r%   r%   r&   r6   i  s   zSwaggerAutoSchema.get_securityc                 C   r   )zReturn ``True`` if this operation is to be marked as deprecated.

        :return: deprecation status
        :rtype: bool
        r0   Nr   rP   r%   r%   r&   r9   s  s   zSwaggerAutoSchema.is_deprecatedc                 C   s(   |p| 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]
        r.   r   )r   r"   rT   )r   r   r.   r%   r%   r&   r:   {  s
   
	
zSwaggerAutoSchema.get_tagsc                 C      t |  S )zTReturn the MIME types this endpoint can consume.

        :rtype: list[str]
        )r   get_parser_classesrP   r%   r%   r&   r        zSwaggerAutoSchema.get_consumesc                 C   r   )zTReturn the MIME types this endpoint can produce.

        :rtype: list[str]
        )r   get_renderer_classesrP   r%   r%   r&   r     r   zSwaggerAutoSchema.get_producesr   )__name__
__module____qualname__r   r?   r1   rQ   rY   rF   rJ   rK   rL   r3   r;   rq   r   rm   ro   r   r2   r4   r   r5   r6   r9   r:   r   r   __classcell__r%   r%   r#   r&   r      s4    
"&



r   )loggingcollectionsr   rest_framework.requestr   rest_framework.schemasr   rest_framework.statusr   rr   r   errorsr	   utilsr
   r   r   r   r   r   r   r   r   baser   r   	getLoggerr   loggerr   r%   r%   r%   r&   <module>   s    ,
