U
    K¼|ea  ã                   @   sj  d dl Z 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!m"Z" edddid�Z#dd„ 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,d!d"„ Z-G d#d$„ d$ƒZ.G d%d&„ d&ƒZ/d'd(„ Z0d)d*„ Z1dS ),é    N)ÚPath)Úsettings)ÚHttp404ÚHttpResponseÚHttpResponseNotFound)ÚContextÚEngineÚTemplateDoesNotExist)Úpprint)Úresolve)Útimezone)ÚMultiValueDict)Ú	force_str)Úimport_string)Ú_lazy_re_compile)ÚPY311Úget_docs_versionTZi18nzdjango.templatetags.i18n)ÚdebugÚ	librariesc                 C   s   t tƒjd |  S )z½
    Return a path to a builtin template.

    Avoid calling this function at the module level or in a class-definition
    because __file__ may not exist, e.g. in frozen environments.
    Ú	templates)r   Ú__file__Úparent)Úname© r   úO/var/www/website-v5/atlas_env/lib/python3.8/site-packages/django/views/debug.pyÚbuiltin_template_path   s    r   c                   @   s   e Zd ZdS )ÚExceptionCycleWarningN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   )   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__5   s    zCallableSettingWrapper.__init__c                 C   s
   t | jƒS r!   )Úreprr"   ©r#   r   r   r   Ú__repr__8   s    zCallableSettingWrapper.__repr__N)r   r   r   Ú__doc__r$   r'   r   r   r   r   r    -   s   r    éô  c                 C   sJ   t | ƒ| |||ƒ}|  d¡r0| ¡ }t||d�S | ¡ }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)Ústatusztext/plain; charset=utf-8)r*   Úcontent_typeN)Úget_exception_reporter_classÚacceptsÚget_traceback_htmlr   Úget_traceback_text)ÚrequestÚexc_typeÚ	exc_valueÚtbÚstatus_codeÚreporterÚhtmlÚtextr   r   r   Útechnical_500_response<   s    
  ÿr8   c                   C   s   t tjƒƒ S r!   )r   r   Ú!DEFAULT_EXCEPTION_REPORTER_FILTERr   r   r   r   Ú%get_default_exception_reporter_filterL   s    r:   c                 C   s   t ƒ }t| d|ƒS )NZexception_reporter_filter)r:   Úgetattr)r0   Zdefault_filterr   r   r   Úget_exception_reporter_filterR   s    r<   c                 C   s   t tjƒ}t| d|ƒS )NZexception_reporter_class)r   r   ÚDEFAULT_EXCEPTION_REPORTERr;   )r0   Z default_exception_reporter_classr   r   r   r,   W   s    ÿ  ÿr,   c                 C   sD   | j }|d kr2zt| jƒ}W n tk
