3
gW                 @   sZ  d dl Z d dlZ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
mZmZ d dlmZ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lmZ d dlmZ d dlm Z  edddidZ!ee"j#Z$G dd de%Z&G dd dZ'd'ddZ(e j) dd Z*dd Z+dd Z,G dd  d Z-G d!d" d"Z.d#d$ Z/d%d& Z0dS )(    N)Path)settings)Http404HttpResponseHttpResponseNotFound)ContextEngineTemplateDoesNotExist)pprint)resolve)timezone)MultiValueDict)	force_str)import_string)_lazy_re_compile)get_docs_versionTZi18nzdjango.templatetags.i18n)debug	librariesc               @   s   e Zd ZdS )ExceptionCycleWarningN)__name__
__module____qualname__ r   r   P/var/www/tester-filtro-web/env/lib/python3.6/site-packages/django/views/debug.pyr       s   r   c               @   s    e Zd ZdZdd Zdd ZdS )CallableSettingWrapperz
    Object to wrap callable appearing in settings.
    * Not to call in the debug page (#21345).
    * Not to break the debug page if the callable forbidding to set attributes
      (#23070).
    c             C   s
   || _ d S )N)_wrapped)selfZcallable_settingr   r   r   __init__+   s    zCallableSettingWrapper.__init__c             C   s
   t | jS )N)reprr   )r   r   r   r   __repr__.   s    zCallableSettingWrapper.__repr__N)r   r   r   __doc__r   r   r   r   r   r   r   $   s   r     c             C   sL   t | | |||}| jdr2|j }t||ddS |j }t||ddS dS )z
    Create a technical server error response. The last three arguments are
    the values returned from sys.exc_info() and friends.
    z	text/html)statuscontent_typeztext/plain; charset=utf-8N)get_exception_reporter_classZacceptsget_traceback_htmlr   get_traceback_text)requestexc_type	exc_valuetbstatus_codereporterhtmltextr   r   r   technical_500_response2   s    
r/   c               C   s   t tj S )N)r   r   Z!DEFAULT_EXCEPTION_REPORTER_FILTERr   r   r   r   %get_default_exception_reporter_filter@   s    r0   c             C   s   t  }t| d|S )NZexception_reporter_filter)r0   getattr)r'   Zdefault_filterr   r   r   get_exception_reporter_filterF   s    r2   c             C   s   t tj}t| d|S )NZexception_reporter_class)r   r   ZDEFAULT_EXCEPTION_REPORTERr1   )r'   Z default_exception_reporter_classr   r   r   r$   K   s    
r$   c               @   sb   e Zd ZdZdZedej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dS )SafeExceptionReporterFilterz
    Use annotations made by the sensitive_post_parameters and
    sensitive_variables decorators to filter out sensitive information.
    z********************z#API|TOKEN|KEY|SECRET|PASS|SIGNATURE)flagsc                s   y j j|}W n tk
r(   d}Y nX |r6 j}nft|trX fdd|j D }nDt|trv fdd|D }n&t|trt fdd|D }n|}t	|rt
|}|S )z
        Cleanse an individual setting key/value of sensitive content. If the
        value is a dictionary, recursively cleanse the keys in that dictionary.
        Fc                s   i | ]\}} j |||qS r   )cleanse_setting).0kv)r   r   r   
<dictcomp>e   s    z?SafeExceptionReporterFilter.cleanse_setting.<locals>.<dictcomp>c                s   g | ]} j d |qS ) )r5   )r6   r8   )r   r   r   
<listcomp>g   s    z?SafeExceptionReporterFilter.cleanse_setting.<locals>.<listcomp>c                s   g | ]} j d |qS )r:   )r5   )r6   r8   )r   r   r   r;   i   s    )hidden_settingssearch	TypeErrorcleansed_substitute
isinstancedictitemslisttuplecallabler   )r   keyvalueZis_sensitivecleansedr   )r   r   r5   X   s     



