B
     `>                 @   s  d dl mZ d dlZd dlZd dl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mZmZmZmZmZ d d	lmZmZ d d
lmZ d dlmZ d dlm Z  yPd dl!m"Z# d dl$m%Z% yd dl&m'Z' W n" e(k
r   d dl)m'Z' Y nX W n e(k
r*   edY nX d dl*m+Z+ d dl,m-Z- d dl.m/Z/ d dl0m1Z1 erd dl2m3Z3 d dl2m4Z4 d dl2m5Z5 d dl2m6Z6 d dl2m7Z7 d dl2m8Z8 d dl9m:Z: d dl;m<Z< d dl=m>Z> d dl?m@Z@ d dlmAZA d d lmBZBmCZCmDZDmEZE e#d!k rd"d# ZFnd$d# ZFd%ZGG d&d' d'eZHd(aIeJ ZKd)d* ZLd+d, ZMd-d. ZNd/d0 ZOd1d2 ZPd3d4 ZQd=d5d6ZRG d7d8 d8e ZSd9d: ZTd;d< ZUdS )>    )absolute_importN)MYPY)Hub_should_send_default_pii)add_global_event_processor)add_global_repr_processor)record_sql_queries)HAS_REAL_CONTEXTVARSCONTEXTVARS_ERROR_MESSAGEloggercapture_internal_exceptionsevent_from_exceptiontransaction_from_functionwalk_exception_chain)IntegrationDidNotEnable)ignore_logger)SentryWsgiMiddleware)RequestExtractor)VERSION)signals)resolvezDjango not installed)LEGACY_RESOLVER)!get_template_frame_from_exception)patch_django_middlewares)patch_views)Any)Callable)Dict)Optional)Union)List)WSGIRequest)HttpResponse)	QueryDict)MultiValueDict)_ScopedResponse)EventHintEventProcessorNotImplementedType)   
   c             C   s   |   S )N)is_authenticated)request_user r/   V/home/dcms/DCMS/lib/python3.7/site-packages/sentry_sdk/integrations/django/__init__.pyr-   @   s    r-   c             C   s   | j S )N)r-   )r.   r/   r/   r0   r-   G   s    )function_nameurlc               @   s.   e Zd ZdZdZdZd	ddZedd ZdS )
DjangoIntegrationdjangoNr2   Tc             C   s(   |t krtd|t f || _|| _d S )Nz7Invalid value for transaction_style: %s (must be in %s))TRANSACTION_STYLE_VALUES
ValueErrortransaction_stylemiddleware_spans)selfr7   r8   r/   r/   r0   __init__U   s    zDjangoIntegration.__init__c                 s   t dk rtdt  td td ddlm   j fdd} |  _t  t  t	j
t td	d
 }tdd }t  t  t  d S )N)r+      z Django 1.6 or newer is required.zdjango.serverzdjango.requestr   )WSGIHandlerc                s6   t jtd kr| ||S |  }t|||S )N)r   currentget_integrationr3   __get__r   )r9   environZstart_responseZbound_old_app)r<   old_appr/   r0   sentry_patched_wsgi_handlerq   s    zADjangoIntegration.setup_once.<locals>.sentry_patched_wsgi_handlerc             S   s   |d kr| S | dd }|d kr$| S |  dd }|d kr<| S | dd }|d krT| S xtt|t|D ]\}\}}}t|}|d k	rh| di  dg }xNttt|D ]2}	||	 }
|
 ddkr|
 dd	kr|	d
7 }	P qW t|}	||	| qhW | S )Nexc_info	exceptionvaluesZ
stacktraceframesfunction)zParser.parseparserendermodulezdjango.template.baser+   )getzipreversedr   r   rangeleninsert)eventhintrC   rD   rE   _	exc_valueframerF   ifr/   r/   r0   process_django_templates   s2    z>DjangoIntegration.setup_once.<locals>.process_django_templatesc             S   sR   yddl m} W n tk
r$   tS X t| |r6| jr:tS d| jj| jt	| f S )Nr   )QuerySetz<%s from %s at 0x%x>)
Zdjango.db.models.queryrY   	ExceptionNotImplemented
isinstanceZ_result_cache	__class____name__
__module__id)valuerR   rY   r/   r/   r0   _django_queryset_repr   s    z;DjangoIntegration.setup_once.<locals>._django_queryset_repr)DJANGO_VERSIONr   install_sql_hookr   django.core.handlers.wsgir<   __call___patch_get_response_patch_django_asgi_handlerr   Zgot_request_exceptionconnect_got_request_exceptionr   r   _patch_channelsr   r   )rB   rX   rb   r/   )r<   rA   r0   
setup_once_   s"    	+zDjangoIntegration.setup_once)r2   T)	r^   r_   __qualname__
identifierr7   r8   r:   staticmethodrl   r/   r/   r/   r0   r3   O   s
   

r3   Fc           
      sx   t rdS tb t rdS da t D yddlm}  W n tk
rF   Y nX | j  fdd}|| _W dQ R X W dQ R X dS )a  
    Patch Django Rest Framework for more/better request data. DRF's request
    type is a wrapper around Django's request type. The attribute we're
    interested in is `request.data`, which is a cached property containing a
    parsed request body. Reading a request body from that property is more
    reliable than reading from any of Django's own properties, as those don't
    hold payloads in memory and therefore can only be accessed once.

    We patch the Django request object to include a weak backreference to the
    DRF request object, such that we can later use either in
    `DjangoRequestExtractor`.

    This function is not called directly on SDK setup, because importing almost
    any part of Django Rest Framework will try to access Django settings (where
    `sentry_sdk.init()` might be called from in the first place). Instead we
    run this function on every request and do the patching on the first
    request.
    NTr   )APIViewc          	      s2   t   t||j_W d Q R X  | |f||S )N)r   weakrefref_request_sentry_drf_request_backref)r9   requestargskwargs)old_drf_initialr/   r0   sentry_patched_drf_initial   s
    

z._patch_drf.<locals>.sentry_patched_drf_initial)_DRF_PATCHED_DRF_PATCH_LOCKr   Zrest_framework.viewsrp   ImportErrorinitial)rp   ry   r/   )rx   r0   
_patch_drf   s    	r~   c              C   sP   yddl m}  W n tk
r$   d S X ts8tdt  ddlm} ||  d S )Nr   )AsgiHandlerz3We detected that you are using Django channels 2.0.) patch_channels_asgi_handler_impl)	Zchannels.httpr   r|   r	   r   warningr
   #sentry_sdk.integrations.django.asgir   )r   r   r/   r/   r0   rk   	  s    rk   c              C   sP   yddl m}  W n tk
r$   d S X ts8tdt  ddlm} ||  d S )Nr   )ASGIHandlerz(We detected that you are using Django 3.)patch_django_asgi_handler_impl)	Zdjango.core.handlers.asgir   r|   r	   r   r   r
   r   r   )r   r   r/   r/   r0   rh   "  s    
rh   c          	   C   s   t j}|t}|d krd S t  | l}y:|jdkrLtt| j	j
|_n|jdkrdt| j	|_W n tk
rz   Y nX |tt| | W d Q R X d S )Nr1   r2   )r   r=   r>   r3   r~   Zconfigure_scoper7   r   r   pathfuncZtransactionr   rZ   Zadd_event_processor_make_event_processorrq   rr   )ru   hubintegrationZscoper/   r/   r0   _before_get_response8  s     



r   c                 sH   ddl m}  | j  fdd}|| _t| drDddlm} || t dS )zU
    patch get_response, because at that point we have the Django request object
    r   )BaseHandlerc                s   t |  | |S )N)r   )r9   ru   )old_get_responser/   r0   sentry_patched_get_response[  s    z8_patch_get_response.<locals>.sentry_patched_get_responseZget_response_async)patch_get_response_asyncN)Zdjango.core.handlers.baser   Zget_responsehasattrr   r   r   )r   r   r   r/   )r   r0   rg   R  s    
rg   c                s    fdd}|S )Nc          	      s     }|d kr| S y|  }|d k	r(|}W n tk
r>   Y nX t  t||  W d Q R X t rt  t||  W d Q R X | S )N)rt   AttributeErrorr   DjangoRequestExtractorZextract_into_eventr   _set_user_info)rQ   rR   ru   Zdrf_request)weak_requestr/   r0   event_processorj  s    z._make_event_processor.<locals>.event_processorr/   )r   r   r   r/   )r   r0   r   h  s    r   c             K   sN   t j}|t}|d k	rJ|j}tt |jdddd\}}|j	||d d S )Nr4   F)typeZhandled)Zclient_optionsZ	mechanism)rR   )
r   r=   r>   r3   clientr   sysrC   optionsZcapture_event)ru   rw   r   r   r   rQ   rR   r/   r/   r0   rj     s    
rj   c               @   sD   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dS )r   c             C   s   | j jS )N)ru   ZMETA)r9   r/   r/   r0   env  s    zDjangoRequestExtractor.envc             C   s   | j jS )N)ru   ZCOOKIES)r9   r/   r/   r0   cookies  s    zDjangoRequestExtractor.cookiesc             C   s   | j jS )N)ru   body)r9   r/   r/   r0   raw_data  s    zDjangoRequestExtractor.raw_datac             C   s   | j jS )N)ru   POST)r9   r/   r/   r0   form  s    zDjangoRequestExtractor.formc             C   s   | j jS )N)ru   ZFILES)r9   r/   r/   r0   files  s    zDjangoRequestExtractor.filesc             C   s   |j S )N)size)r9   filer/   r/   r0   size_of_file  s    z#DjangoRequestExtractor.size_of_filec             C   s(   y| j jS  tk
r"   t| S X d S )N)ru   datar   r   parsed_body)r9   r/   r/   r0   r     s    z"DjangoRequestExtractor.parsed_bodyN)
r^   r_   rm   r   r   r   r   r   r   r   r/   r/   r/   r0   r     s   r   c             C   s   | di }t| dd }|d ks(t|s,d S y| dt|j W n tk
rV   Y nX y| d|j W n tk
r~   Y nX y| d|  W n tk
r   Y nX d S )Nuserr`   emailusername)
setdefaultgetattrr-   strpkrZ   r   Zget_username)ru   rQ   Z	user_infor   r/   r/   r0   r     s     r   c                 s   yddl m}  W n  tk
r0   ddlm}  Y nX y| j | jW n tk
rV   dS X d	 fdd	}fdd}|| _|| _td dS )
z9If installed this causes Django's queries to be captured.r   )CursorWrapperNc          	      sN   t j}|td kr  | ||S t|| j||ddd  | ||S Q R X d S )NformatF)
paramstyleexecutemany)r   r=   r>   r3   r   cursor)r9   sqlparamsr   )real_executer/   r0   execute  s    z!install_sql_hook.<locals>.executec          	      sN   t j}|td kr  | ||S t|| j||ddd  | ||S Q R X d S )Nr   T)r   r   )r   r=   r>   r3   r   r   )r9   r   Z
param_listr   )real_executemanyr/   r0   r     s    z%install_sql_hook.<locals>.executemanyzdjango.db.backends)N)Zdjango.db.backends.utilsr   r|   Zdjango.db.backends.utilr   r   r   r   )r   r   r   r/   )r   r   r0   rd     s    
rd   )N)V
__future__r   r   	threadingrq   Zsentry_sdk._typesr   Zsentry_sdk.hubr   r   Zsentry_sdk.scoper   Zsentry_sdk.serializerr   Zsentry_sdk.tracingr   Zsentry_sdk.utilsr	   r
   r   r   r   r   r   Zsentry_sdk.integrationsr   r   Zsentry_sdk.integrations.loggingr   Zsentry_sdk.integrations.wsgir   Z$sentry_sdk.integrations._wsgi_commonr   r4   r   rc   Zdjango.corer   Zdjango.urlsr   r|   Zdjango.core.urlresolversZ+sentry_sdk.integrations.django.transactionsr   Z(sentry_sdk.integrations.django.templatesr   Z)sentry_sdk.integrations.django.middlewarer   Z$sentry_sdk.integrations.django.viewsr   typingr   r   r   r   r    r!   re   r"   Zdjango.http.responser#   Zdjango.http.requestr$   Zdjango.utils.datastructuresr%   r&   r'   r(   r)   r*   r-   r5   r3   rz   Lockr{   r~   rk   rh   r   rg   r   rj   r   r   rd   r/   r/   r/   r0   <module>   sp   $	

7
!