o
    uvXjO  ã                   @   sb  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
 d dlmZmZ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
lmZ d dlmZ d dlmZ edddid�Zejdej d�Z!dZ"ee#ƒj$Z%G dd„ dƒZ&dd„ Z'dd„ Z(d+dd„Z)e  *¡ dd„ ƒZ+dd „ Z,G d!d"„ d"ƒZ-G d#d$„ d$e-ƒZ.G d%d&„ d&ƒZ/d'd(„ Z0d)d*„ Z1dS ),é    N)ÚPath)Úsettings)ÚHttpResponseÚHttpResponseNotFound)ÚContextÚEngineÚTemplateDoesNotExist)Úpprint)ÚResolver404Úresolve)Útimezone)ÚMultiValueDict)Ú
force_text)Úimport_string)Úget_docs_versionTÚi18nzdjango.templatetags.i18n)ÚdebugÚ	librariesz#API|TOKEN|KEY|SECRET|PASS|SIGNATURE)Úflagsz********************c                   @   ó    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)ÚselfÚcallable_setting© r   úO/var/www/html/myproject/venv/lib/python3.10/site-packages/django/views/debug.pyÚ__init__)   ó   
zCallableSettingWrapper.__init__c                 C   s
   t | jƒS r   )Úreprr   )r   r   r   r   Ú__repr__,   r   zCallableSettingWrapper.__repr__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r    r   r   r   r   r   "   s    r   c                 C   sb   zt  | ¡r	t}nt|tƒrdd„ | ¡ D ƒ}n|}W n ty&   |}Y nw 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.
    c                 S   s   i | ]
\}}|t ||ƒ“qS r   )Úcleanse_setting)Ú.0ÚkÚvr   r   r   Ú
<dictcomp>:   s    z#cleanse_setting.<locals>.<dictcomp>)	ÚHIDDEN_SETTINGSÚsearchÚCLEANSED_SUBSTITUTEÚ
isinstanceÚdictÚitemsÚ	TypeErrorÚcallabler   )ÚkeyÚvalueÚcleansedr   r   r   r%   0   s   

€þr%   c                  C   s2   i } t tƒD ]}| ¡ rt|tt|ƒƒ| |< q| S )z{
    Return a dictionary of the settings module with values of sensitive
    settings replaced with stars (*********).
    )Údirr   Úisupperr%   Úgetattr)Úsettings_dictr'   r   r   r   Úget_safe_settingsH   s   €r9   éô  c                 C   sB   t | |||ƒ}|  ¡ 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.
    ztext/plain; charset=utf-8)ÚstatusÚcontent_typeú	text/html)ÚExceptionReporterÚis_ajaxÚget_traceback_textr   Úget_traceback_html)ÚrequestÚexc_typeÚ	exc_valueÚtbÚstatus_codeÚreporterÚtextÚhtmlr   r   r   Útechnical_500_responseT   s   rJ   c                   C   s   t tjƒƒ S r   )r   r   Ú!DEFAULT_EXCEPTION_REPORTER_FILTERr   r   r   r   Ú%get_default_exception_reporter_filterb   s   rL   c                 C   s   t ƒ }t| d|ƒS )NÚexception_reporter_filter)rL   r7   )rB   Údefault_filterr   r   r   Úget_exception_reporter_filterh   s   rO   c                   @   r   )ÚExceptionReporterFilterzv
    Base for all exception reporter filter classes. All overridable hooks
    contain lenient default behaviors.
    c                 C   s   |d u ri S |j S r   )ÚPOST©r   rB   r   r   r   Úget_post_parameterss   s   z+ExceptionReporterFilter.get_post_parametersc                 C   s   t |j ¡ ƒS r   )ÚlistÚf_localsr/   )r   rB   Útb_framer   r   r   Úget_traceback_frame_variablesy   s   z5ExceptionReporterFilter.get_traceback_frame_variablesN)r!   r"   r#   r$   rS   rW   r   r   r   r   rP   m   s    rP   c                   @   s8   e Zd Z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.
    c                 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   ÚDEBUGrR   r   r   r   Ú	is_activeƒ   s   
