
    NwgoI                         d Z ddlmZ ddlmZ ddlmZmZ ddlm	Z	 ddl
mZ ddlmZ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 ddlmZ ddlmZ ddlmZ ddl m!Z! d Z"ddZ#d Z$d Z% G d de      Z&y)zL
Provides an APIView class that is the base of all views in REST framework.
    )settings)PermissionDenied)connectionsmodels)Http404)HttpResponseBase)cc_delim_repatch_vary_headers)	smart_str)csrf_exempt)View)
exceptionsstatus)Request)Response)DefaultSchema)api_settings)
formattingc                    t        | dd      }||S | j                  j                  }t        j                  |d      }t        j                  |d      }t        j
                  |      }t        | dd      }|r|d|z   z  }|S )z
    Given a view instance, return a textual name to represent the view.
    This name is used in the browsable API, and in OPTIONS responses.

    This function is the default for the `VIEW_NAME_FUNCTION` setting.
    nameNr   ViewSetsuffix )getattr	__class____name__r   remove_trailing_stringcamelcase_to_spaces)viewr   r   s      K/var/www/horilla/myenv/lib/python3.12/site-packages/rest_framework/views.pyget_view_namer!      s     4&D>>""D,,T6:D,,T9=D))$/D T8T*FfK    c                     t        | dd      }|| j                  j                  xs d}t        j                  t        |            }|rt        j                  |      S |S )z
    Given a view instance, return a textual description to represent the view.
    This name is used in the browsable API, and in OPTIONS responses.

    This function is the default for the `VIEW_DESCRIPTION_FUNCTION` setting.
    descriptionN )r   r   __doc__r   dedentr   markup_description)r   htmlr$   s      r    get_view_descriptionr*   /   s[     $t4Knn,,2##Ik$:;K,,[99r"   c                      t        j                         D ]0  } | j                  d   s| j                  s | j	                  d       2 y )NATOMIC_REQUESTST)r   allsettings_dictin_atomic_blockset_rollback)dbs    r    r0   r0   A   s;    oo "-.23E3EOOD!"r"   c                    t        | t              rt        j                  | j                   } n,t        | t
              rt        j
                  | j                   } t        | t        j                        ri }t        | dd      r| j                  |d<   t        | dd      rd| j                  z  |d<   t        | j                  t        t        f      r| j                  }nd| j                  i}t                t        || j                  |      S y)	a4  
    Returns the response that should be used for any given exception.

    By default we handle the REST framework `APIException`, and also
    Django's built-in `Http404` and `PermissionDenied` exceptions.

    Any unhandled exceptions may return `None`, which will cause a 500 error
    to be raised.
    auth_headerNzWWW-Authenticatewaitz%dzRetry-Afterdetail)r   headers)
isinstancer   r   NotFoundargsr   APIExceptionr   r3   r4   r5   listdictr0   r   status_code)exccontextr6   datas       r    exception_handlerrA   G   s     #w!!CHH.	C)	*))CHH6#z../3t,*-//G&'3%%)CHH_GM"cjj4,/::Dcjj)DS__gFFr"   c                       e Zd Zej                  Zej                  Zej                  Z	ej                  Zej                  Zej                  Zej                   Zej$                  ZeZ e       Ze fd       Zed        Zed        Zd Zd"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 Z)d Z*d Z+d Z,d#dZ-d Z.d Z/d Z0d Z1d Z2d Z3d Z4d Z5d Z6d Z7d  Z8d! Z9 xZ:S )$APIViewc                     t        t        | dd      t        j                  j                        rd }|| j
                  _        t        |    di |}| |_	        ||_
        t        |      S )z
        Store the original class on the view function.

        This allows us to discover information about the view when we do URL
        reverse lookups.  Used for breadcrumb generation.
        querysetNc                      t        d      )NzDo not evaluate the `.queryset` attribute directly, as the result will be cached and reused between requests. Use `.all()` or call `.get_queryset()` instead.)RuntimeError r"   r    force_evaluationz)APIView.as_view.<locals>.force_evaluation   s    "F r"   rH   )r7   r   r   queryQuerySetrE   
