o
    hW                     @   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 ) 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)debugZ	librariesc                   @   s   e Zd ZdS )ExceptionCycleWarningN)__name__
__module____qualname__ r   r   Z/var/www/html/magazine_api/magazine_env/lib/python3.10/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__+      
zCallableSettingWrapper.__init__c                 C   s
   t | jS r   )reprr   r   r   r   r   __repr__.   r   zCallableSettingWrapper.__repr__N)r   r   r   __doc__r   r!   r   r   r   r   r   $   s    r     c                 C   sH   t | | |||}| dr| }t||ddS | }t||ddS )z
    Create a technical server error response. The last three arguments are
    the values returned from sys.exc_info() and friends.
    	text/html)statuscontent_typeztext/plain; charset=utf-8)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   
r2   c                   C   s   t tj S r   )r   r   Z!DEFAULT_EXCEPTION_REPORTER_FILTERr   r   r   r   %get_default_exception_reporter_filter@   s   r3   c                 C   s   t  }t| d|S )NZexception_reporter_filter)r3   getattr)r*   Zdefault_filterr   r   r   get_exception_reporter_filterF   s   r5   c                 C   s   t tj}t| d|S )NZexception_reporter_class)r   r   ZDEFAULT_EXCEPTION_REPORTERr4   )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   z j |}W n ty   d}Y nw |r j}n3t|tr+ fdd| D }n"t|tr: fdd|D }nt|trKt fdd|D }n|}t	|rUt
|}|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                       i | ]\}}|  ||qS r   cleanse_setting.0kvr    r   r   
<dictcomp>e       z?SafeExceptionReporterFilter.cleanse_setting.<locals>.<dictcomp>c                       g | ]}  d |qS  r9   r<   r>   r    r   r   
<listcomp>g       z?SafeExceptionReporterFilter.cleanse_setting.<locals>.<listcomp>c                    rA   rB   r9   rD   r    r   r   rE   i   rF   )hidden_settingssearch	TypeErrorcleansed_substitute
isinstancedictitemslisttuplecallabler   )r   keyvalueZis_sensitivecleansedr   r    r   r:   X   s"   


z+SafeExceptionReporterFilter.cleanse_settingc                 C   s4   i }t tD ]}| r| |tt|||< q|S )z
        Return a dictionary of the settings module with values of sensitive
        settings replaced with stars (*********).
        )dirr   isupperr:   r4   )r   Zsettings_dictr=   r   r   r   get_safe_settingsr   s   z-SafeExceptionReporterFilter.get_safe_settingsc                    s&   t |dsi S  fdd|j D S )zU
        Return a dictionary of request.META with sensitive values redacted.
        METAc                    r8   r   r9   r;   r    r   r   r?      r@   zESafeExceptionReporterFilter.get_safe_request_meta.<locals>.<dictcomp>)hasattrrW   rM   r   r*   r   r    r   get_safe_request_meta}   s   
z1SafeExceptionReporterFilter.get_safe_request_metac                 C   s
   t jdu 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   DEBUGrY   r   r   r   	is_active   s   
z%SafeExceptionReporterFilter.is_activec                 C   sB   t |dg }| |r|r| }|D ]}||v r| j||< q|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)r4   r\   copyrJ   )r   r*   Zmultivaluedictr]   paramr   r   r   get_cleansed_multivaluedict   s   
z7SafeExceptionReporterFilter.get_cleansed_multivaluedictc                 C   sv   |du ri S t |dg }| |r8|r8|j }|dkr(|D ]}| j||< q|S |D ]}||v r5| j||< q*|S |jS )zk
        Replace the values of POST parameters marked as sensitive with
        stars (*********).
        Nr]   __ALL__)r4   r\   POSTr^   rJ   )r   r*   r]   rS   r=   r_   r   r   r   get_post_parameters   s   

