o
    ^"<fm                     @   s  d 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
 ddlmZ ddl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mZ ddlmZmZ ddlm Z  ddl!m"Z" ddl#m$Z$m%Z% ddl&m'Z' G dd dZ(d.ddZ)ej*ddd.ddZ+ej*dddd Z,G dd dZ-G dd dZ.G d d! d!e.Z/ed"Z0d/d$d%Z1G d&d' d'e.Z2G d(d) d)Z3G d*d+ d+Z4G d,d- d-Z5dS )0z
This module converts requested URLs to callback view functions.

URLResolver is the main class here. Its resolve() method takes a URL (as
a string) and returns a ResolverMatch object which provides access to all
attributes of the resolved URL match.
    N)import_module)quote)Local)settings)ErrorWarning)check_resolver)ImproperlyConfiguredViewDoesNotExist)MultiValueDict)cached_property)RFC3986_SUBDELIMSescape_leading_slashes)_lazy_re_compile	normalize)get_language   )get_converter)NoReverseMatchResolver404)get_callablec                   @   s&   e Zd ZdddZdd Zdd ZdS )	ResolverMatchNc	           
      C   s   || _ || _|| _|| _|| _|| _|rdd |D ng | _d| j| _|r.dd |D ng | _	d| j	| _
t|dsI|jjd |jj | _n	|jd |j | _|pV| j}	d| j	|	g | _d S )Nc                 S      g | ]}|r|qS  r   .0xr   r   L/var/www/html/kck/venv/lib/python3.10/site-packages/django/urls/resolvers.py
<listcomp>+       z*ResolverMatch.__init__.<locals>.<listcomp>:c                 S   r   r   r   r   r   r   r   r   -   r   __name__.)funcargskwargsurl_nameroutetried	app_namesjoinapp_name
namespaces	namespacehasattr	__class__
__module__r!   
_func_path	view_name)
selfr#   r$   r%   r&   r)   r,   r'   r(   	view_pathr   r   r   __init__!   s   

zResolverMatch.__init__c                 C   s   | j | j| jf| S N)r#   r$   r%   )r3   indexr   r   r   __getitem__:   s   zResolverMatch.__getitem__c                 C   s$   d| j | j| j| j| j| j| jf S )Nz^ResolverMatch(func=%s, args=%s, kwargs=%s, url_name=%s, app_names=%s, namespaces=%s, route=%s))r1   r$   r%   r&   r)   r,   r'   r3   r   r   r   __repr__=   s   zResolverMatch.__repr__)NNNNN)r!   r0   __qualname__r5   r8   r:   r   r   r   r   r       s    
r   c                 C   s   | d u rt j} t| S r6   )r   ROOT_URLCONF_get_cached_resolverurlconfr   r   r   get_resolverD   s   r@   )maxsizec                 C   s   t td| S N^/)URLResolverRegexPatternr>   r   r   r   r=   J   s   r=   c                 C   s.   t | }t||_t||j}tt d|gS rB   )rE   dict
convertersrD   url_patterns)
ns_patternresolverrG   patternns_resolverr   r   r   get_ns_resolverO   s   
rM   c                   @   s   e Zd Zdd ZdddZdS )LocaleRegexDescriptorc                 C   s
   || _ d S r6   )attr)r3   rO   r   r   r   r5   [      
zLocaleRegexDescriptor.__init__Nc                 C   sj   |du r| S t || j}t|tr|||jd< |jd S t }||jvr0|t||j|< |j| S )zT
        Return a compiled regular expression based on the active language.
        Nregex)getattrrO   
isinstancestr_compile__dict__r   _regex_dict)r3   instanceclsrK   language_coder   r   r   __get__^   s   



zLocaleRegexDescriptor.__get__r6   )r!   r0   r;   r5   r[   r   r   r   r   rN   Z   s    rN   c                   @   s   e Zd Zdd Zdd ZdS )CheckURLMixinc                 C   s$   d | }| jr|d | j7 }|S )zI
        Format the URL pattern for display in warning messages.
        '{}'z [name='{}'])formatname)r3   descriptionr   r   r   describer   s   