_fetch_allsuperas_viewcls
initkwargsr   )rO   rP   rI   r   r   s       r    rN   zAPIView.as_viewy   sa     gc:t4fll6K6KL '7CLL#w,,$ 4  r"   c                 "    | j                         S )zZ
        Wrap Django's private `_allowed_methods` interface in a public property.
        )_allowed_methodsselfs    r    allowed_methodszAPIView.allowed_methods   s    
 $$&&r"   c                 z    ddj                  | j                        i}t        | j                        dkD  rd|d<   |S )NAllowz,    AcceptVary)joinrU   lenrenderer_classes)rT   r6   s     r    default_response_headersz APIView.default_response_headers   sB     TYYt334
 t$$%)&GFOr"   c                 @    t        j                  |j                        )z
        If `request.method` does not correspond to a handler method,
        determine what kind of exception to raise.
        )r   MethodNotAllowedmethod)rT   requestr9   kwargss       r    http_method_not_allowedzAPIView.http_method_not_allowed   s    
 ))'..99r"   c                     |j                   r |j                  st        j                         t        j                  ||      )zY
        If request is not permitted, determine what kind of exception to raise.
        )r5   code)authenticatorssuccessful_authenticatorr   NotAuthenticatedr   )rT   rb   messagerf   s       r    permission_deniedzAPIView.permission_denied   s7     !!'*J*J--//))tDDr"   c                 ,    t        j                  |      )zU
        If request is throttled, determine what kind of exception to raise.
        )r   	Throttled)rT   rb   r4   s      r    	throttledzAPIView.throttled   s     ""4((r"   c                 P    | j                         }|r|d   j                  |      S y)z
        If a request is unauthenticated, determine the WWW-Authenticate
        header to use for 401 responses, if any.
        r   N)get_authenticatorsauthenticate_header)rT   rb   rg   s      r    get_authenticate_headerzAPIView.get_authenticate_header   s0    
 002!!$88AA r"   c                 :    | t        | dd      t        | di       dS )z|
        Returns a dict that is passed through to Parser.parse(),
        as the `parser_context` keyword argument.
        r9   rH   rc   )r   r9   rc   r   )rT   http_requests     r    get_parser_contextzAPIView.get_parser_context   s)     D&"-dHb1
 	
r"   c                 R    | t        | dd      t        | di       t        | dd      dS )z
        Returns a dict that is passed through to Renderer.render(),
        as the `renderer_context` keyword argument.
        r9   rH   rc   rb   Nr   r9   rc   rb   rt   rS   s    r    get_renderer_contextzAPIView.get_renderer_context   s5     D&"-dHb1tY5	
 	
r"   c                 R    | t        | dd      t        | di       t        | dd      dS )zp
        Returns a dict that is passed through to EXCEPTION_HANDLER,
        as the `context` argument.
        r9   rH   rc   rb   Nrx   rt   rS   s    r    get_exception_handler_contextz%APIView.get_exception_handler_context   s5     D&"-dHb1tY5	
 	
r"   c                 >    | j                   j                  } ||       S )zf
        Return the view name, as used in OPTIONS responses and in the
        browsable API.
        )r   VIEW_NAME_FUNCTION)rT   funcs     r    r!   zAPIView.get_view_name   s    
 }}//Dzr"   c                 @    | j                   j                  } || |      S )z{
        Return some descriptive text for the view, as used in OPTIONS responses
        and in the browsable API.
        )r   VIEW_DESCRIPTION_FUNCTION)rT   r)   r~   s      r    r*   zAPIView.get_view_description   s     
 }}66D$r"   c                 z    | j                   j                  r%|j                  | j                   j                        S y)zQ
        Determine if the request includes a '.json' style format suffix
        N)r   FORMAT_SUFFIX_KWARGget)rT   rc   s     r    get_format_suffixzAPIView.get_format_suffix   s/     ==,,::dmm??@@ -r"   c                 J    | j                   D cg c]	  } |        c}S c c}w )zX
        Instantiates and returns the list of renderers that this view can use.
        )r]   )rT   renderers     r    get_rendererszAPIView.get_renderers         ,0+@+@Ax
AAA    c                 J    | j                   D cg c]	  } |        c}S c c}w )zV
        Instantiates and returns the list of parsers that this view can use.
        )parser_classes)rT   parsers     r    get_parserszAPIView.get_parsers  s      (,':':;V;;;r   c                 J    | j                   D cg c]	  } |        c}S c c}w )z]
        Instantiates and returns the list of authenticators that this view can use.
        )authentication_classes)rT   auths     r    rp   zAPIView.get_authenticators  s      $(#>#>?4???r   c                 J    | j                   D cg c]	  } |        c}S c c}w )z[
        Instantiates and returns the list of permissions that this view requires.
        )permission_classes)rT   