z+SafeExceptionReporterFilter.cleanse_settingc             C   s8   i }x.t tD ]"}|j r| j|tt|||< qW |S )z
        Return a dictionary of the settings module with values of sensitive
        settings replaced with stars (*********).
        )dirr   isupperr5   r1   )r   Zsettings_dictr7   r   r   r   get_safe_settingsr   s
    z-SafeExceptionReporterFilter.get_safe_settingsc                s&   t |dsi S  fdd|jj D S )zU
        Return a dictionary of request.META with sensitive values redacted.
        METAc                s   i | ]\}} j |||qS r   )r5   )r6   r7   r8   )r   r   r   r9      s    zESafeExceptionReporterFilter.get_safe_request_meta.<locals>.<dictcomp>)hasattrrL   rB   )r   r'   r   )r   r   get_safe_request_meta}   s    
z1SafeExceptionReporterFilter.get_safe_request_metac             C   s
   t jdkS )a  
        This filter is to add safety in production environments (i.e. DEBUG
        is False). If DEBUG is True then your site is not safe anyway.
        This hook is provided as a convenience to easily activate or
        deactivate the filter on a per request basis.
        F)r   DEBUG)r   r'   r   r   r   	is_active   s    z%SafeExceptionReporterFilter.is_activec             C   sF   t |dg }| j|rB|rB|j }x|D ]}||kr(| j||< q(W |S )z
        Replace the keys in a MultiValueDict marked as sensitive with stars.
        This mitigates leaking sensitive POST parameters if something like
        request.POST['nonexistent_key'] throws an exception (#21098).
        sensitive_post_parameters)r1   rP   copyr?   )r   r'   ZmultivaluedictrQ   paramr   r   r   get_cleansed_multivaluedict   s    
z7SafeExceptionReporterFilter.get_cleansed_multivaluedictc             C   s   |dkri S t |dg }| j|rz|rz|jj }|dkrTx|D ]}| j||< q>W |S x|D ]}||krZ| j||< qZW |S n|jS dS )zk
        Replace the values of POST parameters marked as sensitive with
        stars (*********).
        NrQ   __ALL__)r1   rP   POSTrR   r?   )r   r'   rQ   rH   r7   rS   r   r   r   get_post_parameters   s    


z/SafeExceptionReporterFilter.get_post_parametersc             C   sL   yt |t}W n( tk
r6 } zdj||S d }~X nX |rH| j||}|S )Nz{!r} while evaluating {!r})r@   r   	ExceptionformatrT   )r   r'   rG   Zis_multivalue_dicter   r   r   cleanse_special_types   s    z1SafeExceptionReporterFilter.cleanse_special_typesc       	      C   s  |j }d}x@|dk	rJ|jjdkrBd|jkrB|jd }t|dd}P |j }qW i }| j|r|r|dkrxV|jD ]}| j||< qnW qxd|jj D ],\}}||kr| j}n| j||}|||< qW n(x&|jj D ]\}}| j||||< qW |jjdkod|jkr| j|d< | j|d< |j S )ze
        Replace the values of variables marked as sensitive with
        stars (*********).
        NZsensitive_variables_wrappersensitive_variablesrU   	func_argsZfunc_kwargs)	f_backf_codeco_namef_localsr1   rP   r?   rB   r[   )	r   r'   tb_framecurrent_framer\   wrapperrH   namerG   r   r   r   get_traceback_frame_variables   s4    





z9SafeExceptionReporterFilter.get_traceback_frame_variablesN)r   r   r   r    r?   r   reIr<   r5   rK   rN   rP   rT   rW   r[   rf   r   r   r   r   r3   P   s   	r3   c               @   st   e Zd ZdZedd Zedd ZdddZd	d
 Zdd Z	dd Z
dd ZdddZdd Zdd Zdd ZdS )ExceptionReporterz0Organize and coordinate reporting on exceptions.c             C   s   t d d S )N	templatesztechnical_500.html)CURRENT_DIR)r   r   r   r   html_template_path   s    z$ExceptionReporter.html_template_pathc             C   s   t d d S )Nrj   ztechnical_500.txt)rk   )r   r   r   r   text_template_path   s    z$ExceptionReporter.text_template_pathFc             C   sJ   || _ t| j | _|| _|| _|| _|| _t| jdd | _d| _	d | _
d S )NZtemplate_debugF)r'   r2   filterr(   r)   r*   is_emailr1   template_infotemplate_does_not_exist
postmortem)r   r'   r(   r)   r*   ro   r   r   r   r     s    zExceptionReporter.__init__c             C   sd  | j r*t| j tr*d| _| jjp&| jg| _| j }xt|D ]t\}}d|krg }xN|d D ]B\}}t	|}t
|dkrd|dd t
|f }|j||f qZW ||d< |||< q<W d}| j ot| j tr4t| jdd}t| jd	d}	|dk	o|	dk	r4| jjd
 }
