B
    ª0H[�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_versionTZi18nzdjango.templatetags.i18n)ÚdebugÚ	librariesz#API|TOKEN|KEY|SECRET|PASS|SIGNATURE)Úflagsz********************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_setting© r   úVC:\Users\HIRONO~1\AppData\Local\Temp\pip-install-6bm3nxem\django\django\views\debug.pyÚ__init__)   s    zCallableSettingWrapper.__init__c             C   s
   t | jƒS )N)Úreprr   )r   r   r   r   Ú__repr__,   s    zCallableSettingWrapper.__repr__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   "   s   r   c             C   sd   y6t  | ¡rt}n"t|tƒr0dd„ | ¡ D ƒ}n|}W n tk
rN   |}Y nX 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   s6   i } x,t tƒD ] }| ¡ rt|tt|ƒƒ| |< qW | S )z{
    Return a dictionary of the settings module with values of sensitive
    settings replaced with stars (*********).
    )Údirr   Úisupperr    Úgetattr)Zsettings_dictr"   r   r   r   Úget_safe_settingsH   s
    r3   éô  c             C   sF   t | |||ƒ}|  ¡ r,| ¡ }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.
    ztext/plain; charset=utf-8)ÚstatusÚcontent_typez	text/htmlN)ÚExceptionReporterZis_ajaxÚget_traceback_textr   Úget_traceback_html)ÚrequestÚexc_typeÚ	exc_valueÚtbÚstatus_codeZreporterÚtextÚhtmlr   r   r   Útechnical_500_responseT   s    rA   c               C   s   t tjƒƒ S )N)r   r   Z!DEFAULT_EXCEPTION_REPORTER_FILTERr   r   r   r   Ú%get_default_exception_reporter_filterb   s    rB   c             C   s   t ƒ }t| d|ƒS )NZexception_reporter_filter)rB   r2   )r:   Zdefault_filterr   r   r   Úget_exception_reporter_filterh   s    rC   c               @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚExceptionReporterFilterzv
    Base for all exception reporter filter classes. All overridable hooks
    contain lenient default behaviors.
    c             C   s   |d kri S |j S d S )N)ÚPOST)r   r:   r   r   r   Úget_post_parameterss   s    z+ExceptionReporterFilter.get_post_parametersc             C   s   t |j ¡ ƒS )N)ÚlistÚf_localsr*   )r   r:   Útb_framer   r   r   Úget_traceback_frame_variablesy   s    z5ExceptionReporterFilter.get_traceback_frame_variablesN)r   r   r   r   rF   rJ   r   r   r   r   rD   m   s   rD   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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   sD   t |dg ƒ}|  |¡r@|r@| ¡ }x|D ]}||kr(t||< 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)r2   rM   Úcopyr'   )r   r:   ZmultivaluedictrN   Úparamr   r   r   Úget_cleansed_multivaluedictŒ   s    
z7SafeExceptionReporterFilter.get_cleansed_multivaluedictc             C   sˆ   |dkri S t |dg ƒ}|  |¡r~|r~|j ¡ }|dkrZx| ¡ D ]\}}t||< qBW |S x|D ]}||kr`t||< q`W |S n|jS dS )zk
        Replace the values of POST parameters marked as sensitive with
        stars (*********).
        NrN   Ú__ALL__)r2   rM   rE   rO   r*   r'   )r   r:   rN   r/   r"   r#   rP   r   r   r   rF   š   s    

z/SafeExceptionReporterFilter.get_post_parametersc          
   C   sN   yt |tƒ}W n* tk
r8 } zd ||¡S d }~X Y nX |rJ|  ||¡}|S )Nz{!r} while evaluating {!r})r(   r   Ú	ExceptionÚformatrQ   )r   r:   r.   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 }|  |¡rÄ|rÄ|dkrˆxZ|j ¡ D ]\}}t||< qrW qìxb|j ¡ D ]*\}}||krªt}n|  ||¡}|||< q”W n(x&|j ¡ D ]\}}|  ||¡||< qÐW |jjdk�rd|jk�rt|d< t|d< | ¡ S )ze
        Replace the values of variables marked as sensitive with
        stars (*********).
        NZsensitive_variables_wrapperÚsensitive_variablesrR   Ú	func_argsZfunc_kwargs)	Úf_backÚf_codeÚco_namerH   r2   rM   r*   r'   rV   )	r   r:   rI   Úcurrent_framerW   Úwrapperr/   Únamer.   r   r   r   rJ   Á   s4    