z/SafeExceptionReporterFilter.get_post_parametersc              
   C   sT   zt |t}W n ty } zd||W  Y d }~S d }~ww |r(| ||}|S )Nz{!r} while evaluating {!r})rK   r   	Exceptionformatr`   )r   r*   rR   Zis_multivalue_dicter   r   r   cleanse_special_types   s   z1SafeExceptionReporterFilter.cleanse_special_typesc           	      C   s  |j }d}|dur'|jjdkr d|jv r |jd }t|dd}n|j }|dus	i }| |r]|r]|dkr@|jD ]}| j||< q7n/|j D ]\}}||v rQ| j}n| ||}|||< qEn|j D ]\}}| ||||< qb|jjdkrd|jv r| j|d< | j|d< | S )ze
        Replace the values of variables marked as sensitive with
        stars (*********).
        NZsensitive_variables_wrappersensitive_variablesra   Z	func_argsZfunc_kwargs)	f_backf_codeco_namef_localsr4   r\   rJ   rM   rg   )	r   r*   tb_framecurrent_framerh   wrapperrS   namerR   r   r   r   get_traceback_frame_variables   s:   




	


z9SafeExceptionReporterFilter.get_traceback_frame_variablesN)r   r   r   r"   rJ   r   reIrG   r:   rV   rZ   r\   r`   rc   rg   rq   r   r   r   r   r6   P   s    	r6   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      t d d S )N	templatesztechnical_500.htmlCURRENT_DIRr    r   r   r   html_template_path      z$ExceptionReporter.html_template_pathc                 C   ru   )Nrv   ztechnical_500.txtrw   r    r   r   r   text_template_path   rz   z$ExceptionReporter.text_template_pathFc                 C   sJ   || _ t| j | _|| _|| _|| _|| _t| jdd | _d| _	d | _
d S )NZtemplate_debugF)r*   r5   filterr+   r,   r-   is_emailr4   template_infotemplate_does_not_exist
postmortem)r   r*   r+   r,   r-   r}   r   r   r   r     s   
zExceptionReporter.__init__c                 C   s  | j rt| j trd| _| jjp| jg| _|  }t|D ]8\}}d|v rQg }|d D ]!\}}t	|}t
|dkrEd|dd t
|f }|||f q+||d< |||< qd}| j rt| j trt| jdd}t| jd	d}	|dur|	dur| jjd
 }
t|
t|d dt|	d t
|
 ddd}ddlm} | jdu rd}nzt| jj}W n ty   d}Y nw i d| jd|d|d| jd| j| jd|dt| j| j d| j dtj ddtj!dd  dt"# d| dtj$d | j%d!| jd"| j}| jdur(| jj& |d#< | jj' |d$< | jj( |d%< | j r2| j j)|d&< | jr=t| j|d'< |rF|d( |d)< |S )*z5Return a dictionary containing traceback information.Tvarsi   u   %s… <trimmed %d bytes string>r   rC   startNend      asciireplace)errors)get_versionz%[unable to retrieve the current user]r}   unicode_hintframesr*   Zrequest_metauser_strZfiltered_POST_itemsr   Zsys_executableZsys_version_infoz%d.%d.%d   Zserver_timeZdjango_version_infosys_pathr~   r   r   Zrequest_GET_itemsZrequest_FILES_itemsZrequest_COOKIES_itemsZexception_typeZexception_valueZ	lastframe)*r+   
issubclassr	   r   r,   chainr   get_traceback_frames	enumerater
   lenappendUnicodeErrorr4   argsr   maxmindjangor   r*   struserrd   r}   r|   rZ   rN   rc   rM   rV   sys
executableversion_infor   nowpathr~   GETZFILESZCOOKIESr   )r   r   iframeZ
frame_varsr=   r>   r   r   r   Zunicode_strr   r   cr   r   r   get_traceback_data  s   
"

	
z$ExceptionReporter.get_traceback_datac                 C   sV   | j jdd}t| }W d   n1 sw   Y  t|  dd}||S )z1Return HTML version of debug 500 HTTP error page.utf-8encodingNF)use_l10n)ry   openDEBUG_ENGINEfrom_stringreadr   r   renderr   fhtr   r   r   r   r(   U  s
   
z$ExceptionReporter.get_traceback_htmlc                 C   sX   | j jdd}t| }W d   n1 sw   Y  t|  ddd}||S )z7Return plain text version of debug 500 HTTP error page.r   r   NF)Z
autoescaper   )r{   r   r   r   r   r   r   r   r   r   r   r   r)   \  s
   