t|
t|d dt|	d t
|
 ddd}ddlm} | jdkrRd}n,yt| jj}W n tk
r|   d}Y nX | j||| j| jj| j|t| jj| jj | jj tj dtj!dd  t"j# | tj$| j%| j| jd}| jdk	r$| jj&j |d< | jj'j |d< | jj(j |d< | j r8| j j)|d< | jrNt| j|d< |r`|d |d< |S )z5Return a dictionary containing traceback information.Tvarsi   u   %s… <trimmed %d bytes string>r   r:   startNend      asciireplace)errors)get_versionz%[unable to retrieve the current user]z%d.%d.%d   )ro   unicode_hintframesr'   Zrequest_metauser_strZfiltered_POST_itemsr   Zsys_executableZsys_version_infoZserver_timeZdjango_version_infosys_pathrp   rq   rr   Zrequest_GET_itemsZrequest_FILES_itemsZrequest_COOKIES_itemsZexception_typeZexception_valueZ	lastframe)*r(   
issubclassr	   rq   r)   chainrr   get_traceback_frames	enumerater
   lenappendUnicodeErrorr1   argsr   maxmindjangor{   r'   struserrX   ro   rn   rN   rC   rW   rB   rK   sys
executableversion_infor   nowpathrp   GETZFILESZCOOKIESr   )r   r~   iframeZ
frame_varsr7   r8   r}   rt   ru   Zunicode_strr{   r   cr   r   r   get_traceback_data  sr    "


z$ExceptionReporter.get_traceback_datac             C   sB   | j jdd}tj|j }W dQ R X t| j dd}|j|S )z1Return HTML version of debug 500 HTTP error page.zutf-8)encodingNF)use_l10n)rl   openDEBUG_ENGINEfrom_stringreadr   r   render)r   fhtr   r   r   r   r%   U  s    z$ExceptionReporter.get_traceback_htmlc             C   sD   | j jdd}tj|j }W dQ R X t| j ddd}|j|S )z7Return plain text version of debug 500 HTTP error page.zutf-8)r   NF)Z
autoescaper   )rm   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r&   \  s    z$ExceptionReporter.get_traceback_textc             C   s   d }t |drBy|j|}W n tk
r0   Y nX |d k	rB|j }|d kry&t|d}|j j }W d Q R X W n tk
r   Y nX |S )N
get_sourcerb)rM   r   ImportError
splitlinesr   r   OSError)r   filenameloadermodule_namesourcefpr   r   r   _get_sourcec  s    
zExceptionReporter._get_sourceNc                s   | j |||}|dkr"dg dg fS t|d tr|d x4|dd D ]$}tjd|}|rB|d jd P qBW  fdd|D }td|| }	|| }
y(||	| }|| }||d |
 }W n tk
r   dg dg fS X |	|||fS )	z
        Return context_lines before and after lineno from file.
        Return (pre_context_lineno, pre_context, context_line, post_context).
        Nr   rx      s   coding[:=]\s*([-\w.]+)rv   c                s   g | ]}t | d qS )ry   )r   )r6   sline)r   r   r   r;     s    z:ExceptionReporter._get_lines_from_file.<locals>.<listcomp>)r   r@   bytesrg   r=   decoder   
