o
     ÞhžJ  ã                   @   s  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d„ Z%ddd„Z&dd„ Z'dd„ Z(G dd„ deƒZ)dS )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                 C   sb   t | ddƒ}|dur|S | jj}t |d¡}t |d¡}t |¡}t | ddƒ}|r/|d| 7 }|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   © r   úT/var/www/html/premium_crap/venv/lib/python3.10/site-packages/rest_framework/views.pyÚget_view_name   s   
r!   Fc                 C   s@   t | ddƒ}|du r| jjpd}t t|ƒ¡}|rt |¡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"   r   r   r    Úget_view_description0   s   
r(   c                  C   s,   t  ¡ D ]} | jd r| jr|  d¡ qd S )NÚATOMIC_REQUESTST)r   ÚallÚsettings_dictÚin_atomic_blockÚset_rollback)Údbr   r   r    r-   B   s
   
€þr-   c                 C   sª   t | tƒrtj| jŽ } nt | tƒrtj| jŽ } t | tjƒrSi }t| ddƒr*| j|d< t| ddƒr7d| j	 |d< t | j
ttfƒrC| j
}nd| j
i}tƒ  t|| j|d�S dS )	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   r/   r0   r1   ÚlistÚdictr-   r   Ústatus_code)ÚexcÚcontextr2   Údatar   r   r    Úexception_handlerH   s    




r=   c                       sh  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d„ƒZedd„ ƒZedd„ ƒZdd„ ZdEd
d„Zdd„ Zdd„ Zdd„ Z dd„ Z!dd„ Z"dd„ Z#dFd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,dFd+d,„Z-d-d.„ Z.d/d0„ Z/d1d2„ Z0d3d4„ Z1d5d6„ Z2d7d8„ Z3d9d:„ Z4d;d<„ Z5d=d>„ Z6d?d@„ Z7dAdB„ Z8dCdD„ Z9‡  Z:S )GÚAPIViewc                    sZ   t t| ddƒtjjƒrdd„ }|| j_tƒ j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                   S   s   t dƒ‚)Nz�Do not evaluate the `.queryset` attribute directly, as the result will be cached and reused between requests. Use `.all()` or call `.get_queryset()` instead.)ÚRuntimeErrorr   r   r   r    Úforce_evaluationƒ   s   ÿz)APIView.as_view.<locals>.force_evaluation)é   é   Fr   )r3   r   r   ÚqueryÚQuerySetr?   Ú
_fetch_allÚsuperÚas_viewÚclsÚ
initkwargsÚDJANGO_VERSIONÚlogin_requiredr   )rI   rJ   rA   r   ©r   r   r    rH   z   s   zAPIView.as_viewc                 C   s   |   ¡ S )zZ
        Wrap Django's private `_allowed_methods` interface in a public property.
        )Ú_allowed_methods©Úselfr   r   r    Úallowed_methods˜   s   zAPIView.allowed_methodsc                 C   s*   dd  | j¡i}t| jƒdkrd|d< |S )NÚAllowz, rC   ÚAcceptÚVary)ÚjoinrQ   ÚlenÚrenderer_classes)rP   r2   r   r   r    Údefault_response_headersŸ   s
   ÿz APIView.default_response_headersc                 O   s   t  |j¡‚)z�
        If `request.method` does not correspond to a handler method,
        determine what kind of exception to raise.
        )r   ÚMethodNotAllowedÚmethod)rP   Úrequestr5   Úkwargsr   r   r    Úhttp_method_not_allowed¨   s   zAPIView.http_method_not_allowedNc                 C   s"   |j r
|js
t ¡ ‚tj||d�‚)zY
        If request is not permitted, determine what kind of exception to raise.
        )r1   Úcode)ÚauthenticatorsÚsuccessful_authenticatorr   ÚNotAuthenticatedr   )rP   r[   Úmessager^   r   r   r    Úpermission_denied¯   s   zAPIView.permission_deniedc                 C   s
   t  |¡‚)zU
        If request is throttled, determine what kind of exception to raise.
        )r   Ú	Throttled)rP   r[   r0   r   r   r    Ú	throttled·   s   
zAPIView.throttledc                 C   s   |   ¡ }|r|d  |¡S dS )z‚
        If a request is unauthenticated, determine the WWW-Authenticate
        header to use for 401 responses, if any.
        r   N)Úget_authenticatorsÚauthenticate_header)rP   r[   r_   r   r   r    Úget_authenticate_header½   s   ÿzAPIView.get_authenticate_headerc                 C   s   | 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.
        r5   r   r\   )r   r5   r\   ©r   )rP   Úhttp_requestr   r   r    Úget_parser_contextÆ   s   