permissions     r    get_permissionszAPIView.get_permissions  s      04/F/FG
GGGr   c                 J    | j                   D cg c]	  } |        c}S c c}w )zU
        Instantiates and returns the list of throttles that this view uses.
        )throttle_classes)rT   throttles     r    get_throttleszAPIView.get_throttles  r   r   c                 ^    t        | dd      s| j                         | _        | j                  S )zN
        Instantiate and return the content negotiation class to use.
        _negotiatorN)r   content_negotiation_classr   rS   s    r    get_content_negotiatorzAPIView.get_content_negotiator  s-     t]D1#==?Dr"   c                 .    | j                   j                  S )zD
        Returns the exception handler that this view uses.
        )r   EXCEPTION_HANDLERrS   s    r    get_exception_handlerzAPIView.get_exception_handler&  s     }}...r"   c                     | j                         }| j                         }	 |j                  ||| j                        S # t        $ r |r|d   |d   j
                  fcY S  w xY w)zU
        Determine which renderer and media type to use render the response.
        r   )r   r   select_rendererformat_kwarg	Exception
media_type)rT   rb   force	renderersconnegs        r    perform_content_negotiationz#APIView.perform_content_negotiation.  sn     &&(	,,.	))'9d>O>OPP 	!!il&=&=>>	s   ? A" A"c                     |j                    y)a  
        Perform authentication on the incoming request.

        Note that if you override this and simply 'pass', then authentication
        will instead be performed lazily, the first time either
        `request.user` or `request.auth` is accessed.
        N)user)rT   rb   s     r    perform_authenticationzAPIView.perform_authentication<  s     	r"   c           
          | j                         D ]?  }|j                  ||       r| j                  |t        |dd      t        |dd             A y)z
        Check if the request should be permitted.
        Raises an appropriate exception if the request is not permitted.
        rj   Nrf   rj   rf   )r   has_permissionrk   r   )rT   rb   r   s      r    check_permissionszAPIView.check_permissionsF  sZ    
 ..0 	J,,Wd;&&#J	4@ VT: ' 	r"   c           
          | j                         D ]@  }|j                  || |      r| j                  |t        |dd      t        |dd             B y)z
        Check if the request should be permitted for a given object.
        Raises an appropriate exception if the request is not permitted.
        rj   Nrf   r   )r   has_object_permissionrk   r   )rT   rb   objr   s       r    check_object_permissionsz APIView.check_object_permissionsS  s\    
 ..0 	J33GT3G&&#J	4@ VT: ' 	r"   c                    g }| j                         D ]4  }|j                  ||       r|j                  |j                                6 |r1|D cg c]  }|| }}t	        |d      }| j                  ||       yyc c}w )z|
        Check if request should be throttled.
        Raises an appropriate exception if the request is throttled.
        N)default)r   allow_requestappendr4   maxrn   )rT   rb   throttle_durationsr   duration	durationss         r    check_throttleszAPIView.check_throttles`  s    
  **, 	;H))'48"))(--/:	;  *<%' I 
 9d3HNN7H- s   A>c                 l    | j                   y| j                         } |j                  |g|i ||fS )z
        If versioning is being used, then determine any API version for the
        incoming request. Returns a two-tuple of (version, versioning_scheme)
        NN)versioning_classdetermine_version)rT   rb   r9   rc   schemes        r    r   zAPIView.determine_versionu  sC    
   (&&((((B4B6BFKKr"   c                     | j                  |      }t        || j                         | j                         | j	                         |      S )z5
        Returns the initial request object.
        )parsersrg   
negotiatorparser_context)rv   r   r   rp   r   )rT   rb   r9   rc   r   s        r    initialize_requestzAPIView.initialize_request  sK     009$$&224224)
 	
r"   c                 (    | j                   di || _        | j                  |      }|\  |_        |_         | j
                  |g|i |\  }}||c|_        |_        | j                  |       | j                  |       | j                  |       y)zX
        Runs anything that needs to occur prior to calling the method handler.
        NrH   )r   r   r   accepted_rendereraccepted_media_typer   versionversioning_schemer   r   r   )rT   rb   r9   rc   negr   r   s          r    initialzAPIView.initial  s     3D22<V< ..w7AD>!7#> 1$00J4J6J5<f22 	##G,w'W%r"   c                     t        |t              sJ dt        |      z         t        |t              rft	        |dd      s"| j                  |d      }|\  |_        |_        |j                  |_        |j                  |_        | j                         |_	        | j                  j                  dd      }|t        |t        j                  |             | j                  j                         D ]
  \  }}|||<    |S )z4
        Returns the final response object.
        zrExpected a `Response`, `HttpResponse` or `StreamingHttpResponse` to be returned from the view, but received a `%s`r   NT)r   rZ   )r7   r   typer   r   r   r   r   ry   renderer_contextr6   popr
   r	   splititems)	rT   rb   responser9   rc   r   vary_headerskeyvalues	            r    finalize_responsezAPIView.finalize_response  s    
 ($45 	
@8n	
5 h)7$7>66wd6KILF)7+F)0)B)BH&+2+F+FH((,(A(A(CH% ||''5#x):):<)HI,,,,. 	"JC!HSM	" r"   c                 T   t        |t        j                  t        j                  f      r:| j	                  | j
                        }|r||_        nt        j                  |_	        | j                         }| j                         } |||      }|| j                  |       d|_        |S )zz
        Handle any exception that occurs, by returning an appropriate response,
        or re-raising the error.
        T)r7   r   ri   AuthenticationFailedrr   rb   r3   r   HTTP_403_FORBIDDENr=   r   r{   raise_uncaught_exception	exception)rT   r>   r3   rA   r?   r   s         r    handle_exceptionzAPIView.handle_exception  s    
 cJ77&;;= > 66t||DK"-"(";"; 668446$S'2))#.!r"   c                     t         j                  r7| j                  }t        |j                  d      }|dv}|j                  |       |)Nformat)r)   apiadmin)r   DEBUGrb   r   r   force_plaintext_errors)rT   r>   rb   renderer_formatuse_plaintext_tracebacks        r    r   z APIView.raise_uncaught_exception  sD    >>llG%g&?&?JO&5=U&U#**+BC	r"   c                 "   || _         || _         | j                  |g|i |}|| _        | j                  | _        	  | j                  |g|i | |j                  j                         | j                  v r0t        | |j                  j                         | j                        }n| j                  } ||g|i |} | j                  ||g|i || _        | j                  S # t        $ r}| j                  |      }Y d}~Gd}~ww xY w)z
        `.dispatch()` is pretty much the same as Django's regular dispatch,
        but with extra hooks for startup, finalize, and exception handling.
        N)r9   rc   r   rb   r^   r6   r   ra   lowerhttp_method_namesr   rd   r   r   r   r   )rT   rb   r9   rc   handlerr   r>   s          r    dispatchzAPIView.dispatch  s   
 	)$))'CDCFC44	2DLL24262 ~~##%)?)??!$(<(<(>"&">">@ 66w888H
 /..wR4R6R}}	  	2,,S1H	2s   BC* *	D3D		Dc                     | j                    | j                  |g|i |S | j                         j                  ||       }t        |t        j
                        S )z<
        Handler method for HTTP 'OPTIONS' request.
        )r   )metadata_classrd   determine_metadatar   r   HTTP_200_OK)rT   rb   r9   rc   r@   s        r    optionszAPIView.options  s[     &/4//I$I&II""$77FV%7%788r"   r   F);r   
__module____qualname__r   DEFAULT_RENDERER_CLASSESr]   DEFAULT_PARSER_CLASSESr   DEFAULT_AUTHENTICATION_CLASSESr   DEFAULT_THROTTLE_CLASSESr   DEFAULT_PERMISSION_CLASSESr   !DEFAULT_CONTENT_NEGOTIATION_CLASSr   DEFAULT_METADATA_CLASSr   DEFAULT_VERSIONING_CLASSr   r   r   schemaclassmethodrN   propertyrU   r^   rd   rk   rn   rr   rv   ry   r{   r!   r*   r   r   r   rp   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   __classcell__)r   s   @r    rC   rC   h   sI    $<<!88N)HH#<<%@@ , N N!88N#<< H_F! !0 ' '  :E)B



 AB<@HB /.*L
&&<4:9r"   rC   Nr   )'r&   django.confr   django.core.exceptionsr   	django.dbr   r   django.httpr   django.http.responser   django.utils.cacher	   r
   django.utils.encodingr   django.views.decorators.csrfr   django.views.genericr   rest_frameworkr   r   rest_framework.requestr   rest_framework.responser   rest_framework.schemasr   rest_framework.settingsr   rest_framework.utilsr   r!   r*   r0   rA   rC   rH   r"   r    <module>r     s\    ! 3 )  1 > + 4 % - * , 0 0 +2$"Ba9d a9r"   