r0   Y nX |d kr>dS |jS )NÚ )Úresolver_matchr   Úpathr   Ú
_func_path)r0   r?   r   r   r   Ú
get_caller`   s    rB   c                   @   sj   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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|HTTP_COOKIE)Úflagsc                    sÀ   |t jkrd}n*zˆ j |¡}W n tk
r8   d}Y nX |rFˆ j}nft|tƒrh‡ fdd„| ¡ D ƒ}nDt|t	ƒr†‡ fdd„|D ƒ}n&t|t
ƒr¨t
‡ fdd„|D ƒƒ}n|}t|ƒr¼t|ƒ}|S )z¦
        Cleanse an individual setting key/value of sensitive content. If the
        value is a dictionary, recursively cleanse the keys in that dictionary.
        TFc                    s   i | ]\}}|ˆ   ||¡“qS r   ©Úcleanse_setting©Ú.0ÚkÚvr&   r   r   Ú
<dictcomp>…   s      z?SafeExceptionReporterFilter.cleanse_setting.<locals>.<dictcomp>c                    s   g | ]}ˆ   d |¡‘qS ©r>   rE   ©rH   rJ   r&   r   r   Ú
<listcomp>‡   s     z?SafeExceptionReporterFilter.cleanse_setting.<locals>.<listcomp>c                    s   g | ]}ˆ   d |¡‘qS rL   rE   rM   r&   r   r   rN   ‰   s     )r   ÚSESSION_COOKIE_NAMEÚhidden_settingsÚsearchÚ	TypeErrorÚcleansed_substituteÚ
isinstanceÚdictÚitemsÚlistÚtupleÚcallabler    )r#   ÚkeyÚvalueZis_sensitiveÚcleansedr   r&   r   rF   u   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   ÚisupperrF   r;   )r#   Úsettings_dictrI   r   r   r   Úget_safe_settings’   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                    s   i | ]\}}|ˆ   ||¡“qS r   rE   rG   r&   r   r   rK   £   s      zESafeExceptionReporterFilter.get_safe_request_meta.<locals>.<dictcomp>)Úhasattrra   rV   ©r#   r0   r   r&   r   Úget_safe_request_meta�   s    
z1SafeExceptionReporterFilter.get_safe_request_metac                    s&   t |dƒsi S ‡ fdd„|j ¡ D ƒS )zX
        Return a dictionary of request.COOKIES with sensitive values redacted.
        ÚCOOKIESc                    s   i | ]\}}|ˆ   ||¡“qS r   rE   rG   r&   r   r   rK   «   s      z@SafeExceptionReporterFilter.get_safe_cookies.<locals>.<dictcomp>)rb   re   rV   rc   r   r&   r   Úget_safe_cookies¥   s    
z,SafeExceptionReporterFilter.get_safe_cookiesc                 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   ÚDEBUGrc   r   r   r   Ú	is_active­   s    z%SafeExceptionReporterFilter.is_activec                 C   sB   t |dg ƒ}|  |¡r>|r>| ¡ }|D ]}||k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)r;   rh   ÚcopyrS   )r#   r0   Zmultivaluedictri   Úparamr   r   r   Úget_cleansed_multivaluedict¶   s    z7SafeExceptionReporterFilter.get_cleansed_multivaluedictc                 C   s|   |dkri S t |dg ƒ}|  |¡rr|rr|j ¡ }|dkrP|D ]}| j||< q<|S |D ]}||krT| j||< qT|S n|jS dS )zk
        Replace the values of POST parameters marked as sensitive with
        stars (*********).
        Nri   Ú__ALL__)r;   rh   ÚPOSTrj   rS   )r#   r0   ri   r\   rI   rk   r   r   r   Úget_post_parametersÄ   s$      ÿ
z/SafeExceptionReporterFilter.get_post_parametersc              
   C   sV   zt |tƒ}W n2 tk
r@ } zd ||¡ W Y ¢S d }~X Y nX |rR|  ||¡}|S )Nz{!r} while evaluating {!r})rT   r   Ú	ExceptionÚformatrl   )r#   r0   r[   Zis_multivalue_dictÚer   r   r   Úcleanse_special_typesß   s    "z1SafeExceptionReporterFilter.cleanse_special_typesc           	      C   s  |j }d}|dk	rH|jjdkr@d|jkr@|jd }t|ddƒ}qH|j }q
i }|  |¡r´|r´|dkrz|jD ]}| j||< qhqØ|j ¡ D ],\}}||krœ| j}n|  ||¡}|||< q„n$|j ¡ D ]\}}|  ||¡||< q¾|jjdk�rd|jk�r| j|d< | j|d< | ¡ S )ze
        Replace the values of variables marked as sensitive with
        stars (*********).
        NZsensitive_variables_wrapperÚsensitive_variablesrm   Z	func_argsZfunc_kwargs)	Úf_backÚf_codeÚco_nameÚf_localsr;   rh   rS   rV   rs   )	r#   r0   Útb_frameÚcurrent_framert   Úwrapperr\   r   r[   r   r   r   Úget_traceback_frame_variablesí   s<    
ÿþ


ÿþ

z9SafeExceptionReporterFilter.get_traceback_frame_variablesN)r   r   r   r(   rS   r   ÚreÚIrP   rF   r`   rd   rf   rh   rl   ro   rs   r|   r   r   r   r   rC   j   s    ÿ	rC   c                   @   s|   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„ 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ƒS )Nztechnical_500.html©r   r&   r   r   r   Úhtml_template_path'  s    z$ExceptionReporter.html_template_pathc                 C   s   t dƒS )Nztechnical_500.txtr€   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 )NÚtemplate_debugF)r0   r<   Úfilterr1   r2   r3   Úis_emailr;   Útemplate_infoÚtemplate_does_not_existÚ
postmortem)r#   r0   r1   r2   r3   r…   r   r   r   r$   /  s    zExceptionReporter.__init__c                 C   s    dj | jj| j ¡ | j ¡ d�S )z•
        Return an absolute URI from variables available in this request. Skip
        allowed hosts protection, so may return insecure URI.
        z{scheme}://{host}{path})ÚschemeÚhostr@   )rq   r0   r‰   Ú_get_raw_hostÚget_full_pathr&   r   r   r   Ú_get_raw_insecure_uri;  s
    ýz'ExceptionReporter._get_raw_insecure_uric                 C   s   | j r*t| j tƒr*d| _| jjp&| jg| _|  ¡ }t|ƒD ]p\}}d|kr¢g }|d D ]B\}}t	|ƒ}t
|ƒdkrŠd|dd… t
|ƒf }| ||f¡ qV||d< |||< q:d}| j �r0t| j tƒ�r0t| jddƒ}t| jd	dƒ}	|dk	�r0|	dk	�r0| jjd
 }
