
    =!gD                         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 j        e          Z G 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                        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 )SwaggerAutoSchemaNc                     t          t          |                               ||||||           t                      | _        || j        _        || _        d S N)superr   __init__r   _schviewoperation_keys)	selfr   pathmethod
componentsrequest	overridesr   	__class__s	           S/var/www/html/nourish/venv/lib/python3.11/site-packages/drf_yasg/inspectors/view.pyr   zSwaggerAutoSchema.__init__   sP    &&//dFJPWYbcccLL		,    c                    |p| j         }|                                 }|                                 }|                     |          }|                                 }||z   }t          |          }|                     |          }|                     |          }|                                 \  }}	| 	                                }
|
t          |
t                    s
J d            |                                 }|                     |          }|                                 }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   sW   '>4+>$$&&$$&&//99))++E\
 ,,
//
;;
,,^<<#??AA$$&&:h#=#=?x=''))
}}^,,&&((	 %&{33"7++!!
 
 
 	
r(   c                 X   |                                  }d}|g S t          |t          j        j                  r|}t          d |D                       r&|t          d          |                     |          S ||                     |          }|| 	                    |          gn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   4   K   | ]}t          |          V  d S r   r   .0encodings     r'   	<genexpr>z@SwaggerAutoSchema.get_request_body_parameters.<locals>.<genexpr>O   s+      EE!(++EEEEEEr(   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_parameters<   s     0022
Ij'."788 	 FEEHEEEEE 	T!,-STTT33J???~55jAA9?9KD,,V4455QS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_serializerX   s      	+;<<<r(   c                 6   | j                             dd          }|{|t          u rt          S | j        | j        vr-t          dd                    | j                  z   dz             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
   joinr:   r	   rJ   rK   r   r    body_overrides     r'   _get_request_body_overridez,SwaggerAutoSchema._get_request_body_override`   s    **>4@@$''{$"333,-TWZW_W_`d`qWrWr-r-l.m n n n-)>?? %$$,];;;r(   c                     |                                  }|"| 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)r^   r"   implicit_body_methodsrU   r   r\   s     r'   rI   z(SwaggerAutoSchema.get_request_serializerp   sR     7799 T[D4N%N%N++---G##4r(   c                 D    |                      |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    rP   s     r'   rM   z-SwaggerAutoSchema.get_request_form_parameters   s     ,,ZW_,MMMr(   c                 ,    |                      |          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_schemarf   s     r'   rN   z)SwaggerAutoSchema.get_request_body_schema   s     ((444r(   c                 F    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)namerc   requiredrQ   )r	   	ParameterIN_BODY)r    rQ   s     r'   rO   z%SwaggerAutoSchema.make_body_parameter   s!      f'/DY_````r(   c                    | j                             dd          pg }t          d |D                       rt          d          t          d |D                       rt          d |D                       }|s+t          d |                                 D                       st          d          | j        | j        vr-t          d	d
                    | j                  z   dz             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              3   @   K   | ]}|j         t          j        k    V  d S r   rc   r	   rn   rF   params     r'   rH   z:SwaggerAutoSchema.add_manual_parameters.<locals>.<genexpr>   +      KKuyGO+KKKKKKr(   zDspecify the body parameter as a Schema or Serializer in request_bodyc              3   @   K   | ]}|j         t          j        k    V  d S r   )rc   r	   re   rs   s     r'   rH   z:SwaggerAutoSchema.add_manual_parameters.<locals>.<genexpr>   ru   r(   c              3   @   K   | ]}|j         t          j        k    V  d S r   rr   rs   s     r'   rH   z:SwaggerAutoSchema.add_manual_parameters.<locals>.<genexpr>   s+      $Z$ZeUY'/%A$Z$Z$Z$Z$Z$Zr(   c              3   4   K   | ]}t          |          V  d S r   r   rE   s     r'   rH   z:SwaggerAutoSchema.add_manual_parameters.<locals>.<genexpr>   s+      ,n,nh-?-I-I,n,n,n,n,n,nr(   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 (rX   z) HTTP methods)	r%   rY   rL   r
   r   r"   rZ   r[   r   )r    r.   rp   has_body_parameters       r'   r6   z'SwaggerAutoSchema.add_manual_parameters   s>    !N../BDIIORKK9JKKKKK 	q()opppKK9JKKKKK 	c!$$Z$Zz$Z$Z$Z!Z!Z! o,n,nZ^ZkZkZmZm,n,n,n)n)n o, .n o o o{$"333, .13688D<M3N3N.OQa.b c c c J(9:::r(   c                 z    |                                  }t          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   sB      $<<>> //0DEE
 
 
 	
r(   c                 l    |                                  }|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``
        )r^   r   rU   r\   s     r'   get_default_response_serializerz1SwaggerAutoSchema.get_default_response_serializer   s?     7799 	!]'99  '')))r(   c                    | j                                         }t          |          }d}|dv r|                                 }|pd}|r1t	          |t
          j                  s|                     |          pd}|r_|                                 r t          j        t
          j	        |          }| 
                                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]
         )rY   postputpatch)typeitems)r"   lowerr   r   r:   r	   rJ   rh   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6444!AACCN'-2 	M*^W^"L"L 	M!66~FFL"N 	_%%'' _!(W5G~!^!^!^!! _!%!<!<^!L!L!^P^C//@AAAr(   c                 n   | j                             dd          pi }t          d |                                D                       }t                      }t	          d |D                       s|                                 }|                    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              3   >   K   | ]\  }}t          |          |fV  d S r   r   rF   scresps      r'   rH   z=SwaggerAutoSchema.get_response_serializers.<locals>.<genexpr>   s0      &`&`82tB&`&`&`&`&`&`r(   c              3   Z   K   | ]&}|d k    t          t          |                    V  'dS )defaultN)r   int)rF   r   s     r'   rH   z=SwaggerAutoSchema.get_response_serializers.<locals>.<genexpr>   s4      UU2R9__:c"gg&&____UUr(   c              3   >   K   | ]\  }}t          |          |fV  d S r   r   r   s      r'   rH   z=SwaggerAutoSchema.get_response_serializers.<locals>.<genexpr>   s0      RRXR#b''4RRRRRRr(   )r%   rY   r   r   rL   r   update)r    manual_responsesr-   s      r'   r{   z*SwaggerAutoSchema.get_response_serializers   s      >--k4@@FB&&`&`GWG]G]G_G_&`&`&```MM	UU1AUUUUU 	52244IRR9I9O9O9Q9QRRRRRRr(   c                 
   t                      }|                                D ]^\  }}t          |t                    r$t	          j        t          |                    }n|sBt          |t          j                  re|}t          |d          rRt          |j        t          j	        j
                  s.t          |j                  }|                     |          |_        nt          |t          j	        j
                  rt	          j        d|          }nUt          |t          j                  r|}n8t          |          }t	          j        d|                     |                    }||t          |          <   `|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+   rQ   r   )r+   rQ   )r   r   r:   r   r	   Responser   hasattrrQ   rJ   rK   r   rh   _Ref)r    r~   r-   r   rP   responses         r'   r}   z&SwaggerAutoSchema.get_response_schemas   s     MM	288:: 	* 	*NB
*c** "+ .z : :     J(899 %8X.. Lz(/SZSaSh7i7i L!:8?!K!KJ&*&?&?
&K&KHOJ(=>> "+ "%   J55 %6zBB
"+ "44Z@@  
 "*Ic"ggr(   c                 ^    | 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%   rY   r   )r    r   s     r'   get_query_serializerz&SwaggerAutoSchema.get_query_serializer  s8    
  >--.@$GG'89IJJr(   c                    |                                  |                                 z   }|                                 }g }||                     |t          j                  }t          t          t          |                    t          t          |                    z            dk    rt          d| j
        d| j                  ||z   S )zdReturn the query parameters accepted by this view.

        :rtype: list[openapi.Parameter]
        Nrb   r   zmyour query_serializer contains fields that conflict with the filter_backend or paginator_class on the view -  )get_filter_parametersget_pagination_parametersr   rd   r	   IN_QUERYlensetr   r
   r"   r!   )r    natural_parametersr   serializer_parameterss       r'   r5   z&SwaggerAutoSchema.get_query_parameters   s    
 "7799D<Z<Z<\<\\4466 "'$($A$ABRX_Xh$A$i$i!3*+=>>??#FYZoFpFpBqBqqrrvwww,,OS{{{\`\e\eg  
 "$999r(   c                 |    |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*   r   _)r   r%   rY   r[   )r    r   r*   s      r'   r7   z"SwaggerAutoSchema.get_operation_id4  sG     (>4+>~))."== 	488N33Lr(   c                    d}d}|                     dd          }t          |          dk    rO|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sectionss        r'   split_summary_from_descriptionz0SwaggerAutoSchema.split_summary_from_descriptionC  s     $$VQ//x==A"1+++--HQK8A;/11'/$)//11##r(   c                 P   | j                             dd          }| j                             dd          }|k| j                            | j        | j                  pd}|                                                    dd          }|r||                     |          \  }}||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%   rY   r   get_descriptionr!   r"   r   replacer   )r    r+   r,   s      r'   r8   z-SwaggerAutoSchema.get_summary_and_descriptionW  s     n(()@$GG.$$%8$??)33DIt{KKQrK%++--55dB??K X'+'J'J;'W'W$##r(   c                 8    | 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]]]r2   Nr%   rY   rT   s    r'   r9   zSwaggerAutoSchema.get_securityi  s     ~!!*d333r(   c                 8    | j                             dd          S )zReturn ``True`` if this operation is to be marked as deprecated.

        :return: deprecation status
        :rtype: bool
        r3   Nr   rT   s    r'   r<   zSwaggerAutoSchema.is_deprecateds  s     ~!!,555r(   c                 b    |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]
        r1   r   )r   r%   rY   )r    r   r1   s      r'   r=   zSwaggerAutoSchema.get_tags{  s@     (>4+>~!!&)) 	'"1%&Dr(   c                 D    t          |                                           S )zTReturn the MIME types this endpoint can consume.

        :rtype: list[str]
        )r   get_parser_classesrT   s    r'   r   zSwaggerAutoSchema.get_consumes  s    
 D3355666r(   c                 D    t          |                                           S )zTReturn the MIME types this endpoint can produce.

        :rtype: list[str]
        )r   get_renderer_classesrT   s    r'   r   zSwaggerAutoSchema.get_produces  s    
 D5577888r(   r   )__name__
__module____qualname__r   rB   r4   rU   r^   rI   rM   rN   rO   r6   r>   r   r   r{   r}   r   r5   r7   r   r8   r9   r<   r=   r   r   __classcell__)r&   s   @r'   r   r      s       - - - - - - 
  
  
  
DT T T8= = =      N N N5 5 5a a a; ; ;,	
 	
 	

* 
* 
*B B B0  &$ $ $L     : : :(   $ $ $($ $ $$4 4 46 6 6   "7 7 79 9 9 9 9 9 9r(   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      sR    # # # # # # 5 5 5 5 5 5 - - - - - - , , , , , ,       + + + + + +                      2 1 1 1 1 1 1 1		8	$	$E9 E9 E9 E9 E9 E9 E9 E9 E9 E9r(   