ýzAPIView.get_parser_contextc                 C   ó&   | 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.
        r5   r   r\   r[   N©r   r5   r\   r[   ri   rO   r   r   r    Úget_renderer_contextÓ   s
   


üzAPIView.get_renderer_contextc                 C   rl   )zp
        Returns a dict that is passed through to EXCEPTION_HANDLER,
        as the `context` argument.
        r5   r   r\   r[   Nrm   ri   rO   r   r   r    Úget_exception_handler_contextá   s
   


üz%APIView.get_exception_handler_contextc                 C   s   | j j}|| ƒS )zf
        Return the view name, as used in OPTIONS responses and in the
        browsable API.
        )r   ÚVIEW_NAME_FUNCTION)rP   Úfuncr   r   r    r!   í   s   zAPIView.get_view_nameFc                 C   s   | 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)rP   r'   rq   r   r   r    r(   õ   s   
zAPIView.get_view_descriptionc                 K   s   | j jr| | j j¡S dS )zQ
        Determine if the request includes a '.json' style format suffix
        N)r   ÚFORMAT_SUFFIX_KWARGÚget)rP   r\   r   r   r    Úget_format_suffixÿ   s   ÿzAPIView.get_format_suffixc                 C   ó   dd„ | j D ƒS )zX
        Instantiates and returns the list of renderers that this view can use.
        c                 S   ó   g | ]}|ƒ ‘qS r   r   )Ú.0Úrendererr   r   r    Ú
<listcomp>
  ó    z)APIView.get_renderers.<locals>.<listcomp>)rW   rO   r   r   r    Úget_renderers  ó   zAPIView.get_renderersc                 C   rv   )zV
        Instantiates and returns the list of parsers that this view can use.
        c                 S   rw   r   r   )rx   Úparserr   r   r    rz     r{   z'APIView.get_parsers.<locals>.<listcomp>)Úparser_classesrO   r   r   r    Úget_parsers  r}   zAPIView.get_parsersc                 C   rv   )z]
        Instantiates and returns the list of authenticators that this view can use.
        c                 S   rw   r   r   )rx   Úauthr   r   r    rz     r{   z.APIView.get_authenticators.<locals>.<listcomp>)Úauthentication_classesrO   r   r   r    rf     r}   zAPIView.get_authenticatorsc                 C   rv   )z[
        Instantiates and returns the list of permissions that this view requires.
        c                 S   rw   r   r   )rx   Ú
permissionr   r   r    rz     r{   z+APIView.get_permissions.<locals>.<listcomp>)Úpermission_classesrO   r   r   r    Úget_permissions  r}   zAPIView.get_permissionsc                 C   rv   )zU
        Instantiates and returns the list of throttles that this view uses.
        c                 S   rw   r   r   )rx   Úthrottler   r   r    rz   "  r{   z)APIView.get_throttles.<locals>.<listcomp>)Úthrottle_classesrO   r   r   r    Úget_throttles  r}   zAPIView.get_throttlesc                 C   s   t | ddƒs|  ¡ | _| jS )zN
        Instantiate and return the content negotiation class to use.
        Ú_negotiatorN)r   Úcontent_negotiation_classr‰   rO   r   r   r    Úget_content_negotiator$  s   
zAPIView.get_content_negotiatorc                 C   s   | j jS )zD
        Returns the exception handler that this view uses.
        )r   ÚEXCEPTION_HANDLERrO   r   r   r    Úget_exception_handler,  s   zAPIView.get_exception_handlerc                 C   sN   |   ¡ }|  ¡ }z	| ||| j¡W S  ty&   |r%|d |d jf Y S ‚ w )zU
        Determine which renderer and media type to use render the response.
        r   )r|   r‹   Úselect_rendererÚformat_kwargÚ	ExceptionÚ
media_type)rP   r[   ÚforceÚ	renderersÚconnegr   r   r    Úperform_content_negotiation4  s   ýz#APIView.perform_content_negotiationc                 C   s
   |j  dS )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)rP   r[   r   r   r    Úperform_authenticationB  s   
zAPIView.perform_authenticationc                 C   s>   |   ¡ D ]}| || ¡s| j|t|ddƒt|ddƒd� qdS )z„
        Check if the request should be permitted.
        Raises an appropriate exception if the request is not permitted.
        rb   Nr^   ©rb   r^   )r…   Úhas_permissionrc   r   )rP   r[   rƒ   r   r   r    Úcheck_permissionsL  s   

ý€þzAPIView.check_permissionsc                 C   s@   |   ¡ D ]}| || |¡s| j|t|ddƒt|ddƒd� qdS )z—
        Check if the request should be permitted for a given object.
        Raises an appropriate exception if the request is not permitted.
        rb   Nr^   r˜   )r…   Úhas_object_permissionrc   r   )rP   r[   Úobjrƒ   r   r   r    Úcheck_object_permissionsY  s   

ý€þz APIView.check_object_permissionsc                 C   s^   g }|   ¡ D ]}| || ¡s| | ¡ ¡ q|r-dd„ |D ƒ}t|dd�}|  ||¡ dS dS )z|
        Check if request should be throttled.
        Raises an appropriate exception if the request is throttled.
        c                 S   s   g | ]}|d ur|‘qS )Nr   )rx   Údurationr   r   r    rz   s  s
    ÿz+APIView.check_throttles.<locals>.<listcomp>N)Údefault)rˆ   Úallow_requestÚappendr0   Úmaxre   )rP   r[   Úthrottle_durationsr†   Ú	durationsrž   r   r   r    Úcheck_throttlesf  s   €ÿ÷zAPIView.check_throttlesc                 O   s2   | j du rdS |   ¡ }|j|g|¢R 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)
        N©NN)Úversioning_classÚdetermine_version)rP   r[   r5   r\   Úschemer   r   r    r¨   {  s   
zAPIView.determine_versionc                 O   s(   |   |¡}t||  ¡ |  ¡ |  ¡ |d�S )z5
        Returns the initial request object.
        )Úparsersr_   Ú
