a
    ÛEbQ\  ã                   @   sX  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!dd„ 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(„ Z.d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                 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.
    Z	templates)r   Ú__file__Úparent)Úname© r   úR/home/ja/django-apps/lartica_env/lib/python3.9/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__4   s    zCallableSettingWrapper.__init__c                 C   s
   t | jƒS r   )Úreprr    ©r!   r   r   r   Ú__repr__7   s    zCallableSettingWrapper.__repr__N)r   r   r   Ú__doc__r"   r%   r   r   r   r   r   ,   s   r   éô  c                 C   sL   t | ƒ| |||ƒ}|  d¡r2| ¡ }t||d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.
    ú	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_response;   s    
ÿr6   c                   C   s   t tjƒƒ S r   )r   r   Z!DEFAULT_EXCEPTION_REPORTER_FILTERr   r   r   r   Ú%get_default_exception_reporter_filterK   s    r7   c                 C   s   t ƒ }t| d|ƒS )NZexception_reporter_filter)r7   Úgetattr)r.   Zdefault_filterr   r   r   Úget_exception_reporter_filterQ   s    r9   c                 C   s   t tjƒ}t| d|ƒS )NZexception_reporter_class)r   r   ZDEFAULT_EXCEPTION_REPORTERr8   )r.   Z default_exception_reporter_classr   r   r   r+   V   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0 |r4ˆ j}nft|tƒrV‡ fdd„| ¡ D ƒ}nDt|tƒrt‡ 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.
        Fc                    s   i | ]\}}|ˆ   ||¡“qS r   ©Úcleanse_setting©Ú.0ÚkÚvr$   r   r   Ú