t|
t|d dƒt|	d t
|
ƒƒ… ddd�}ddlm} | jdk�rNd}n,zt| jjƒ}W n tk
�rx   d}Y nX | j||| j| j | j¡| j | j¡ ¡ |t| j | j¡ ¡ ƒ| j ¡ t j!dt j"dd…  t# $¡ |ƒ t j%| j&| j| jdœ}| jdk	�r:| jj' ¡ |d< | jj( ¡ |d< |  )¡ |d< t*| jƒ|d< | j �rN| j j+|d< | j�rŠt| jƒ|d< t| jddƒ }�rŠdd ,|¡ |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é   )r…   Úunicode_hintÚframesr0   Zrequest_metaZrequest_COOKIES_itemsÚuser_strZfiltered_POST_itemsr   Zsys_executableZsys_version_infoÚserver_timeZdjango_version_infoZsys_pathr†   r‡   rˆ   Zrequest_GET_itemsZrequest_FILES_itemsZrequest_insecure_uriÚraising_view_nameZexception_typeZexception_valueZ	__notes__Ú
Zexception_noteséÿÿÿÿZ	lastframe)-r1   Ú
issubclassr	   r‡   r2   Úchainrˆ   Úget_traceback_framesÚ	enumerater
   ÚlenÚappendÚUnicodeErrorr;   Úargsr   ÚmaxÚminÚdjangor–   r0   ÚstrÚuserrp   r…   r„   rd   rf   rV   rW   ro   r`   ÚsysÚ
executableÚversion_infor   Únowr@   r†   ÚGETÚFILESr�   rB   r   Újoin)r#   r™   ÚiÚframeZ
frame_varsrI   rJ   r˜   r�   r�   Zunicode_strr–   rš   ÚcZ	exc_notesr   r   r   Úget_traceback_dataF  s„    
"ý
ÿíz$ExceptionReporter.get_traceback_datac              	   C   sB   | j jdd��}t | ¡ ¡}W 5 Q R X t|  ¡ dd�}| |¡S )z1Return HTML version of debug 500 HTTP error page.úutf-8©ÚencodingF)Úuse_l10n)r�   ÚopenÚDEBUG_ENGINEÚfrom_stringÚreadr   r¶   Úrender©r#   ÚfhÚtrµ   r   r   r   r.   –  s    z$ExceptionReporter.get_traceback_htmlc              	   C   sD   | j jdd��}t | ¡ ¡}W 5 Q R X t|  ¡ ddd�}| |¡S )z7Return plain text version of debug 500 HTTP error page.r·   r¸   F)Ú
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ƒrBz| |¡}W n tk
r0   Y nX |d k	rB| ¡ }|d kr†z&t|dƒ�}| ¡  ¡ }W 5 Q R X W n tk
r„   Y nX |S )NÚ
get_sourceÚrb)rb   rÄ   ÚImportErrorÚ
splitlinesr»   r¾   ÚOSError)r#   ÚfilenameÚloaderÚmodule_nameÚsourceÚfpr   r   r   Ú_get_source¤  s    
zExceptionReporter._get_sourceNc                    sæ   |   |||¡}|dkr"dg dg fS t|d tƒrzd‰ |dd… D ]&}t d|¡}|r@|d  d¡‰  qhq@‡ fdd„|D ƒ}td|| ƒ}	|| }
z(||	|… }|| }||d |
… }W n  tk
rØ   dg dg f Y 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   r“   é   s   coding[:=]\s*([-\w.]+)r‘   c                    s   g | ]}t |ˆ d ƒ‘qS )r”   )rª   )rH   Úsliner¸   r   r   rN   Ì  s     z:ExceptionReporter._get_lines_from_file.<locals>.<listcomp>)rÎ   rT   Úbytesr}   rQ   Údecoder§   Ú
IndexError)r#   rÉ   ÚlinenoÚcontext_linesrÊ   rË   rÌ   ÚlineÚmatchZlower_boundZupper_boundÚpre_contextÚcontext_lineÚpost_contextr   r¸   r   Ú_get_lines_from_fileµ  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__)r;   )r#   r2   ZexplicitZsuppress_contextZimplicitr   r   r   Ú_get_explicit_or_implicit_causeÙ  s    z1ExceptionReporter._get_explicit_or_implicit_causec                 C   s¦   g }| j }|r>| |¡ |  |¡}||kr
t d| t¡ q>q
g }|sJ|S | ¡ }|s\| jn|j}| 	|  
||¡¡ z| ¡ }W n tk
r˜   Y q¢Y nX |j}qb|S )NzHCycle in the exception chain detected: exception '%s' encountered again.)r2   r¤   rß   ÚwarningsÚwarnr   Úpopr3   Ú__traceback__ÚextendÚget_exception_traceback_framesrÓ   )r#   Ú
exceptionsr2   r™   r3   r   r   r   r¡   ß  s2    

ÿý
z&ExceptionReporter.get_traceback_framesc                 c   sÀ  |   |¡}t|ddƒ}|d kr.||d ddœV  |d k	�r¼|jj d¡rN|j}q.|jjj}|jjj}|j	d }|jj
 d¡}|jj
 d¡pŠd	}	|  ||d
||	¡\}
}}}|
d kr¾|}
g }d}g }d	 }}t�rdtt |jj ¡ |jd d ¡ƒ\}}}}|�rd|�rdd||  }d|tt|d ƒƒ d  }d|› |› �}dd| t|ƒt| ¡ ƒ   }d|› |› �}||||	 d¡�rzdnd|||d | j | j|j¡t|ƒ||||
d ||dœV  |j}q.d S )NrÜ   Tr«   )Ú	exc_causeÚexc_cause_explicitr3   ÚtypeÚ__traceback_hide__r‘   Ú
__loader__r   r>   é   z<source code not available>rÏ   ú^ú r�   é   zdjango.r©   )rç   rè   r3   ré   rÉ   ÚfunctionrÔ   rŽ   ÚidrØ   rÙ   rÚ   Úpre_context_linenoÚcolnoÚtb_area_colno)rß   r;   ry   rx   ÚgetÚtb_nextrv   Úco_filenamerw   Ú	tb_linenoÚ	f_globalsrÛ   r   ÚnextÚ	itertoolsÚisliceZco_positionsÚtb_lastir£   rª   ÚlstripÚ
startswithr„   r|   r0   rñ   )r#   r2   r3   rç   rè   rÉ   rð   rÔ   rÊ   rË   rò   rØ   rÙ   rÚ   ró   rô   Ú_Zstart_columnZ
end_columnZ	underlineZspacesZtb_area_spacesr   r   r   rå      s–    
ü



ûû
  ÿÿÿþÿ ÿïz0ExceptionReporter.get_exception_traceback_frames)F)NN)r   r   r   r(   Úpropertyr�   r‚   r$   r�   r¶   r.   r/   rÎ   rÛ   rß   r¡   rå   r   r   r   r   r   $  s"   


P   ÿ
$!r   c           
      C   sx  z|j d d }W n( tttfk
r:   | jdd… }Y nX z|j d d }W n0 tttfk
r~   d}| jrv| jjnd}Y npX d}|ræ| jdkrît|ƒdkrît|d ƒdkrît	|d d d	d
ƒt	|d d dd
ƒ  krâdkrîn nt
| ƒS t	| dtjƒ}t|tjƒ�r|j}tdƒjdd��}t | ¡ ¡}W 5 Q R X tƒ }t|tj|||t|ƒ| | ¡ t| ƒdœ	ƒ}	t| |	¡ƒS )zBCreate a technical 404 error response. `exception` is the Http404.r   r@   r‘   NÚtriedTFú/Úapp_namer>   Ú	namespaceÚadminÚurlconfztechnical_404.htmlr·   r¸   )	r  Zroot_urlconfZrequest_pathÚurlpatternsÚresolvedÚreasonr0   r   rœ   )r¦   rÓ   rR   ÚKeyErrorÚ	path_infor?   r  r@   r£   r;   Údefault_urlconfr   ÚROOT_URLCONFrT   ÚtypesÚ
ModuleTyper   r   r»   r¼   r½   r¾   r:   r   rª   r`   rB   r   r¿   )
r0   Ú	exceptionZ	error_urlr  r	  r  rÁ   rÂ   Zreporter_filterrµ   r   r   r   Útechnical_404_responseN  sT    ÿ
þýûú
÷ÿr  c              	   C   sF   t dƒjdd��}t | ¡ ¡}W 5 Q R X tdtƒ iƒ}t| |¡ƒS )z+Create an empty URLconf 404 error response.zdefault_urlconf.htmlr·   r¸   Úversion)	r   r»   r¼   r½   r¾   r   r   r   r¿   )r0   rÁ   rÂ   rµ   r   r   r   r  }  s     ÿÿr  )r)   )2Ú	functoolsrû   r}   r¬   r  rà   Úpathlibr   Údjango.confr   Údjango.httpr   r   r   Údjango.templater   r   r	   Zdjango.template.defaultfiltersr
   Údjango.urlsr   Údjango.utilsr   Údjango.utils.datastructuresr   Údjango.utils.encodingr   Údjango.utils.module_loadingr   Údjango.utils.regex_helperr   Údjango.utils.versionr   r   r¼   r   ÚUserWarningr   r    r8   Ú	lru_cacher:   r<   r,   rB   rC   r   r  r  r   r   r   r   Ú<module>   sJ   þ


	
 ;  ,/