o
    h                      @   s$  d Z ddlZddlZddlZddlmZ ddl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 erddd	lmZmZmZmZ dd
lmZ ddlmZ ddlmZmZ ededef dZeedrpejZej Z nejZd ddZ dd Z!dd Z"dd Z#dd Z$dd Z%dd Z&dS )!z
Instrumentation for Django 3.0

Since this file contains `async def` it is conditionally imported in
`sentry_sdk.integrations.django` (depending on the existence of
`django.core.handlers.asgi`.
    N)WSGIRequest)OP)SentryAsgiMiddleware)should_send_default_pii)capture_internal_exceptionsensure_integration_enabled)TYPE_CHECKING)AnyCallableUnionTypeVar)ASGIRequest)HttpResponse)EventEventProcessor_F.)boundmarkcoroutinefunctionfuncreturnc                 C   s   t jj| _| S N)asyncio
coroutines_is_coroutine)r    r   e/var/www/html/pro-man-master/venv/lib/python3.10/site-packages/sentry_sdk/integrations/django/asgi.pyr   1   s   
c                    s    fdd}|S )Nc                    s   ddl m}m}  d u r| S t tkr| S t  | |  W d    n1 s+w   Y  t rLt  | |  W d    | S 1 sGw   Y  | S )Nr   )DjangoRequestExtractor_set_user_info)sentry_sdk.integrations.djangor   r   typer   r   extract_into_eventr   )eventhintr   r   requestr   r   asgi_request_event_processor8   s   
zH_make_asgi_request_event_processor.<locals>.asgi_request_event_processorr   )r$   r%   r   r#   r   "_make_asgi_request_event_processor6   s   r&   c                    s`   ddl m  j fdd}|_td}|r.jt fdd}|_d S d S )Nr   DjangoIntegrationc                    s\   t   }|d u r| |||I d H S t| d j|jdj}||||I d H S )NTunsafe_context_dataspan_originhttp_methods_to_capture)
sentry_sdk
get_clientget_integrationr   __get__originr,   
_run_asgi3)selfscopereceivesendintegration
middlewarer(   clsold_appr   r   sentry_patched_asgi_handler[   s   
zCpatch_django_asgi_handler_impl.<locals>.sentry_patched_asgi_handlercreate_requestc                    s8    | g|R i |\}}t  }|t| ||fS r   )r-   get_isolation_scopeadd_event_processorr&   )r3   argskwargsr$   error_responser4   )old_create_requestr   r   sentry_patched_create_requestp   s   zEpatch_django_asgi_handler_impl.<locals>.sentry_patched_create_request)r   r(   __call__hasattrr=   r   )r:   r<   modern_django_asgi_supportrD   r   )r(   r:   r;   rC   r   patch_django_asgi_handler_implT   s   

rH   c                    s   | j  fdd}|| _ d S )Nc                    s    | | |I d H S r   r   )r3   r$   _before_get_responseold_get_response_asyncr   r   !sentry_patched_get_response_async   s   zCpatch_get_response_async.<locals>.sentry_patched_get_response_async)get_response_async)r:   rJ   rL   r   rI   r   patch_get_response_async|   s   
rN   c                    sJ   dd l }ddlm  |jdk rj fdd}|_d S t d S )Nr   r'   z3.0.0c                    s`   t  }|d u r ||I d H S t fdddj|jd}| j||I d H S )Nc                    s     S r   )r0   )_scope)r:   r;   r3   r   r   <lambda>   s    zWpatch_channels_asgi_handler_impl.<locals>.sentry_patched_asgi_handler.<locals>.<lambda>Tr)   )r-   r.   r/   r   r1   r,   r4   )r3   r5   r6   r7   r8   r9   r3   r   r<      s   zEpatch_channels_asgi_handler_impl.<locals>.sentry_patched_asgi_handler)channelsr   r(   __version__rE   rH   )r:   rR   r<   r   r9   r    patch_channels_asgi_handler_impl   s   

rT   c                    s(   ddl m  t fdd}|S )Nr   r'   c                    s   t  }|jd ur|j  t  }|jd ur|j  t jtj	| j
j jd | g|R i |I d H W  d    S 1 sBw   Y  d S )N)opnamer1   )r-   get_current_scopetransactionupdate_active_threadr>   profileupdate_active_thread_id
start_spanr   VIEW_RENDERresolver_match	view_namer1   )r$   r@   rA   current_scopesentry_scoper(   callbackr   r   sentry_wrapped_callback   s   



$z0wrap_async_view.<locals>.sentry_wrapped_callback)r   r(   	functoolswraps)rc   rd   r   rb   r   wrap_async_view   s   rg   c                    s   G  fddd}|S )zh
    Mixin class factory that generates a middleware mixin for handling requests
    in async mode.
    c                       s8   e Zd ZerdZdd Zdd Zdd Z fdd	ZdS )
z7_asgi_middleware_mixin_factory.<locals>.SentryASGIMixinNc                 S   s   || _ d | _|   d S r   )get_response_acall_method_async_check)r3   rh   r   r   r   __init__   s   z@_asgi_middleware_mixin_factory.<locals>.SentryASGIMixin.__init__c                 S   s   t | jrt|  dS dS )z
            If get_response is a coroutine function, turns us into async mode so
            a thread is not consumed during a whole request.
            Taken from django.utils.deprecation::MiddlewareMixin._async_check
            N)iscoroutinefunctionrh   r   rQ   r   r   r   rj      s   
zD_asgi_middleware_mixin_factory.<locals>.SentryASGIMixin._async_checkc                 S   s
   t | jS )z
            Function that checks if we are in async mode,
            and if we are forwards the handling of requests to __acall__
            )rl   rh   rQ   r   r   r   async_route_check   s   
zI_asgi_middleware_mixin_factory.<locals>.SentryASGIMixin.async_route_checkc                    s   | j }|d u rt| jdr| jj | _ }n| j | _ } |d}|d u r/||i |I d H S | ||i |I d H W  d    S 1 sFw   Y  d S )N	__acall__)
old_method)ri   rF   _innerrn   )r3   r@   rA   fmiddleware_span_check_middleware_spanr   r   rn      s   
$zA_asgi_middleware_mixin_factory.<locals>.SentryASGIMixin.__acall__)	__name__
__module____qualname__r   rp   rk   rj   rm   rn   r   rs   r   r   SentryASGIMixin   s    
rx   r   )rt   rx   r   rs   r   _asgi_middleware_mixin_factory   s   -ry   )r   r   r   r   )'__doc__r   re   inspectdjango.core.handlers.wsgir   r-   sentry_sdk.constsr   sentry_sdk.integrations.asgir   sentry_sdk.scoper   sentry_sdk.utilsr   r   typingr   r	   r
   r   r   django.core.handlers.asgir   django.http.responser   sentry_sdk._typesr   r   r   rF   rl   r   r&   rH   rN   rT   rg   ry   r   r   r   r   <module>   s8    

( 