<dictcomp>w   ó    z?SafeExceptionReporterFilter.cleanse_setting.<locals>.<dictcomp>c                    s   g | ]}ˆ   d |¡‘qS ©Ú r<   ©r?   rA   r$   r   r   Ú
<listcomp>y   rC   z?SafeExceptionReporterFilter.cleanse_setting.<locals>.<listcomp>c                    s   g | ]}ˆ   d |¡‘qS rD   r<   rF   r$   r   r   rG   {   rC   )Úhidden_settingsÚsearchÚ	TypeErrorÚcleansed_substituteÚ
isinstanceÚdictÚitemsÚlistÚtupleÚcallabler   )r!   ÚkeyÚvalueZis_sensitiveÚcleansedr   r$   r   r=   j   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=   r8   )r!   Zsettings_dictr@   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   r<   r>   r$   r   r   rB   •   rC   zESafeExceptionReporterFilter.get_safe_request_meta.<locals>.<dictcomp>)ÚhasattrrX   rN   ©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   ÚDEBUGrZ   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)r8   r]   ÚcopyrK   )r!   r.   Zmultivaluedictr^   Úparamr   r   r   Úget_cleansed_multivaluedict    s    z7SafeExceptionReporterFilter.get_cleansed_multivaluedictc                 C   s|   |du ri S t |dg ƒ}|  |¡rr|rr|j ¡ }|dkrP|D ]}| j||< q<|S |D ]}||v rT| j||< qT|S n|jS dS )zk
        Replace the values of POST parameters marked as sensitive with
        stars (*********).
        Nr^   Ú__ALL__)r8   r]   ÚPOSTr_   rK   )r!   r.   r^   rT   r@   r`   r   r   r   Úget_post_parameters®   s     ÿ
z/SafeExceptionReporterFilter.get_post_parametersc              
   C   sT   zt |tƒ}W n0 ty> } zd ||¡W  Y d }~S d }~0 0 |rP|  ||¡}|S )Nz{!r} while evaluating {!r})rL   r   Ú	ExceptionÚformatra   )r!   r.   rS   Zis_multivalue_dictÚer   r   r   Úcleanse_special_typesÉ   s    "z1SafeExceptionReporterFilter.cleanse_special_typesc           	      C   s  |j }d}|durH|jjdkr@d|jv r@|jd }t|ddƒ}qH|j }q
i }|  |¡r´|r´|dkrz|jD ]}| j||< qhqØ|j ¡ D ],\}}||v rœ| j}n|  ||¡}|||< q„n$|j ¡ D ]\}}|  ||¡||< q¾|jjdk�rd|jv �r| j|d< | j|d< | ¡ S )ze
        Replace the values of variables marked as sensitive with
        stars (*********).
        NZsensitive_variables_wrapperÚsensitive_variablesrb   Ú	func_argsZfunc_kwargs)	Úf_backÚf_codeÚco_nameÚf_localsr8   r]   rK   rN   rh   )	r!   r.   Útb_frameÚcurrent_frameri   ÚwrapperrT   r   rS   r   r   r   Úget_traceback_frame_variables×   s<    
ÿþ


ÿþ

z9SafeExceptionReporterFilter.get_traceback_frame_variablesN)r   r   r   r&   rK   r   ÚreÚIrH   r=   rW   r[   r]   ra   rd   rh   rr   r   r   r   r   r:   _   s   ÿ	r:   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.txtrv   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.   r9   Úfilterr/   r0   r1   Úis_emailr8   Útemplate_infoÚtemplate_does_not_existÚ
postmortem)r!   r.   r/   r0   r1   rz   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ÚhostÚpath)rf   r.   r~   Z_get_raw_hostZget_full_pathr$   r   r   r   Ú_get_raw_insecure_uri%  s
    ýz'ExceptionReporter._get_raw_insecure_uric                 C   sj  | j r*t| j tƒr*d| _| jjp&| jg| _|  ¡ }t|ƒD ]p\}}d|v 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u�r0|	du�r0| jjd
 }
t|
t|d dƒt|	d t
|
ƒƒ… ddd�}ddlm} | jdu �rNd}n*zt| jjƒ}W n t�yv   d}Y n0 | j||| j| j | j¡|t| j | j¡ ¡ ƒ| j ¡ tj dtj!dd…  t" #¡ |ƒ tj$| j%| j| jdœ}| jdu�r*| jj& ¡ |d< | jj' ¡ |d< | jj( ¡ |d< |  )¡ |d< | j �r>| j j*|d< | j�rTt| jƒ|d< |�rf|d |d< |S )z5Return a dictionary containing traceback information.TÚvarsi   u   %sâ€¦ <trimmed %d bytes string>r   rE   ÚstartNÚendé   é   ÚasciiÚreplace)Úerrors)Úget_versionz%[unable to retrieve the current user]z%d.%d.%dé   )rz   Úunicode_hintÚframesr.   Zrequest_metaÚuser_strZfiltered_POST_itemsr   Zsys_executableZsys_version_infoZserver_timeZdjango_version_infoÚsys_pathr{   r|   r}   Zrequest_GET_itemsZrequest_FILES_itemsZrequest_COOKIES_itemsZrequest_insecure_uriZexception_typeZexception_valueéÿÿÿÿZ	lastframe)+r/   Ú
issubclassr	   r|   r0   Úchainr}   Úget_traceback_framesÚ	enumerater
   ÚlenÚappendÚUnicodeErrorr8   Úargsr   ÚmaxÚminÚdjangorŠ   r.   ÚstrÚuserre   rz   ry   r[   rO   rd   rN   rW   ÚsysÚ
executableÚversion_infor   Únowr€   r{   ÚGETZFILESZCOOKIESr�   r   )r!   r�   ÚiÚframeZ
frame_varsr@   rA   rŒ   rƒ   r„   Zunicode_strrŠ   rŽ   Úcr   r   r   Úget_traceback_data0  s~    
"ý
ÿîz$ExceptionReporter.get_traceback_datac                 C   sV   | j jdd��}t | ¡ ¡}W d  ƒ n1 s20    Y  t|  ¡ dd�}| |¡S )z1Return HTML version of debug 500 HTTP error page.úutf-8©ÚencodingNF)Úuse_l10n)rw   ÚopenÚDEBUG_ENGINEÚfrom_stringÚreadr   r¦   Úrender©r!   ÚfhÚtr¥   r   r   r   r,   }  s    ,z$ExceptionReporter.get_traceback_htmlc                 C   sX   | j jdd��}t | ¡ ¡}W d  ƒ n1 s20    Y  t|  ¡ ddd�}| |¡S )z7Return plain text version of debug 500 HTTP error page.r§   r¨   NF)Z
autoescaperª   )rx   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0 |d ur@| ¡ }|d u r–z:t|dƒ�}| ¡  ¡ }W d   ƒ n1 sv0    Y  W n ty”   Y n0 |S )NÚ
get_sourceÚrb)rY   r³   ÚImportErrorÚ
splitlinesr«   r®   ÚOSError)r!   ÚfilenameÚloaderÚmodule_nameÚsourceÚfpr   r   r   Ú_get_source‹  s    
.zExceptionReporter._get_sourceNc                    sä   |   |||¡}|du 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yÖ   dg dg f Y S 0 |	|||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   rG   ³  rC   z:ExceptionReporter._get_lines_from_file.<locals>.<listcomp>)r½   rL   Úbytesrs   rI   Ú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_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__)r8   )r!   r0   Ú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¡ q>q
g }|sJ|S | ¡ }|s\| jn|j}| 	|  
||¡¡ z| ¡ }W n ty–   Y q Y n0 |j}qb|S )NzHCycle in the exception chain detected: exception '%s' encountered again.)r0   r–   rÐ   ÚwarningsÚwarnr   Úpopr1   Ú__traceback__ÚextendÚget_exception_traceback_framesrÂ   )r!   Ú
exceptionsr0   r�   r1   r   r   r   r“   Æ  s2    

ÿý
z&ExceptionReporter.get_traceback_framesc                 c   s  |   |¡}t|ddƒ}|d u r.||d ddœV  |d u�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 u r¾|}
g }d}g }||||	 d¡rÒdnd|||d | j | j|j¡t|ƒ||||
d dœV  |j}q.d S )NrÌ   Tr�   )Ú	exc_causeÚexc_cause_explicitr1   ÚtypeZ__traceback_hide__r…   Ú
__loader__r   rE   é   z<source code not available>zdjango.r›   )rØ   rÙ   r1   rÚ   r¸   ÚfunctionrÃ   r‚   ÚidrÈ   rÉ   rÊ   Úpre_context_lineno)rÐ   r8   ro   rn   ÚgetÚtb_nextrl   Úco_filenamerm   Ú	tb_linenoÚ	f_globalsrË   Ú
startswithry   rr   r.   rÞ   )r!   r0   r1   rØ   rÙ   r¸   rÝ   rÃ   r¹   rº   rß   rÈ   rÉ   rÊ   r   r   r   rÖ   ç  sf    
ü



ûûÿñz0ExceptionReporter.get_exception_traceback_frames)F)NN)r   r   r   r&   Úpropertyrw   rx   r"   r�   r¦   r,   r-   r½   rË   rÐ   r“   rÖ   r   r   r   r   ru     s    


M ÿ
$!ru   c                 C   s  z|j d d }W n& tttfy8   | jdd… }Y n0 z|j d d }W n. tttfyz   d}| jrr| jjnd}Y np0 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}d
}zt| jƒ}W n t�y2   Y nn0 |j}t|dƒ�rL|j}t|dƒ�r`|j}n"t|dƒ�r‚t|jdƒ�r‚|jj}t|dƒ�r |j}	d|	|f }tdƒjdd��}
t |
 ¡ ¡}W d  ƒ n1 �sÖ0    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_namerE   Ú	namespaceZadminÚurlconfÚ