z9SafeExceptionReporterFilter.get_traceback_frame_variablesN)	r   r   r   r   rM   rQ   rF   rV   rJ   r   r   r   r   rK   }   s   	rK   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 )r7   z0Organize and coordinate reporting on exceptions.Fc             C   sJ   || _ t| j ƒ| _|| _|| _|| _|| _t| jdd ƒ| _d| _	d | _
d S )NZtemplate_debugF)r:   rC   Úfilterr;   r<   r=   Úis_emailr2   Útemplate_infoÚtemplate_does_not_existÚ
postmortem)r   r:   r;   r<   r=   r`   r   r   r   r   ö   s    zExceptionReporter.__init__c             C   sX  | j r*t| j tƒr*d| _| jjp&| jg| _|  ¡ }x€t|ƒD ]t\}}d|kr¨g }xN|d D ]B\}}t	|ƒ}t
|ƒdkrŽd|dd… t
|ƒf }| ||f¡ qZW ||d< |||< q<W d}| j �r8t| j tƒ�r8t| jddƒ}t| jd	dƒ}	|dk	�r8|	dk	�r8| jjd
 }
t|
t|d dƒt|	d t
|
ƒƒ… ddd�}ddlm} | jdk�rVd}n,yt| jjƒ}W n tk
�r€   d}Y nX | j||| j|t| j | j¡ ¡ ƒtƒ tjdtj dd…  t! "¡ |ƒ tj#| j$| j| jdœ}| jdk	�r| jj% ¡ |d< | jj& ¡ |d< | jj' ¡ |d< | j �r,| j j(|d< | j�rBt| jƒ|d< |�rT|d |d< |S )z5Return a dictionary containing traceback information.TÚvarsi   z%s... <trimmed %d bytes string>r   Ú ÚstartNÚendé   é   ÚasciiÚreplace)Úerrors)Úget_versionz%[unable to retrieve the current user]z%d.%d.%dé   )r`   Úunicode_hintÚframesr:   Úuser_strZfiltered_POST_itemsr   Zsys_executableZsys_version_infoZserver_timeZdjango_version_infoÚsys_pathra   rb   rc   Zrequest_GET_itemsZrequest_FILES_itemsZrequest_COOKIES_itemsZexception_typeZexception_valueéÿÿÿÿZ	lastframe))r;   Ú
issubclassr   rb   r<   Úchainrc   Úget_traceback_framesÚ	enumerater	   ÚlenÚappendÚUnicodeErrorr2   Úargsr   ÚmaxÚminÚdjangorm   r:   ÚstrÚuserrS   r`   rG   r_   rF   r*   r3   ÚsysÚ
executableÚversion_infor   ÚnowÚpathra   ÚGETZFILESZCOOKIESr   )r   rp   ÚiÚframeZ
frame_varsr"   r#   ro   rf   rg   Zunicode_strrm   rq   Úcr   r   r   Úget_traceback_data  sp    "


z$ExceptionReporter.get_traceback_datac          	   C   sD   t tddƒ ¡ �}t | ¡ ¡}W dQ R X t|  ¡ dd�}| |¡S )z1Return HTML version of debug 500 HTTP error page.Ú	templatesztechnical_500.htmlNF)Úuse_l10n)	r   ÚCURRENT_DIRÚopenÚDEBUG_ENGINEÚfrom_stringÚreadr   rŠ   Úrender)r   ÚfhÚtr‰   r   r   r   r9   I  s    z$ExceptionReporter.get_traceback_htmlc          	   C   sF   t tddƒ ¡ �}t | ¡ ¡}W dQ R X t|  ¡ ddd�}| |¡S )z7Return plain text version of debug 500 HTTP error page.r‹   ztechnical_500.txtNF)Z
autoescaperŒ   )	r   r�   rŽ   r�   r�   r‘   r   rŠ   r’   )r   r“   r”   r‰   r   r   r   r8   P  s    z$ExceptionReporter.get_traceback_textNc          	      sJ  d}|dk	rJt |dƒrJy| |¡}W n tk
r8   Y nX |dk	rJ| ¡ }|dkr’y&t|dƒ�}| ¡  ¡ }W dQ R X W n ttfk
r�   Y nX |dkr¦dg dg fS t|d t	ƒ�rd‰ x6|dd… D ]&}t
 d|¡}	|	rÈ|	 d¡ d¡‰ P qÈW ‡ 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   rj   é   s   coding[:=]\s*([-\w.]+)rh   c                s   g | ]}t |ˆ d ƒ‘qS )rk   )r   )r!   Úsline)Úencodingr   r   ú
<listcomp>y  s    z:ExceptionReporter._get_lines_from_file.<locals>.<listcomp>)Úhasattrr•   ÚImportErrorÚ
splitlinesrŽ   r‘   ÚOSErrorÚIOErrorr(   ÚbytesÚrer&   ÚgroupÚdecoder|   )r   ÚfilenameÚlinenoZcontext_linesÚloaderÚmodule_nameÚsourceÚfpÚlineÚmatchZlower_boundZupper_boundÚpre_contextÚcontext_lineÚpost_contextr   )r™   r   Ú_get_lines_from_fileW  s<    z&ExceptionReporter._get_lines_from_filec             C   sn  dd„ }g }| j }x|r,| |¡ ||ƒ}qW g }|s:|S | ¡ }|sL| jn|j}�x|d k	�rh|jj d¡rv|j}qV|jj	j
}|jj	j}|jd }|jj d¡}	|jj d¡p²d}
|  ||d|	|
¡\}}}}|d kræ|}g }d	}g }| ||ƒt|d
dƒ||
 d¡�rdnd|||d | j | j|j¡t|ƒ||||d dœ¡ |j�s`|�r`| ¡ }|j}qV|j}qVW |S )Nc             S   s    t | dd ƒ}t | dd ƒ}|p|S )NÚ	__cause__Ú__context__)r2   )r<   ÚexplicitZimplicitr   r   r   Úexplicit_or_implicit_cause…  s    zJExceptionReporter.get_traceback_frames.<locals>.explicit_or_implicit_causeZ__traceback_hide__rh   Ú
__loader__r   re   é   z<source code not available>r°   Tzdjango.r~   r€   )Z	exc_causeZexc_cause_explicitr=   Útyper¤   Úfunctionr¥   rd   Úidr¬   r­   r®   Úpre_context_lineno)r<   ry   Úpopr=   Ú__traceback__rI   rH   ÚgetÚtb_nextrZ   Úco_filenamer[   Ú	tb_linenoÚ	f_globalsr¯   r2   Ú
startswithr_   rJ   r:   r¸   )r   r³   Ú
exceptionsr<   rp   r=   r¤   r·   r¥   r¦   r§   r¹   r¬   r­   r®   r   r   r   rv   „  s\    





z&ExceptionReporter.get_traceback_frames)F)NN)
r   r   r   r   r   rŠ   r9   r8   r¯   rv   r   r   r   r   r7   ô   s   
G
-r7   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 n tttfk
rl   g }Y nlX |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}yt| jƒ}W n tk
�r    Y n\X |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Q R X t|t	j
||t|ƒ| tƒ |dœƒ}t|
 |¡dd�S )zBCreate a technical 404 error response. `exception` is the Http404.r   r…   rh   NÚtriedú/Zapp_namere   Ú	namespaceZadminÚurlconfr   Ú	__class__r   z%s.%sr‹   ztechnical_404.html)rÆ   Zroot_urlconfÚrequest_pathZurlpatternsÚreasonr:   r   Zraising_view_namez	text/html)r6   ) r{   Ú
IndexErrorr+   ÚKeyErrorZ	path_infor…   rx   r2   Údefault_urlconfr   ZROOT_URLCONFr(   ÚtypesÚ
ModuleTyper   r   r
   Úfuncr›   rÇ   r   r   r�   rŽ   r�   r�   r‘   r   r   r3   r   r’   )r:   Ú	exceptionZ	error_urlrÃ   rÆ   ZcallerZresolver_matchÚobjÚmoduler“   r”   r‰   r   r   r   Útechnical_404_responseÈ  sT    

8
rÓ   c          	   C   sJ   t tddƒ ¡ �}t | ¡ ¡}W dQ R X tdtƒ iƒ}t| 	|¡dd�S )z+Create an empty URLconf 404 error response.r‹   zdefault_urlconf.htmlNÚversionz	text/html)r6   )
r   r�   rŽ   r�   r�   r‘   r   r   r   r’   )r:   r“   r”   r‰   r   r   r   rÌ      s
    rÌ   )r4   )2Ú	functoolsr¡   r�   rÍ   Zpathlibr   Zdjango.confr   Zdjango.httpr   r   Zdjango.templater   r   r   Zdjango.template.defaultfiltersr	   Zdjango.urlsr
   r   Zdjango.utilsr   Zdjango.utils.datastructuresr   Zdjango.utils.encodingr   Zdjango.utils.module_loadingr   Zdjango.utils.versionr   r�   ÚcompileÚ
IGNORECASEr%   r'   Ú__file__Úparentr�   r   r    r3   rA   Ú	lru_cacherB   rC   rD   rK   r7   rÓ   rÌ   r   r   r   r   Ú<module>   s@   

w U8