
    =!gR                     .   d dl Z d dlZd dlmZ ddlmZ ddlmZmZm	Z	m
Z
  e            Z ej        e          Zd ZddZ G d d	          Z G d
 de          Z G d de          Z G d de          Z G d de          Z G d de          ZdS )    N)serializers   )openapi)force_real_strget_field_defaultget_object_classesis_list_viewc                     t          | |          }t          j        |          rt          |dd           r|dfS ddlm} |t	           || |d           t
                    fS )N__self__Tr   )getattr_static)getattrinspectismethodr   
isinstancestaticmethod)cls_or_instancemethod_namemethodr   s       S/var/www/html/nourish/venv/lib/python3.11/site-packages/drf_yasg/inspectors/base.pyis_callable_methodr      sw    _k22F GFJ$E$E t|&&&&&&:nn_k4PPR^____    c                 *   t          | |          r`	 t          | |          \  }}|r
 |            S n?# t          $ r2 t                              dt          |           j        d           Y nw xY w|r t          | |          rt          | |          S |S )a  Call a view method which might throw an exception. If an exception is thrown, log an informative error message
    and return the value of fallback_attr, or default if not present. The method must be callable without any arguments
    except cls or self.

    :param view: view class or instance; if a class is passed, instance methods won't be called
    :type view: rest_framework.views.APIView or type[rest_framework.views.APIView]
    :param str method_name: name of a method on the view
    :param str fallback_attr: name of an attribute on the view to fall back on, if calling the method fails
    :param default: default value if all else fails
    :return: view method's return value, or value of view's fallback_attr, or default
    :rtype: any or None
    zview's %s raised exception during schema generation; use `getattr(self, 'swagger_fake_view', False)` to detect and short-circuit thisT)exc_info)hasattrr   	Exceptionloggerwarningtype__name__r   )viewr   fallback_attrdefaultview_methodis_callabales         r   call_view_methodr%      s     t[!! ?	?(:4(M(M%K %"{}}$% 	? 	? 	?NN j::.  ? ? ? ? ?	?
  ,}55 ,t]+++Ns   2 9A.-A.c                   .    e Zd Zd Zd ZddZd Zd ZdS )BaseInspectorc                 L    || _         || _        || _        || _        || _        dS )a  
        :param rest_framework.views.APIView view: the view associated with this endpoint
        :param str path: the path component of the operation URL
        :param str method: the http method of the operation
        :param openapi.ReferenceResolver components: referenceable components
        :param rest_framework.request.Request request: the request made against the schema view; can be None
        N)r    pathr   
componentsrequest)selfr    r)   r   r*   r+   s         r   __init__zBaseInspector.__init__7   s+     		$r   c                     |S )ap  After an inspector handles an object (i.e. returns a value other than :data:`.NotHandled`), all inspectors
        that were probed get the chance to alter the result, in reverse order. The inspector that handled the object
        is the first to receive a ``process_result`` call with the object it just returned.

        This behavior is similar to the Django request/response middleware processing.

        If this inspector has no post-processing to do, it should just ``return result`` (the default implementation).

        :param result: the return value of the winning inspector, or ``None`` if no inspector handled the object
        :param str method_name: name of the method that was called on the inspector
        :param obj: first argument passed to inspector method
        :param kwargs: additional arguments passed to inspector method
        :return:
         )r,   resultr   objkwargss        r   process_resultzBaseInspector.process_resultE   s	     r   Nc                    |pi }g }|D ]}t          j        |          s
J d            t          |t                    s
J d             || j        | j        | j        | j        | j        fi |}|	                    |           t          ||d          }| ||fi |}	|	t          ur n t                              d|||           d}	t          |          D ]} |j        |	||fi |}	|	S )af  Probe a list of inspectors with a given object. The first inspector in the list to return a value that
        is not :data:`.NotHandled` wins.

        :param list[type[BaseInspector]] inspectors: list of inspectors to probe
        :param str method_name: name of the target method on the inspector
        :param obj: first argument to inspector method
        :param dict initkwargs: extra kwargs for instantiating inspector class
        :param kwargs: additional arguments to inspector method
        :return: the return value of the winning inspector, or ``None`` if no inspector handled the object
        z(inspector must be a class, not an objectz&inspectors must subclass BaseInspectorNz@%s ignored because no inspector in %s handled it (operation: %s))r   isclass
issubclassr'   r    r)   r   r*   r+   appendr   
NotHandledr   r   reversedr3   )
r,   
inspectorsr   r1   
initkwargsr2   tried_inspectors	inspectorr   r0   s
             r   probe_inspectorszBaseInspector.probe_inspectorsV   sL     %2
# 	 	I?9--YY/YYY-i77aa9aaa7!	$)TYT_VZVbqqfpqqI##I...YT::F~VC**6**FZ'' ( NN]
K9 9 9F!"233 	R 	RI-Y-fk3QQ&QQFFr   c                 J    t          t          | j        ddg                     S )zGet the renderer classes of this view by calling `get_renderers`.

        :return: renderer classes
        :rtype: list[type[rest_framework.renderers.BaseRenderer]]
        get_renderersrenderer_classesr   r%   r    r,   s    r   get_renderer_classesz"BaseInspector.get_renderer_classes{   s%     ""249oOace"f"fgggr   c                 J    t          t          | j        ddg                     S )zGet the parser classes of this view by calling `get_parsers`.

        :return: parser classes
        :rtype: list[type[rest_framework.parsers.BaseParser]]
        get_parsersparser_classesrB   rC   s    r   get_parser_classesz BaseInspector.get_parser_classes   s%     ""249mM]_a"b"bcccr   N)r   
__module____qualname__r-   r3   r>   rD   rH   r/   r   r   r'   r'   6   sm            "# # # #Jh h hd d d d dr   r'   c                       e Zd ZdZd Zd ZdS )PaginatorInspectorzBase inspector for paginators.

    Responsible for determining extra query parameters and response structure added by given paginators.
    c                     t           S )a  Get the pagination parameters for a single paginator **instance**.

        Should return :data:`.NotHandled` if this inspector does not know how to handle the given `paginator`.

        :param BasePagination paginator: the paginator
        :rtype: list[openapi.Parameter]
        r8   )r,   	paginators     r   get_paginator_parametersz+PaginatorInspector.get_paginator_parameters   
     r   c                     t           S )ad  Add appropriate paging fields to a response :class:`.Schema`.

        Should return :data:`.NotHandled` if this inspector does not know how to handle the given `paginator`.

        :param BasePagination paginator: the paginator
        :param openapi.Schema response_schema: the response schema that must be paged.
        :rtype: openapi.Schema
        rO   )r,   rP   response_schemas      r   get_paginated_responsez)PaginatorInspector.get_paginated_response   
     r   N)r   rJ   rK   __doc__rQ   rU   r/   r   r   rM   rM      s<         
  	 	 	 	 	r   rM   c                       e Zd ZdZd ZdS )FilterInspectorzBase inspector for filter backends.

    Responsible for determining extra query parameters added by given filter backends.
    c                     t           S )a.  Get the filter parameters for a single filter backend **instance**.

        Should return :data:`.NotHandled` if this inspector does not know how to handle the given `filter_backend`.

        :param BaseFilterBackend filter_backend: the filter backend
        :rtype: list[openapi.Parameter]
        rO   )r,   filter_backends     r   get_filter_parametersz%FilterInspector.get_filter_parameters   rR   r   N)r   rJ   rK   rW   r\   r/   r   r   rY   rY      s-         
    r   rY   c                   :     e Zd ZdZ fdZd Zd Zd Zd Z xZ	S )FieldInspectorz6Base inspector for serializers and serializer fields. c                 n    t          t          |                               |||||           || _        d S rI   )superr^   r-   field_inspectors)r,   r    r)   r   r*   r+   ra   	__class__s          r   r-   zFieldInspector.__init__   s7    nd##,,T4WUUU 0r   c                     t          |dd          }t          |di           }|r+|                                D ]\  }}t          |||           dS dS )aV  Set fields from the ``swagger_schema_fields`` attribute on the Meta class. This method is called
        only for serializers or fields that are converted into ``openapi.Schema`` objects.

        :param serializer_or_field: serializer or field instance
        :param openapi.Schema schema: the schema object to be modified in-place
        MetaNswagger_schema_fields)r   itemssetattr)r,   serializer_or_fieldschemametare   attrvals          r   add_manual_fieldsz FieldInspector.add_manual_fields   sy     *FD99 '.Er J J  	+288:: + +	cc****	+ 	++ +r   c                     t           S )a  Convert a drf Serializer or Field instance into a Swagger object.

        Should return :data:`.NotHandled` if this inspector does not know how to handle the given `field`.

        :param rest_framework.serializers.Field field: the source field
        :param type[openapi.SwaggerDict] swagger_object_type: should be one of Schema, Parameter, Items
        :param bool use_references: if False, forces all objects to be declared inline
           instead of by referencing other components
        :param kwargs: extra attributes for constructing the object;
           if swagger_object_type is Parameter, ``name`` and ``in_`` should be provided
        :return: the swagger object
        :rtype: openapi.Parameter or openapi.Items or openapi.Schema or openapi.SchemaRef
        rO   r,   fieldswagger_object_typeuse_referencesr2   s        r   field_to_swagger_objectz&FieldInspector.field_to_swagger_object   s
     r   c                 B     | j         | j        d|d| j        if||d|S )zHelper method for recursively probing `field_inspectors` to handle a given field.

        All arguments are the same as :meth:`.field_to_swagger_object`.

        :rtype: openapi.Parameter or openapi.Items or openapi.Schema or openapi.SchemaRef
        rs   ra   )rq   rr   r>   ra   ro   s        r   probe_field_inspectorsz%FieldInspector.probe_field_inspectors   sI     %t$!#<eFXZ^ZoEp
 3N
 
V\
 
 	
r   c                    	 t           j        t           j        t           j        fv sJ t	          t           j                  r
J d            j        rt          j                  nd	t           j        k    r	nd	t          dd          }|rt          |          ndt           j        k    rndd 	fd	}t           j        k    rt           j        nt           j        }||fS )a[  Helper method to extract generic information from a field and return a partial constructor for the
        appropriate openapi object.

        All arguments are the same as :meth:`.field_to_swagger_object`.

        The return value is a tuple consisting of:

        * a function for constructing objects of `swagger_object_type`; its prototype is: ::

            def SwaggerType(existing_object=None, **instance_kwargs):

          This function creates an instance of `swagger_object_type`, passing the following attributes to its init,
          in order of precedence:

            - arguments specified by the ``kwargs`` parameter of :meth:`._get_partial_types`
            - ``instance_kwargs`` passed to the constructor function
            - ``title``, ``description``, ``required``, ``x-nullable`` and ``default`` inferred from the field,
              where appropriate

          If ``existing_object`` is not ``None``, it is updated instead of creating a new object.

        * a type that should be used for child objects if `field` is of an array type. This can currently have two
          values:

            - :class:`.Schema` if `swagger_object_type` is :class:`.Schema`
            - :class:`.Items` if `swagger_object_type` is  :class:`.Parameter` or :class:`.Items`

        :rtype: (function,type[openapi.Schema] or type[openapi.Items])
        z,passed field is already a SwaggerDict objectN	help_textTc                    d|vrt           j        k    r
j        |d<   d|vr4t           j        k    r$t	                    }|d t
          j        fvr||d<   |r:|                    dd           t           j        k    r|	                    d           |	                    d           t          dd           rd|d<   |                    	           | Mt          |           sJ t          |                                          D ]\  }}t          | ||           | }n d	i |}t           j        k    r
                    |           |S )
Nrequiredr"   r   titledescription
allow_nullT
x_nullabler/   )r   	Parameterrz   Itemsr   r   emptyget
TYPE_ARRAY
setdefaultr   updater   sortedrf   rg   Schemarm   )existing_objectuse_field_titleinstance_kwargsr"   keyrl   r0   r|   rp   r2   r,   rq   r{   s          r   SwaggerTypez6FieldInspector._get_partial_types.<locals>.SwaggerType  s   005HGL]5]5].3n
+//4G7=4X4X+E224):";;;18OI. ;?#6#6vt#D#DHZ#Z#Z**7E:::&**=+FFFulD11 504-""6****!/3FGGGGG &'<'<'>'> ? ? 7 7HCOS#6666(,,???? #gn44&&uf555Mr   )NT)	r   r   r   r   r   SwaggerDictlabelr   r   )
r,   rp   rq   rr   r2   rx   r   child_swagger_typer|   r{   s
   ``` `   @@r   _get_partial_typesz!FieldInspector._get_partial_types   s   < #w~w7H'-&XXXXXeW%899ii;iii9/4{Du{+++,>>DE;55	3<FnY///$%8GM%I%Ikkt	 	 	 	 	 	 	 	 	 	 	D 0Cgn/T/TW^^ZaZg...r   )
r   rJ   rK   rW   r-   rm   rs   rv   r   __classcell__rb   s   @r   r^   r^      s        @@1 1 1 1 1+ + +   

 

 

I/ I/ I/ I/ I/ I/ I/r   r^   c                       e Zd Zd Zd ZdS )SerializerInspectorc                     t           S )a-  Convert a DRF Serializer instance to an :class:`.openapi.Schema`.

        Should return :data:`.NotHandled` if this inspector does not know how to handle the given `serializer`.

        :param serializers.BaseSerializer serializer: the ``Serializer`` instance
        :rtype: openapi.Schema
        rO   r,   
serializers     r   
get_schemazSerializerInspector.get_schema6  rR   r   c                     t           S )a  Convert a DRF serializer into a list of :class:`.Parameter`\ s.

        Should return :data:`.NotHandled` if this inspector does not know how to handle the given `serializer`.

        :param serializers.BaseSerializer serializer: the ``Serializer`` instance
        :param str in_: the location of the parameters, one of the `openapi.IN_*` constants
        :rtype: list[openapi.Parameter]
        rO   r,   r   in_s      r   get_request_parametersz*SerializerInspector.get_request_parameters@  rV   r   N)r   rJ   rK   r   r   r/   r   r   r   r   5  s2          	 	 	 	 	r   r   c                   x     e Zd ZdZdZdZg Zg Zg Z fdZ	d Z
d Zd Zd Zd	 Zd
 Zd Zd Zd Zd Zd Z xZS )ViewInspector)PUTPATCHPOSTDELETE)r   r   r   )GETc                     t          t          |                               |||||           || _        |                     d           |                     d           |                     d           dS )z
        Inspector class responsible for providing :class:`.Operation` definitions given a view, path and method.

        :param dict overrides: manual overrides as passed to :func:`@swagger_auto_schema <.swagger_auto_schema>`
        ra   filter_inspectorspaginator_inspectorsN)r`   r   r-   	overrides_prepend_inspector_overrides)r,   r    r)   r   r*   r+   r   rb   s          r   r-   zViewInspector.__init__Z  sw     	mT""++D$
