3
g2                 @   s   d dl Z d dl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
 d dlmZmZ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 d d
lmZ d dlmZ dd Zdd Zdd Zdd Z dS )    N)wraps)sync_to_async)settings)signals)
BadRequestPermissionDeniedRequestDataTooBigSuspiciousOperationTooManyFieldsSentTooManyFilesSent)Http404)MultiPartParserError)get_resolverget_urlconf)log_response)debugc                s>   t j r"t  fdd}|S t  fdd}|S dS )a+  
    Wrap the given get_response callable in exception-to-response conversion.

    All exceptions will be converted. All known 4xx exceptions (Http404,
    PermissionDenied, MultiPartParserError, SuspiciousOperation) will be
    converted to the appropriate response, and all other exceptions will be
    converted to 500 responses.

    This decorator is automatically applied to all middleware to ensure that
    no middleware leaks an exception and that the next middleware in the stack
    can rely on getting a response instead of an exception.
    c                sR   y | I d H }W n: t k
rL } zttdd| |I d H }W Y d d }~X nX |S )NF)Zthread_sensitive)	Exceptionr   response_for_exception)requestresponseexc)get_response \/var/www/tester-filtro-web/env/lib/python3.6/site-packages/django/core/handlers/exception.pyinner#   s
    *z,convert_exception_to_response.<locals>.innerc                s>   y | }W n, t k
r8 } zt| |}W Y d d }~X nX |S )N)r   r   )r   r   r   )r   r   r   r   ,   s
    N)asyncioiscoroutinefunctionr   )r   r   r   )r   r   convert_exception_to_response   s
    
r   c             C   s  t |tr6tjrtj| |}nt| tt d|}nt |t	rpt| tt d|}t
d| j|| tj d nxt |trt| tt d|}t
d| j|| tj d n>t |trtjrtj| ftj ddi}nt| tt d|}t
dt|| j|| tj d nt |trt |tttfr2| j  tjd	|jj }|jt|d| d
d tjrtj| ftj ddi}nt| tt d|}nRt |tr nBtjjd | d t | tt tj }t
d|j!| j|| tj d t"|dd rt#t"|dd r|j$ }|S )Ni  i  z!Forbidden (Permission denied): %s)r   r   exc_infoi  z.Bad request (Unable to parse request body): %sstatus_codez%s: %szdjango.security.%s)r   r   )extra)senderr   Zis_renderedTrender)%
isinstancer   r   DEBUGr   Ztechnical_404_responseget_exception_responser   r   r   r   pathsysr   r   r   technical_500_responsestrr	   r   r
   r   Z_mark_post_parse_errorlogging	getLogger	__class____name__error
SystemExitr   got_request_exceptionsendhandle_uncaught_exceptionZreason_phrasegetattrcallabler"   )r   r   r   Zsecurity_loggerr   r   r   r   6   sb    



"r   c             C   sT   y|j |}|| |d}W n4 tk
rN   tjjd | d t| |tj }Y nX |S )N)	exception)r!   r   )resolve_error_handlerr   r   r0   r1   r2   r'   r   )r   resolverr   r5   callbackr   r   r   r   r%      s    
r%   c             C   s0   t jr t jrtj| f| S |jd}|| S )zm
    Processing for any otherwise uncaught exceptions (those that will
    generate HTTP 500 responses).
    i  )r   ZDEBUG_PROPAGATE_EXCEPTIONSr$   r   r(   r6   )r   r7   r   r8   r   r   r   r2      s    
r2   )!r   r*   r'   	functoolsr   Zasgiref.syncr   Zdjango.confr   Zdjango.corer   Zdjango.core.exceptionsr   r   r   r	   r
   r   Zdjango.httpr   Zdjango.http.multipartparserr   Zdjango.urlsr   r   Zdjango.utils.logr   Zdjango.viewsr   r   r   r%   r2   r   r   r   r   <module>   s     !K