z$ExceptionReporter.get_traceback_textc                 C   s   d }t |dr z||}W n	 ty   Y nw |d ur | }|d u rOz t|d}|  }W d    W |S 1 s=w   Y  W |S  tyN   Y |S w |S )N
get_sourcerb)rX   r   ImportError
splitlinesr   r   OSError)r   filenameloadermodule_namesourcefpr   r   r   _get_sourcec  s,   
zExceptionReporter._get_sourceNc                    s   |  |||}|du rdg dg fS t|d tr=d |dd D ]}td|}|r3|d d  nq  fdd|D }td|| }	|| }
z||	| }|| }||d |
 }W n tyk   dg dg f Y S w |	|||fS )	z
        Return context_lines before and after lineno from file.
        Return (pre_context_lineno, pre_context, context_line, post_context).
        Nr   r      s   coding[:=]\s*([-\w.]+)r   c                    s   g | ]}t | d qS )r   )r   )r<   sliner   r   r   rE     rF   z:ExceptionReporter._get_lines_from_file.<locals>.<listcomp>)r   rK   bytesrr   rH   decoder   
IndexError)r   r   linenoZcontext_linesr   r   r   linematchlower_bound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 }|p|rd S |S )N	__cause____suppress_context____context__)r4   )r   r,   ZexplicitZsuppress_contextZimplicitr   r   r   _get_explicit_or_implicit_cause  s   z1ExceptionReporter._get_explicit_or_implicit_causec                 C   s   g }| j }|r || | |}||v rtd| t n|sg }|s&|S | }|s/| jn|j}	 |	| 
|| z| }W n
 tyL   Y |S w |j}q3)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     s:   

z&ExceptionReporter.get_traceback_framesc                 c   s   |  |}t|dd}|d u r||d ddV  |d ur|jjdr'|j}q|jjj}|jjj}|j	d }|jj
d}|jj
dpEd	}	| ||d
||	\}
}}}|
d u r_|}
g }d}g }||||	dridnd|||d | j| j|jt|||||
d dV  |j}|d usd S d S )Nr   Tr   )	exc_causeexc_cause_explicitr-   typeZ__traceback_hide__r   
__loader__r   rC      z<source code not available>zdjango.r   )r   r   r-   r   r   functionr   r   idr   r   r   pre_context_lineno)r   r4   rm   rl   gettb_nextrj   co_filenamerk   	tb_lineno	f_globalsr   
startswithr|   rq   r*   r   )r   r,   r-   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r     sT   




z0ExceptionReporter.get_exception_traceback_frames)F)NN)r   r   r   r"   propertyry   r{   r   r   r(   r)   r   r   r   r   r   r   r   r   r   rt      s    


H
"!rt   c                 C   s  z	|j d d }W n tttfy   | jdd }Y nw z	|j d d }W n tttfy=   d}| jr9| jjnd}Y n;w d}|rt| jdkrxt|dkrxt|d dkrxt	|d d d	d
t	|d d dd
  krrdkrxt
| S  nt
| S t	| dtj}t|tjr|j}d
}zt| j}W n	 ty   Y n*w |j}t|dr|j}nt|drt|jdr|jj}t|dr|j}	d|	|f }ttddjdd}
t|
 }W d   n1 sw   Y  t }t|tj|||t|| |  |d	}t!|"|ddS )zBCreate a technical 404 error response. `exception` is the Http404.r   r   r   NtriedTF/Zapp_namerC   	namespaceZadminurlconfr   	__class__r   z%s.%srv   ztechnical_404.htmlr   r   )	r   Zroot_urlconfrequest_pathZurlpatternsresolvedreasonr*   r   Zraising_view_namer$   r&   )#r   r   rI   KeyErrorZ	path_inforesolver_matchr   r   r   r4   default_urlconfr   ZROOT_URLCONFrK   types
ModuleTyper   r   r   funcrX   r   r   r   rx   r   r   r   r   r3   r   r   rV   r   r   )r*   	exceptionZ	error_urlr   r   r   Zcallerr   objmoduler   r   Zreporter_filterr   r   r   r   technical_404_response  sj   
2

r   c                 C   sb   t tddjdd}t| }W d   n1 sw   Y  tdt i}t|	|ddS )	z+Create an empty URLconf 404 error response.rv   zdefault_urlconf.htmlr   r   Nversionr$   r   )
r   rx   r   r   r   r   r   r   r   r   )r*   r   r   r   r   r   r   r   (  s   r   )r#   )1	functoolsrr   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__parentrx   UserWarningr   r   r2   	lru_cacher3   r5   r'   r6   rt   r   r   r   r   r   r   <module>   sF    


 ' w<