GTTT"))*<===))*=>>>))*@AAAAAr   c                     | j                             |d           r2fdt          | |          D             }t          | ||z              d S d S )Nc                     g | ]}|v|	S r/   r/   ).0inspextra_inspectorss     r   
<listcomp>z>ViewInspector._prepend_inspector_overrides.<locals>.<listcomp>i  s%    !m!m!m4PT\lPlPl$PlPlPlr   )r   r   r   rg   )r,   r:   default_inspectorsr   s      @r   r   z*ViewInspector._prepend_inspector_overridesf  ss    >--j$?? 	M!m!m!m!m743L3L!m!m!mD*&69K&KLLLLL	M 	Mr   c                      t          d          )aw  Get an :class:`.Operation` for the given API endpoint (path, method).
        This includes query, body parameters and response schemas.

        :param tuple[str] operation_keys: an array of keys describing the hierarchical layout of this view in the API;
          e.g. ``('snippets', 'list')``, ``('snippets', 'retrieve')``, etc.
        :rtype: openapi.Operation
        z-ViewInspector must implement get_operation()!)NotImplementedError)r,   operation_keyss     r   get_operationzViewInspector.get_operationl  s     ""QRRRr   c                 B    t          | j        | j        | j                  S )a  Determine whether this view is a list or a detail view. The difference between the two is that
        detail views depend on a pk/id path parameter. Note that a non-detail view does not necessarily imply a list
        response (:meth:`.has_list_response`), nor are list responses limited to non-detail views.

        For example, one might have a `/topic/<pk>/posts` endpoint which is a detail view that has a list response.

        :rtype: bool)r	   r)   r   r    rC   s    r   r	   zViewInspector.is_list_viewv  s     DIt{DI>>>r   c                 j    |                                  o| j                                        | j        v S )zDetermine whether this view returns multiple objects. By default this is any non-detail view
        (see :meth:`.is_list_view`) whose request method is one of :attr:`.implicit_list_response_methods`.

        :rtype: bool
        )r	   r   upperimplicit_list_response_methodsrC   s    r   has_list_responsezViewInspector.has_list_response  s1       ""c(9(9(;(;t?b(bcr   c                 V    t          | j        dd          o|                                 S )zoDetermine whether filter backend parameters should be included for this request.

        :rtype: bool
        filter_backendsNr   r    r   rC   s    r   should_filterzViewInspector.should_filter  s*    
 ty"3T::Wt?U?U?W?WWr   c                     |                                  sg S g }t          | j        d          D ]+}||                     | j        d |                      pg z  },|S )zqReturn the parameters added to the view by its filter backends.

        :rtype: list[openapi.Parameter]
        r   r\   )r   r   r    r>   r   )r,   fieldsr[   s      r   r\   z#ViewInspector.get_filter_parameters  sw    
 !!## 	I%di1BCC 	u 	uNd++D,BD[]k]k]m]mnntrttFFr   c                 V    t          | j        dd          o|                                 S )zDetermine whether paging parameters and structure should be added to this operation's request and response.

        :rtype: bool
        rP   Nr   rC   s    r   should_pagezViewInspector.should_page  s)    
 ty+t44Q9O9O9Q9QQr   c                     |                                  sg S |                     | j        dt          | j        d                    pg S )zkReturn the parameters added to the view by its paginator.

        :rtype: list[openapi.Parameter]
        rQ   rP   )r   r>   r   r   r    rC   s    r   get_pagination_parametersz'ViewInspector.get_pagination_parameters  sX    
 !! 	I$$T%>@Z%,TY%D%DF F LIK	Lr   c                 J    |                      | j        d|d| j        i          S )a#  Convert a serializer to an OpenAPI :class:`.Schema`.

        :param serializers.BaseSerializer serializer: the ``Serializer`` instance
        :returns: the converted :class:`.Schema`, or ``None`` in case of an unknown serializer
        :rtype: openapi.Schema or openapi.SchemaRef
        r   ra   ru   r   s     r   serializer_to_schemaz"ViewInspector.serializer_to_schema  s2     $$!<>PRVRg=h
 
 	