IndexError)r   r   linenoZcontext_linesr   r   r   linematchZlower_boundZupper_boundpre_contextcontext_linepost_contextr   )r   r   _get_lines_from_filet  s(    z&ExceptionReporter._get_lines_from_filec             C   s4   t |dd }t |dd }t |dd }|p2|r0d S |S )N	__cause____suppress_context____context__)r1   )r   r)   explicitZsuppress_contextZimplicitr   r   r   _get_explicit_or_implicit_cause  s    z1ExceptionReporter._get_explicit_or_implicit_causec             C   s   g }| j }x6|r@|j| | j|}||krtjd| t P qW g }|sN|S |j }|s`| jn|j}x@|j	| j
|| y|j }W n tk
r   P Y nX |j}qhW |S )NzHCycle in the exception chain detected: exception '%s' encountered again.)r)   r   r   warningswarnr   popr*   __traceback__extendget_exception_traceback_framesr   )r   
exceptionsr)   r~   r*   r   r   r   r     s0    


z&ExceptionReporter.get_traceback_framesc             c   s  | j |}t|dd}|d kr.||d ddV  x|d k	r|jjjdrP|j}q0|jjj}|jjj}|j	d }|jj
jd}|jj
jdpd	}	| j||d
||	\}
}}}|
d kr|}
g }d}g }||||	jdrdnd|||d | jj| j|jt|||||
d dV  |j}q0W d S )Nr   Tr   )	exc_causeexc_cause_explicitr*   typeZ__traceback_hide__rv   
__loader__r   r:      z<source code not available>zdjango.r   )r   r   r*   r   r   functionr   rs   idr   r   r   pre_context_lineno)r   r1   rb   ra   gettb_nextr_   co_filenamer`   	tb_lineno	f_globalsr   
startswithrn   rf   r'   r   )r   r)   r*   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r     sJ    




z0ExceptionReporter.get_exception_traceback_frames)F)NN)r   r   r   r    propertyrl   rm   r   r   r%   r&   r   r   r   r   r   r   r   r   r   ri      s   
H
"!ri   c          *   C   s  y|j d d }W n( tttfk
r:   | jdd }Y nX y|j d d }W n0 tttfk
r~   d}| jrv| jjnd}Y nrX d}| s| jdkrt|dkrt|d dkrt	|d d d	d
t	|d d dd
  kodkn  rt
| S t	| dtj}t|tjr|j}d
}yt| j}W n tk
r:   Y n\X |j}t|drV|j}n"t|drxt|jdrx|jj}t|dr|j}	d|	|f }ttddjdd}
tj|
j }W dQ R X t }t|tj|||t|| |j  |d	}t!|j"|ddS )zBCreate a technical 404 error response. `exception` is the Http404.r   r   rv   NtriedTF/Zapp_namer:   	namespaceZadminurlconfr   	__class__r   z%s.%srj   ztechnical_404.htmlzutf-8)r   )	r   Zroot_urlconfrequest_pathZurlpatternsresolvedreasonr'   r   Zraising_view_namez	text/html)r#   )#r   r   r>   KeyErrorZ	path_inforesolver_matchr   r   r   r1   default_urlconfr   ZROOT_URLCONFr@   types
ModuleTyper   r   r   funcrM   r   r   r   rk   r   r   r   r   r0   r   r   rK   r   r   )r'   	exceptionZ	error_urlr   r   r   Zcallerr   objmoduler   r   Zreporter_filterr   r   r   r   technical_404_response  s\    
8
r   c             C   sN   t tddjdd}tj|j }W dQ R X tdt i}t|j	|ddS )	z+Create an empty URLconf 404 error response.rj   zdefault_urlconf.htmlzutf-8)r   Nversionz	text/html)r#   )
r   rk   r   r   r   r   r   r   r   r   )r'   r   r   r   r   r   r   r   (  s
    r   )r!   )1	functoolsrg   r   r   r   pathlibr   Zdjango.confr   Zdjango.httpr   r   r   Zdjango.templater   r   r	   Zdjango.template.defaultfiltersr
   Zdjango.urlsr   Zdjango.utilsr   Zdjango.utils.datastructuresr   Zdjango.utils.encodingr   Zdjango.utils.module_loadingr   Zdjango.utils.regex_helperr   Zdjango.utils.versionr   r   __file__parentrk   UserWarningr   r   r/   	lru_cacher0   r2   r$   r3   ri   r   r   r   r   r   r   <module>   s@   

 ' w<