zCheckURLMixin.describec                 C   sF   | j j}tjs	g S |dr!|ds!td|  dd}|gS g S )zM
        Check that the pattern does not begin with a forward slash.
        )/rC   z^\/rb   zYour URL pattern {} has a route beginning with a '/'. Remove this slash as it is unnecessary. If this pattern is targeted in an include(), ensure the include() pattern has a trailing '/'.z	urls.W002id)	rQ   rK   r   APPEND_SLASH
startswithendswithr   r^   ra   )r3   regex_patternwarningr   r   r   _check_pattern_startswith_slash{   s   z-CheckURLMixin._check_pattern_startswith_slashN)r!   r0   r;   ra   rj   r   r   r   r   r\   q   s    	r\   c                   @   sF   e Zd ZedZdddZdd Zdd	 Zd
d Zdd Z	dd Z
dS )rE   _regexNFc                 C   s"   || _ i | _|| _|| _i | _d S r6   )rk   rW   _is_endpointr_   rG   )r3   rQ   r_   is_endpointr   r   r   r5      s
   
zRegexPattern.__init__c                 C   st   | j r| jjdr| j|n| j|}|r8| }|r dn| }dd | D }||	 d  ||fS d S )N$r   c                 S   s   i | ]\}}|d ur||qS r6   r   r   kvr   r   r   
<dictcomp>   s    z&RegexPattern.match.<locals>.<dictcomp>)
rl   rQ   rK   rg   	fullmatchsearch	groupdictgroupsitemsend)r3   pathmatchr%   r$   r   r   r   rz      s   
zRegexPattern.matchc                 C   s*   g }| |   | js| |   |S r6   )extendrj   rl   _check_include_trailing_dollarr3   warningsr   r   r   check   s
   zRegexPattern.checkc                 C   s8   | j j}|dr|dstd|  ddgS g S )Nrn   z\$zYour URL pattern {} uses include with a route ending with a '$'. Remove the dollar from the route to avoid problems including URLs.z	urls.W001rc   )rQ   rK   rg   r   r^   ra   )r3   rh   r   r   r   r|      s   
z+RegexPattern._check_include_trailing_dollarc              
   C   s:   zt |W S  t jy } z	td||f |d}~ww )z0Compile and return the given regular expression.z*"%s" is not a valid regular expression: %sN)recompileerrorr	   )r3   rQ   er   r   r   rU      s   
zRegexPattern._compilec                 C   
   t | jS r6   )rT   rk   r9   r   r   r   __str__   rP   zRegexPattern.__str__NF)r!   r0   r;   rN   rQ   r5   rz   r   r|   rU   r   r   r   r   r   rE      s    
	rE   z1<(?:(?P<converter>[^>:]+):)?(?P<parameter>[^>]+)>Fc           
   
   C   s*  | }dg}i }	 t | }|s|t|  not| tj	s(t
d| |t| d|   | | d } |d }| sNt
d||f |d }|du rXd}zt|}W n tys }	 z	t
d	||f |	d}	~	ww |||< |d
| d |j d  q|r|d d||fS )a  
    Convert a path pattern into a regular expression. Return the regular
    expression and a dictionary mapping the capture names to the converters.
    For example, 'foo/<int:pk>' returns '^foo\/(?P<pk>[0-9]+)'
    and {'pk': <django.urls.converters.IntConverter>}.
    ^TuA   URL route '%s' cannot contain whitespace in angle brackets <…>.N	parameterzLURL route '%s' uses parameter name %r which isn't a valid Python identifier.	converterrT   z'URL route %r uses invalid converter %r.(?P<>)z\Z )_PATH_PARAMETER_COMPONENT_RErt   appendr   escapesetgroup
isdisjointstring
whitespacer	   startrx   isidentifierr   KeyErrorrQ   r*   )
r'   rm   original_routepartsrG   rz   r   raw_converterr   r   r   r   r   _route_to_regex   sX   

r   c                   @   s>   e Zd ZedZdddZdd Zdd	 Zd
d Zdd Z	dS )RoutePattern_routeNFc                 C   s0   || _ i | _|| _|| _tt||d | _d S )Nr   )r   rW   rl   r_   r   rT   rG   )r3   r'   r_   rm   r   r   r   r5     s
   zRoutePattern.__init__c              	   C   sv   | j |}|r9| }| D ]\}}| j| }z	||||< W q ty-   Y  d S w || d  d|fS d S Nr   )rQ   rt   ru   rw   rG   	to_python