z%SafeExceptionReporterFilter.is_activec                 C   s@   t |dg ƒ}|  |¡r|r| ¡ }|D ]
}||v rt||< 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)r7   rZ   Úcopyr,   )r   rB   Úmultivaluedictr[   Úparamr   r   r   Úget_cleansed_multivaluedictŒ   s   €z7SafeExceptionReporterFilter.get_cleansed_multivaluedictc                 C   sr   |du ri S t |dg ƒ}|  |¡r6|r6|j ¡ }|dkr'|D ]}t||< q|S |D ]
}||v r3t||< q)|S |jS )zk
        Replace the values of POST parameters marked as sensitive with
        stars (*********).
        Nr[   Ú__ALL__)r7   rZ   rQ   r\   r,   )r   rB   r[   r4   r'   r^   r   r   r   rS   š   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})r-   r   Ú	ExceptionÚformatr_   )r   rB   r3   Ú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 ]}t||< q7n.|j ¡ D ]\}}||v rOt}n|  ||¡}|||< qDn|j ¡ D ]\}}|  ||¡||< q`|jjdkr€d|jv r€t|d< t|d< | ¡ S )ze
        Replace the values of variables marked as sensitive with
        stars (*********).
        NÚsensitive_variables_wrapperÚsensitive_variablesr`   Ú	func_argsÚfunc_kwargs)	Úf_backÚf_codeÚco_namerU   r7   rZ   r,   r/   re   )	r   rB   rV   Úcurrent_framerg   Úwrapperr4   Únamer3   r   r   r   rW   Á   s:   

ø


ÿ
û	
z9SafeExceptionReporterFilter.get_traceback_frame_variablesN)	r!   r"   r#   r$   rZ   r_   rS   re   rW   r   r   r   r   rX   }   s    	rX   c                   @   sD   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ Zddd„Zdd„ Z	dS )r>   z0Organize and coordinate reporting on exceptions.Fc                 C   sJ   || _ t| j ƒ| _|| _|| _|| _|| _t| jdd ƒ| _d| _	d | _
d S )NÚtemplate_debugF)rB   rO   ÚfilterrC   rD   rE   Úis_emailr7   Útemplate_infoÚtemplate_does_not_existÚ
postmortem)r   rB   rC   rD   rE   rr   r   r   r   r   ö   s   
zExceptionReporter.__init__c                 C   sB  | 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 r”t| j tƒr”t| 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 r¢d}nzt| jjƒ}W n tyµ   d}Y nw | j||| j|t| j | j¡ ¡ ƒtƒ 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< | j �r| j j(|d< | j�rt| jƒ|d< |�r|d |d< |S )z5Return a dictionary containing traceback information.TÚvarsi   u   %sâ€¦ <trimmed %d bytes string>r   Ú ÚstartNÚendé   é   ÚasciiÚreplace)Úerrors)Úget_versionz%[unable to retrieve the current user]z%d.%d.%dé   )rr   Úunicode_hintÚframesrB   Úuser_strÚfiltered_POST_itemsr   Úsys_executableÚsys_version_infoÚserver_timeÚdjango_version_infoÚsys_pathrs   rt   ru   Úrequest_GET_itemsÚrequest_FILES_itemsÚrequest_COOKIES_itemsÚexception_typeÚexception_valueéÿÿÿÿÚ	lastframe))rC   Ú
issubclassr   rt   rD   Úchainru   Úget_traceback_framesÚ	enumerater	   ÚlenÚappendÚUnicodeErrorr7   Úargsr   ÚmaxÚminÚdjangor   rB   ÚstrÚuserra   rr   rT   rq   rS   r/   r9   ÚsysÚ
executableÚversion_infor   ÚnowÚpathrs   ÚGETÚFILESÚCOOKIESr!   )r   r‚   ÚiÚframeÚ
frame_varsr'   r(   r�   rx   ry   Úunicode_strr   rƒ   Úcr   r   r   Úget_traceback_data  sv   
"þ
ýñz$ExceptionReporter.get_traceback_datac                 C   s\   t tddƒjdd��}t | ¡ ¡}W d  ƒ n1 sw   Y  t|  ¡ dd�}| |¡S )z1Return HTML version of debug 500 HTTP error page.Ú	templatesztechnical_500.htmlúutf-8©ÚencodingNF)Úuse_l10n©	r   ÚCURRENT_DIRÚopenÚDEBUG_ENGINEÚfrom_stringÚreadr   r«   Úrender©r   ÚfhÚtrª   r   r   r   rA   I  s
   ÿ
z$ExceptionReporter.get_traceback_htmlc                 C   s^   t tddƒ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¬   ztechnical_500.txtr­   r®   NF)Ú
autoescaper°   r±   r¸   r   r   r   r@   P  s
   ÿ
z$ExceptionReporter.get_traceback_textNc              	      sN  d}t |dƒr z| |¡}W n	 ty   Y nw |dur | ¡ }|du rMzt|dƒ�}| ¡  ¡ }W d  ƒ n1 s;w   Y  W n ttfyL   Y nw |du rWdg dg fS t|d t	ƒr„d‰ |dd… D ]}t
 d|¡}	|	rz|	 d¡ d¡‰  nqf‡ fd	d
„|D ƒ}td|| ƒ}
|| }||
|… }|| }||d |… }|
|||fS )z—
        Return context_lines before and after lineno from file.
        Return (pre_context_lineno, pre_context, context_line, post_context).
        NÚ
get_sourceÚrbr   r|   é   s   coding[:=]\s*([-\w.]+)rz   c                    s   g | ]}t |ˆ d ƒ‘qS )r}   )rœ   )r&   Úsliner®   r   r   Ú
<listcomp>y  s    z:ExceptionReporter._get_lines_from_file.<locals>.<listcomp>)Úhasattrr¼   ÚImportErrorÚ
splitlinesr³   r¶   ÚOSErrorÚIOErrorr-   ÚbytesÚrer+   ÚgroupÚdecoder™   )r   ÚfilenameÚlinenoÚcontext_linesÚloaderÚmodule_nameÚsourceÚfpÚlineÚmatchÚlower_boundÚupper_boundÚpre_contextÚcontext_lineÚpost_contextr   r®   r   Ú_get_lines_from_fileW  sF   
ÿÿ€ÿþz&ExceptionReporter._get_lines_from_filec                 C   sd  dd„ }g }| j }|r| |¡ ||ƒ}|sg }|s|S | ¡ }|s%| jn|j}|d ur°|jj d¡r7|j}q(|jj	j
}|jj	j}|jd }|jj d¡}	|jj d¡pUd}
|  ||d|	|
¡\}}}}|d u ro|}g }d	}g }| ||ƒt|d
dƒ||
 d¡r�dnd|||d | j | j|j¡t|ƒ||||d dœ¡ |js©|r©| ¡ }|j}n|j}|d us,|S )Nc                 S   s    t | dd ƒ}t | dd ƒ}|p|S )NÚ	__cause__Ú__context__)r7   )rD   ÚexplicitÚimplicitr   r   r   Úexplicit_or_implicit_cause…  s   zJExceptionReporter.get_traceback_frames.<locals>.explicit_or_implicit_causeÚ__traceback_hide__rz   Ú
__loader__r!   rw   é   z<source code not available>rÙ   Tzdjango.r›   r�   )Ú	exc_causeÚexc_cause_explicitrE   ÚtyperÊ   ÚfunctionrË   rv   ÚidrÕ   rÖ   r×   Úpre_context_lineno)rD   r–   ÚpoprE   Ú__traceback__rV   rU   ÚgetÚtb_nextrk   Úco_filenamerl   Ú	tb_linenoÚ	f_globalsrØ   r7   Ú
startswithrq   rW   rB   rå   )r   rÝ   Ú
exceptionsrD   r‚   rE   rÊ   rä   rË   rÍ   rÎ   ræ   rÕ   rÖ   r×   r   r   r   r“   „  sd   
þ



ÿ
ó
×+z&ExceptionReporter.get_traceback_frames)F)NN)
r!   r"   r#   r$   r   r«   rA   r@   rØ   r“   r   r   r   r   r>   ô   s    
G
-r>   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y4   g }Y n9w |ri| jdkrmt|ƒdkrmt|d ƒdkrmt|d d ddƒt|d d d	dƒ  krgd
krmt| ƒ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ƒr«t|jdƒr«|jj}t|dƒr¹|j}d||f }ttddƒ ¡ �}	t |	 ¡ ¡}
W d  ƒ n1 sÓw   Y  t|t	j
||t|ƒ| tƒ |dœƒ}t|
 |¡dd�S )zBCreate a technical 404 error response. `exception` is the Http404.r   r¢   rz   NÚtriedú/Úapp_namerw   Ú	namespaceÚadminÚurlconfr!   Ú	__class__r"   z%s.%sr¬   ztechnical_404.html)rõ   Úroot_urlconfÚrequest_pathÚurlpatternsÚreasonrB   r   Úraising_view_namer=   ©r<   ) r˜   Ú
IndexErrorr0   ÚKeyErrorÚ	path_infor¢   r•   r7   Údefault_urlconfr   ÚROOT_URLCONFr-   ÚtypesÚ
ModuleTyper!   r   r
   ÚfuncrÁ   rö   r"   r   r²   r³   r´   rµ   r¶   r   rœ   r9   r   r·   )rB   Ú	exceptionÚ	error_urlrð   rõ   ÚcallerÚresolver_matchÚobjÚmoduler¹   rº   rª   r   r   r   Útechnical_404_responseÈ  sb   ÿÿ
2þÿ

ÿø
r  c                 C   s^   t t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.r¬   zdefault_urlconf.htmlNÚversionr=   rü   )
r   r²   r³   r´   rµ   r¶   r   r   r   r·   )rB   r¹   rº   rª   r   r   r   r      s   ÿÿr   )r:   )2Ú	functoolsrÇ   rž   r  Úpathlibr   Údjango.confr   Údjango.httpr   r   Údjango.templater   r   r   Údjango.template.defaultfiltersr	   Údjango.urlsr
   r   Údjango.utilsr   Údjango.utils.datastructuresr   Údjango.utils.encodingr   Údjango.utils.module_loadingr   Údjango.utils.versionr   r´   ÚcompileÚ
IGNORECASEr*   r,   Ú__file__Úparentr²   r   r%   r9   rJ   Ú	lru_cacherL   rO   rP   rX   r>   r  r   r   r   r   r   Ú<module>   sF    þ


w U8