negotiatorÚparser_context)rk   r   r€   rf   r‹   )rP   r[   r5   r\   r¬   r   r   r    Úinitialize_request‡  s   
ûzAPIView.initialize_requestc                 O   st   | j di |¤Ž| _|  |¡}|\|_|_| j|g|¢R i |¤Ž\}}|||_|_|  |¡ |  	|¡ |  
|¡ dS )zX
        Runs anything that needs to occur prior to calling the method handler.
        Nr   )ru   r�   r•   Úaccepted_rendererÚaccepted_media_typer¨   ÚversionÚversioning_schemer—   rš   r¥   )rP   r[   r5   r\   Únegr°   r©   r   r   r    Úinitial•  s   


zAPIView.initialc           	      O   sª   t |tƒsJ dt|ƒ ƒ‚t |tƒr2t|ddƒs%| j|dd�}|\|_|_|j|_|j|_|  ¡ |_	| j
 dd¡}|durEt|t |¡ƒ | j
 ¡ D ]\}}|||< qJ|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’   rT   )r3   r   Útyper   r   r•   r®   r¯   rn   Úrenderer_contextr2   Úpopr
   r	   ÚsplitÚitems)	rP   r[   Úresponser5   r\   r²   Úvary_headersÚkeyÚvaluer   r   r    Úfinalize_response¨  s$   þÿ


zAPIView.finalize_responsec                 C   sh   t |tjtjfƒr|  | j¡}|r||_ntj|_	|  
¡ }|  ¡ }|||ƒ}|du r/|  |¡ d|_|S )zz
        Handle any exception that occurs, by returning an appropriate response,
        or re-raising the error.
        NT)r3   r   ra   ÚAuthenticationFailedrh   r[   r/   r   ÚHTTP_403_FORBIDDENr9   r�   ro   Úraise_uncaught_exceptionÚ	exception)rP   r:   r/   r=   r;   r¹   r   r   r    Úhandle_exceptionÆ  s   ÿ

zAPIView.handle_exceptionc                 C   s.   t jr| j}t|jdƒ}|dv}| |¡ |‚)NÚformat)r'   ÚapiÚadmin)r   ÚDEBUGr[   r   r®   Úforce_plaintext_errors)rP   r:   r[   Úrenderer_formatÚuse_plaintext_tracebackr   r   r    rÀ   à  s   
z APIView.raise_uncaught_exceptionc              
   O   sâ   || _ || _| j|g|¢R i |¤Ž}|| _| j| _z/| j|g|¢R i |¤Ž |j ¡ | j	v r9t
| |j ¡ | jƒ}n| j}||g|¢R i |¤Ž}W n ty_ } z|  |¡}W Y d}~nd}~ww | j||g|¢R i |¤Ž| _| jS )z¡
        `.dispatch()` is pretty much the same as Django's regular dispatch,
        but with extra hooks for startup, finalize, and exception handling.
        N)r5   r\   r­   r[   rX   r2   r³   rZ   ÚlowerÚhttp_method_namesr   r]   r�   rÂ   r½   r¹   )rP   r[   r5   r\   Úhandlerr¹   r:   r   r   r    Údispatchë  s&   ÿ€ÿzAPIView.dispatchc                 O   s@   | j du r| j|g|¢R i |¤ŽS |   ¡  || ¡}t|tjd�S )z<
        Handler method for HTTP 'OPTIONS' request.
        N)r   )Úmetadata_classr]   Údetermine_metadatar   r   ÚHTTP_200_OK)rP   r[   r5   r\   r<   r   r   r    Úoptions  s   
zAPIView.optionsr¦   ©F);r   Ú
__module__Ú__qualname__r   ÚDEFAULT_RENDERER_CLASSESrW   Ú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ÚclassmethodrH   ÚpropertyrQ   rX   r]   rc   re   rh   rk   rn   ro   r!   r(   ru   r|   r€   rf   r…   rˆ   r‹   r�   r•   r—   rš   r�   r¥   r¨   r­   r³   r½   rÂ   rÀ   rÍ   rÑ   Ú__classcell__r   r   rM   r    r>   i   s^    


	



r>   NrÒ   )*r$   Údjangor   rK   Ú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(   r-   r=   r>   r   r   r   r    Ú<module>   s,    
!