r   c                 R    |                      | j        d|d| j        i|          pg S )a(  Convert a serializer to a possibly empty list of :class:`.Parameter`\ s.

        :param serializers.BaseSerializer serializer: the ``Serializer`` instance
        :param str in_: the location of the parameters, one of the `openapi.IN_*` constants
        :rtype: list[openapi.Parameter]
        r   ra   )r   ru   r   s      r   serializer_to_parametersz&ViewInspector.serializer_to_parameters  sE     $$!#;ZJ\^b^sIt % 
 
  	r   c                 d    |                      | j        dt          | j        d          |          S )a+  Add appropriate paging fields to a response :class:`.Schema`.

        :param openapi.Schema response_schema: the response schema that must be paged.
        :returns: the paginated response class:`.Schema`, or ``None`` in case of an unknown pagination scheme
        :rtype: openapi.Schema
        rU   rP   )rT   )r>   r   r   r    )r,   rT   s     r   rU   z$ViewInspector.get_paginated_response  s>     $$T%>@X%,TY%D%DVe % g g 	gr   )r   rJ   rK   body_methodsimplicit_body_methodsr   ra   r   r   r-   r   r   r	   r   r   r\   r   r   r   r   rU   r   r   s   @r   r   r   L  s$       5L 5 &." 
B 
B 
B 
B 
BM M MS S S? ? ?d d dX X X  R R R	L 	L 	L	
 	
 	

 
 
g g g g g g gr   r   )NN)r   loggingrest_frameworkr    r   utilsr   r   r   r	   objectr8   	getLoggerr   r   r   r%   r'   rM   rY   r^   r   r   r/   r   r   <module>r      s     & & & & & &       W W W W W W W W W W W W VXX
		8	$	$` ` `   :Sd Sd Sd Sd Sd Sd Sd Sdl       8    m   "y/ y/ y/ y/ y/] y/ y/ y/x    .   .Bg Bg Bg Bg BgM Bg Bg Bg Bg Bgr   