ValueErrorrx   )r3   ry   rz   r%   keyvaluer   r   r   r   rz   	  s   
zRoutePattern.matchc                 C   sJ   |   }| j}d|v s|ds|dr#|td|  dd |S )Nr   r   rn   zYour URL pattern {} has a route that contains '(?P<', begins with a '^', or ends with a '$'. This was likely an oversight when migrating to django.urls.path().z2_0.W001rc   )rj   r   rf   rg   r   r   r^   ra   )r3   r~   r'   r   r   r   r     s   
zRoutePattern.checkc                 C   s   t t|| jd S )Nr   )r   r   r   rl   )r3   r'   r   r   r   rU   #     zRoutePattern._compilec                 C   r   r6   )rT   r   r9   r   r   r   r   &  rP   zRoutePattern.__str__r   )
r!   r0   r;   rN   rQ   r5   rz   r   rU   r   r   r   r   r   r      s    
r   c                   @   sN   e Zd ZdddZedd Zedd Zdd	 Zd
d Zdd Z	dd Z
dS )LocalePrefixPatternTc                 C   s   || _ i | _d S r6   )prefix_default_languagerG   )r3   r   r   r   r   r5   +  s   
zLocalePrefixPattern.__init__c                 C   s   t t | jS r6   )r   r   r   language_prefixr9   r   r   r   rQ   /  s   zLocalePrefixPattern.regexc                 C   s(   t  ptj}|tjkr| jsdS d| S )Nr   z%s/)r   r   LANGUAGE_CODEr   r3   rZ   r   r   r   r   4  s   z#LocalePrefixPattern.language_prefixc                 C   s*   | j }||r|t|d  di fS d S r   )r   rf   len)r3   ry   r   r   r   r   rz   <  s   
zLocalePrefixPattern.matchc                 C   s   g S r6   r   r9   r   r   r   r   B  s   zLocalePrefixPattern.checkc                 C   s
   d | S )Nr]   )r^   r9   r   r   r   ra   E  rP   zLocalePrefixPattern.describec                 C   s   | j S r6   )r   r9   r   r   r   r   H  s   zLocalePrefixPattern.__str__N)T)r!   r0   r;   r5   propertyrQ   r   rz   r   ra   r   r   r   r   r   r   *  s    


r   c                   @   sB   e Zd ZdddZdd Zdd Zdd	 Zd
d Zedd Z	dS )
URLPatternNc                 C   s    || _ || _|p	i | _|| _d S r6   )rK   callbackdefault_argsr_   )r3   rK   r   r   r_   r   r   r   r5   M  s   

zURLPattern.__init__c                 C   s   d| j j| j f S )Nz<%s %s>)r/   r!   rK   ra   r9   r   r   r   r:   S  r   zURLPattern.__repr__c                 C   s   |   }|| j  |S r6   )_check_pattern_namer{   rK   r   r}   r   r   r   r   V  s   zURLPattern.checkc                 C   s:   | j jdurd| j jv rtd| j  dd}|gS g S )zG
        Check that the pattern name does not contain a colon.
        Nr    zjYour URL pattern {} has a name including a ':'. Remove the colon, to avoid ambiguous namespace references.z	urls.W003rc   )rK   r_   r   r^   ra   )r3   ri   r   r   r   r   [  s   zURLPattern._check_pattern_namec                 C   sH   | j |}|r"|\}}}|| j t| j||| j jt| j dS d S )N)r'   )rK   rz   updater   r   r   r_   rT   )r3   ry   rz   new_pathr$   r%   r   r   r   resolvei  s   
zURLPattern.resolvec                 C   sD   | j }t|tjr|j}t|ds|jd |jj S |jd |j	 S )zw
        A string that identifies the view (e.g. 'path.to.view_function' or
        'path.to.ClassBasedView').
        r!   r"   )
