
    rhJ                        d 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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.
    )VERSION)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      g/var/www/html/ai-insurance-compliance-backend/venv/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+   0   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!   r1   r1   B   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   r4   r5   r6   listdictr1   r   status_code)exccontextr7   datas       r!   exception_handlerrB   H   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        dk\  rd|_        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#   )      FrI   )r8   r   r   queryQuerySetrF   
_fetch_allsuperas_viewcls
initkwargsDJANGO_VERSIONlogin_requiredr   )rR   rS   rJ   r    r   s       r!   rQ   zAPIView.as_viewz   ss     gc:t4fll6K6KL '7CLL#w,,$ V#"'D 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, rL   AcceptVary)joinrZ   lenrenderer_classes)rY   r7   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)rY   requestr:   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.
        )r6   code)authenticatorssuccessful_authenticatorr   NotAuthenticatedr   )rY   rf   messagerj   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)rY   rf   r5   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)rY   rf   rk   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.
        r:   rI   rg   )r    r:   rg   r   )rY   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.
        r:   rI   rg   rf   Nr    r:   rg   rf   rx   rX   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.
        r:   rI   rg   rf   Nr|   rx   rX   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)rY   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)rY   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)rY   rg   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.
        )ra   )rY   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)rY   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)rY   auths     r!   rt   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)rY   
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)rY   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   rX   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_HANDLERrX   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)rY   rf   force	renderersconnegs        r!   perform_content_negotiationz#APIView.perform_content_negotiation4  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)rY   rf   s     r!   perform_authenticationzAPIView.perform_authenticationB  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.
        rn   Nrj   rn   rj   )r   has_permissionro   r   )rY   rf   r   s      r!   check_permissionszAPIView.check_permissionsL  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.
        rn   Nrj   r   )r   has_object_permissionro   r   )rY   rf   objr   s       r!   check_object_permissionsz APIView.check_object_permissionsY  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appendr5   maxrr   )rY   rf   throttle_durationsr   duration	durationss         r!   check_throttleszAPIView.check_throttlesf  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)rY   rf   r:   rg   schemes        r!   r   zAPIView.determine_version{  sC    
   (&&((((B4B6BFKKr#   c                     | j                  |      }t        || j                         | j                         | j	                         |      S )z5
        Returns the initial request object.
        )parsersrk   
negotiatorparser_context)rz   r   r   rt   r   )rY   rf   r:   rg   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.
        NrI   )r   r   r   accepted_rendereraccepted_media_typer   versionversioning_schemer   r   r   )rY   rf   r:   rg   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   r^   )r8   r	   typer   r   r   r   r   r}   renderer_contextr7   popr   r
   splititems)	rY   rf   responser:   rg   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)r8   r   rm   AuthenticationFailedrv   rf   r4   r   HTTP_403_FORBIDDENr>   r   r   raise_uncaught_exception	exception)rY   r?   r4   rB   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   DEBUGrf   r   r   force_plaintext_errors)rY   r?   rf   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)r:   rg   r   rf   rb   r7   r   re   lowerhttp_method_namesr   rh   r   r   r   r   )rY   rf   r:   rg   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_classrh   determine_metadatar   r   HTTP_200_OK)rY   rf   r:   rg   rA   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_CLASSESra   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classmethodrQ   propertyrZ   rb   rh   ro   rr   rv   rz   r}   r   r"   r+   r   r   r   rt   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   __classcell__)r   s   @r!   rD   rD   i   sI    $<<!88N)HH#<<%@@ , N N!88N#<< H_F! !: ' '  :E)B



 AB<@HB /.*L
&&<4:9r#   rD   Nr   )*r'   djangor   rT   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+   r1   rB   rD   rI   r#   r!   <module>r     s_    -   3 )  1 > + 4 % - * , 0 0 +2$"Bf9d f9r#   