view_classr   Ú	__class__r   z%s.%sztechnical_404.htmlr§   r¨   )	rê   Zroot_urlconfÚrequest_pathZurlpatternsÚresolvedÚreasonr.   r   Zraising_view_namer(   ©r*   )#r˜   rÂ   rJ   ÚKeyErrorZ	path_infoÚresolver_matchrç   r€   r•   r8   Údefault_urlconfr   ZROOT_URLCONFrL   ÚtypesÚ
ModuleTyper   r   r   ÚfuncrY   rë   rì   r   r   r«   r¬   r­   r®   r7   r   rœ   rW   r   r¯   )r.   Ú	exceptionZ	error_urlrç   rî   rê   Zcallerrò   ÚobjÚmoduler±   r²   Zreporter_filterr¥   r   r   r   Útechnical_404_response!  sr    ÿ
þýûú
.÷ÿrú   c                 C   s^   t dƒjdd��}t | ¡ ¡}W d  ƒ n1 s40    Y  tdtƒ iƒ}t| |¡dd�S )z+Create an empty URLconf 404 error response.zdefault_urlconf.htmlr§   r¨   NÚversionr(   rð   )	r   r«   r¬   r­   r®   r   r   r   r¯   )r.   r±   r²   r¥   r   r   r   ró   d  s    ,ÿÿró   )r'   )/Ú	functoolsrs   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¬   r   ÚUserWarningr   r   r6   Ú	lru_cacher7   r9   r+   r:   ru   rú   ró   r   r   r   r   Ú<module>   sF   þ


	 0  C