r   rS   	functoolspartialr#   r.   r0   r/   r!   r;   )r3   r   r   r   r   
lookup_strq  s   
zURLPattern.lookup_str)NN)
r!   r0   r;   r5   r:   r   r   r   r   r   r   r   r   r   r   L  s    
r   c                   @   s   e Zd Zd$ddZdd Zdd Zdd	 Zd
d Zedd Z	edd Z
edd Zed%ddZedd Zdd Zdd Zedd Zedd Zdd Zd d! Zd"d# ZdS )&rD   Nc                 C   sT   || _ || _d | _|pi | _|| _|| _i | _i | _i | _t	 | _
d| _t | _d S r   )rK   urlconf_namer   default_kwargsr-   r+   _reverse_dict_namespace_dict	_app_dictr   _callback_strs
_populatedr   _local)r3   rK   r   r   r+   r-   r   r   r   r5     s   
zURLResolver.__init__c                 C   sP   t | jtr| jrd| jd jj }nt| j}d| jj|| j| j| j	 f S )Nz	<%s list>r   z<%s %s (%s:%s) %s>)
rS   r   listr/   r!   reprr+   r-   rK   ra   )r3   urlconf_reprr   r   r   r:     s   
zURLResolver.__repr__c                 C   s:   g }| j D ]	}|t| q||   |p| j S r6   )rH   r{   r   _check_custom_error_handlersrK   r   )r3   messagesrK   r   r   r   r     s
   
zURLResolver.checkc           
      C   s   g }dD ]r\}}z|  |}W n/ ttfy> } z!t| jd| }dj||d}|t|t|dd W Y d }~qd }~ww t	
|}d g| }	z|j|	  W q tyv   dj||jd |j |d	krgd
ndd}|t|dd Y qw |S )N))i     )i  r   )i  r   )i  r   	handler%szDThe custom handler{status_code} view '{path}' could not be imported.)status_codery   z	urls.E008)hintrd   zeThe custom handler{status_code} view '{path}' does not take the correct number of arguments ({args}).r"   r   zrequest, exceptionrequest)r   ry   r$   z	urls.E007rc   )resolve_error_handlerImportErrorr
   rR   urlconf_moduler^   r   r   rT   inspect	signaturebind	TypeErrorr0   r;   )
r3   r   r   num_parametershandlerr   ry   msgr   r$   r   r   r   r     s6   


z(URLResolver._check_custom_error_handlersc                 C   s  t | jddr	d S zd| j_t }i }i }t }t| jD ]}|jjj}|	dr/|dd  }t
|trd| j|j t|jjj}||j|||j|jjf |jd urc||j|||j|jjf q|  |jr~||jg |j ||f||j< nf|jD ]0}|j|D ]'\}	}
}}t||
 }|||||
 i ||ji | jj|jj|f qq|j D ]\}\}}|jj}|jj| || |f||< q|j  D ]\}}||g !| q| j|j q|| j"|< || j#|< || j$|< d| _%W d| j_d S d| j_w )N
populatingFTr   r   )&rR   r   r   r   r   reversedrH   rK   rQ   rf   rS   r   r   addr   r   
appendlistr   r   rG   r_   	_populater+   
setdefaultr   r-   reverse_dictgetlistr   namespace_dictrw   r   app_dictr{   r   r   r   r   )r3   lookupsr,   appsrZ   url_pattern	p_patternbitsr_   matchespatdefaultsrG   new_matchesr-   prefixsub_patterncurrent_convertersr+   namespace_listr   r   r   r     sj   







zURLResolver._populatec                 C   "   t  }|| jvr|   | j| S r6   )r   r   r   r   r   r   r   r         

zURLResolver.reverse_dictc                 C   r   r6   )r   r   r   r   r   r   r   r     r   zURLResolver.namespace_dictc                 C   r   r6   )r   r   r   r   r   r   r   r     r   zURLResolver.app_dictc                    s4   |d u r|   g d S |  fdd|D  d S )Nc                 3   s    | ]} g|V  qd S r6   r   )r   trK   r   r   	<genexpr>  s    z,URLResolver._extend_tried.<locals>.<genexpr>)r   r{   )r(   rK   	sub_triedr   r   r   _extend_tried  s   zURLResolver._extend_triedc                 C   s&   | s|S | dr|dd }| | S )z<Join two routes, without the starting ^ in the second route.r   r   N)rf   )route1route2r   r   r   _join_route  s
   
zURLResolver._join_routec                 C   s   | j s|   || jv S r6   )r   r   r   )r3   r_   r   r   r   _is_callback%  s   
zURLResolver._is_callbackc                 C   s<  t |}g }| j|}|r|\}}}| jD ]z}z||}W n ty> }	 z| |||	jd d W Y d }	~	qd }	~	ww |ri || j	}
|

|j |j}|
sX||j }t|tr_dnt |j}| |||j t|j||
|j| jg|j | jg|j | ||j|  S ||g qt||dtd|i)Nr   r(   r   )r(   ry   ry   )rT   rK   rz   rH   r   r   r   r$   getr   r   r%   rS   r   r(   r   r#   r&   r+   r)   r-   r,   r   r'   r   )r3   ry   r(   rz   r   r$   r%   rK   	sub_matchr   sub_match_dictsub_match_argscurrent_router   r   r   r   *  sB   

&

zURLResolver.resolvec                 C   s   t | jtrt| jS | jS r6   )rS   r   rT   r   r9   r   r   r   r   P  s   
zURLResolver.urlconf_modulec              
   C   sP   t | jd| j}zt| W |S  ty' } zd}t|j| jd|d }~ww )NurlpatternszThe included URLconf '{name}' does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import.)r_   )rR   r   iterr   r	   r^   r   )r3   patternsr   r   r   r   r   rH   W  s   
zURLResolver.url_patternsc                 C   s8   t | jd| d }|sddlm} t |d| }t|S )Nr   r   )urls)rR   r   django.confr   r   )r3   	view_typer   r   r   r   r   r   f  s
   z!URLResolver.resolve_error_handlerc                 O   s   | j |dg|R i |S )Nr   )_reverse_with_prefix)r3   lookup_viewr$   r%   r   r   r   reverseo  s   zURLResolver.reversec                    s  |r rt d| js|   | j|}|D ]\}}}}	|D ]\}
}|r6t|t|kr.qtt||}nt 	|
|rAqt fdd| D rOq }i }d}| D ]&\}}||	v ryz|	| |||< W qY t yx   d}Y  nw t|||< qY|sq|dd|
 }tdt||f || rt|| td	 d
}t|    S qqt|dd }t|dd }|d ur|d urd||f }n|}dd |D }|r|rd|f }n	 rd  }nd}d||t||f }t|dd|i }t|)Nz2Don't mix *args and **kwargs in call to reverse()!c                 3   s$    | ]\}}  |||kV  qd S r6   )r   ro   r%   r   r   r     s   " z3URLResolver._reverse_with_prefix.<locals>.<genexpr>TF%z%%z^%s%sz/~:@)safer0   r!   z%s.%sc                 S   s   g | ]\}}}}|qS r   r   )r   _rK   r   r   r   r     s    z4URLResolver._reverse_with_prefix.<locals>.<listcomp>zarguments '%s'zkeyword arguments '%s'zno argumentsz;Reverse for '%s' with %s not found. %d pattern(s) tried: %szZReverse for '%(view)s' not found. '%(view)s' is not a valid view function or pattern name.view)r   r   r   r   r   r   rF   zipr   symmetric_difference
differenceanyrw   to_urlrT   replacer   rt   r   r   r   r   rR   r   )r3   r   _prefixr$   r%   possibilitiespossibilityrK   r   rG   resultparamscandidate_substext_candidate_subsrz   rp   rq   candidate_paturlmnlookup_view_sr   arg_msgr   r   r  r   r   r  sr   &
	z URLResolver._reverse_with_prefix)NNNr6   )r!   r0   r;   r5   r:   r   r   r   r   r   r   r   staticmethodr   r   r   r   r   r   rH   r   r  r   r   r   r   r   rD     s2    
=



&

	rD   r6   )F)6__doc__r   r   r   r   	importlibr   urllib.parser   asgiref.localr   r   r   django.core.checksr   r   django.core.checks.urlsr   django.core.exceptionsr	   r
   django.utils.datastructuresr   django.utils.functionalr   django.utils.httpr   r   django.utils.regex_helperr   r   django.utils.translationr   rG   r   
exceptionsr   r   utilsr   r   r@   	lru_cacher=   rM   rN   r\   rE   r   r   r   r   r   rD   r   r   r   r   <module>   sJ    
$



!:
.+"3