o
    hl                     @   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   ]/var/www/html/magazine_api/magazine_env/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_pathZ	view_name)
selfr#   r$   r%   r&   r)   r,   r'   r(   Z	view_pathr   r   r   __init__!   s   

zResolverMatch.__init__c                 C   s   | j | j| jf| S N)r#   r$   r%   )r2   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'   r2   r   r   r   __repr__=   s   zResolverMatch.__repr__)NNNNN)r!   r0   __qualname__r3   r6   r8   r   r   r   r   r       s    
r   c                 C   s   | d u rt j} t| S r4   )r   ZROOT_URLCONF_get_cached_resolverZ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 r>   )rA   dict
convertersr@   url_patterns)Z
ns_patternresolverrC   patternZns_resolverr   r   r   get_ns_resolverO   s   
rG   c                   @   s   e Zd Zdd ZdddZdS )LocaleRegexDescriptorc                 C   s
   || _ d S r4   )attr)r2   rI   r   r   r   r3   [      
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)getattrrI   
isinstancestr_compile__dict__r   _regex_dict)r2   instanceclsrF   language_coder   r   r   __get__^   s   



zLocaleRegexDescriptor.__get__r4   )r!   r0   r9   r3   rU   r   r   r   r   rH   Z   s    rH   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)r2   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.
        )/r?   z^\/r\   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)	rK   rF   r   ZAPPEND_SLASH
startswithendswithr   rX   r[   )r2   regex_patternwarningr   r   r   _check_pattern_startswith_slash{   s   z-CheckURLMixin._check_pattern_startswith_slashN)r!   r0   r9   r[   rc   r   r   r   r   rV   q   s    	rV   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 )rA   _regexNFc                 C   s"   || _ i | _|| _|| _i | _d S r4   )rd   rQ   _is_endpointrY   rC   )r2   rK   rY   is_endpointr   r   r   r3      s
   
zRegexPattern.__init__c                 C   sT   | j |}|r(| }|rdn| }dd | D }|| d  ||fS d S )Nr   c                 S   s   i | ]\}}|d ur||qS r4   r   r   kvr   r   r   
<dictcomp>   s    z&RegexPattern.match.<locals>.<dictcomp>)rK   search	groupdictgroupsitemsend)r2   pathmatchr%   r$   r   r   r   rq      s   zRegexPattern.matchc                 C   s*   g }| |   | js| |   |S r4   )extendrc   re   _check_include_trailing_dollarr2   warningsr   r   r   check   s
   zRegexPattern.checkc                 C   s8   | j j}|dr|dstd|  ddgS g S )N$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.W001r]   )rK   rF   r`   r   rX   r[   )r2   ra   r   r   r   rs      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	   )r2   rK   er   r   r   rO      s   
zRegexPattern._compilec                 C   
   t | jS r4   )rN   rd   r7   r   r   r   __str__   rJ   zRegexPattern.__str__NF)r!   r0   r9   rH   rK   r3   rq   rv   rs   rO   r}   r   r   r   r   rA      s    
	rA   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.	converterrN   z'URL route %r uses invalid converter %r.(?P<>)rw    )_PATH_PARAMETER_COMPONENT_RErk   appendrx   escapesetgroup
isdisjointstring
whitespacer	   startro   isidentifierr   KeyErrorrK   r*   )
r'   rf   Zoriginal_routepartsrC   rq   r   Z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   rQ   re   rY   r   rN   rC   )r2   r'   rY   rf   r   r   r   r3      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   )rK   rk   rl   rn   rC   Z	to_python
ValueErrorro   )r2   rp   rq   r%   keyvaluer   r   r   r   rq     s   
zRoutePattern.matchc                 C   sJ   |   }| j}d|v s|ds|dr#|td|  dd |S )Nr   r   rw   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.W001r]   )rc   r   r_   r`   r   r   rX   r[   )r2   ru   r'   r   r   r   rv     s   
zRoutePattern.checkc                 C   s   t t|| jd S )Nr   )rx   ry   r   re   )r2   r'   r   r   r   rO        zRoutePattern._compilec                 C   r|   r4   )rN   r   r7   r   r   r   r}   "  rJ   zRoutePattern.__str__r~   )
r!   r0   r9   rH   rK   r3   rq   rv   rO   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 r4   )prefix_default_languagerC   )r2   r   r   r   r   r3   '  s   
zLocalePrefixPattern.__init__c                 C   s   t | jS r4   )rx   ry   language_prefixr7   r   r   r   rK   +  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   ZLANGUAGE_CODEr   r2   rT   r   r   r   r   0  s   z#LocalePrefixPattern.language_prefixc                 C   s*   | j }||r|t|d  di fS d S r   )r   r_   len)r2   rp   r   r   r   r   rq   8  s   
zLocalePrefixPattern.matchc                 C   s   g S r4   r   r7   r   r   r   rv   >  s   zLocalePrefixPattern.checkc                 C   s
   d | S )NrW   )rX   r7   r   r   r   r[   A  rJ   zLocalePrefixPattern.describec                 C   s   | j S r4   )r   r7   r   r   r   r}   D  s   zLocalePrefixPattern.__str__N)T)r!   r0   r9   r3   propertyrK   r   rq   rv   r[   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 r4   )rF   callbackdefault_argsrY   )r2   rF   r   r   rY   r   r   r   r3   I  s   

zURLPattern.__init__c                 C   s   d| j j| j f S )Nz<%s %s>)r/   r!   rF   r[   r7   r   r   r   r8   O  r   zURLPattern.__repr__c                 C   s   |   }|| j  |S r4   )_check_pattern_namerr   rF   rv   rt   r   r   r   rv   R  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.W003r]   )rF   rY   r   rX   r[   )r2   rb   r   r   r   r   W  s   zURLPattern._check_pattern_namec                 C   sH   | j |}|r"|\}}}|| j t| j||| j jt| j dS d S )N)r'   )rF   rq   updater   r   r   rY   rN   )r2   rp   rq   new_pathr$   r%   r   r   r   resolvee  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   rM   	functoolspartialr#   r.   r0   r/   r!   r9   )r2   r   r   r   r   
lookup_strm  s   
zURLPattern.lookup_str)NN)
r!   r0   r9   r3   r8   rv   r   r   r   r   r   r   r   r   r   H  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 )&r@   Nc                 C   sT   || _ || _d | _|pi | _|| _|| _i | _i | _i | _t	 | _
d| _t | _d S r~   )rF   urlconf_namer   default_kwargsr-   r+   _reverse_dict_namespace_dict	_app_dictr   _callback_strs
_populatedr   _local)r2   rF   r   r   r+   r-   r   r   r   r3   |  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>)
rM   r   listr/   r!   reprr+   r-   rF   r[   )r2   Zurlconf_reprr   r   r   r8     s   
zURLResolver.__repr__c                 C   s:   g }| j D ]	}|t| q||   |p| j S r4   )rD   rr   r   _check_custom_error_handlersrF   rv   )r2   messagesrF   r   r   r   rv     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_coderp   z	urls.E008)hintr^   zeThe custom handler{status_code} view '{path}' does not take the correct number of arguments ({args}).r"   r   zrequest, exceptionrequest)r   rp   r$   z	urls.E007r]   )resolve_error_handlerImportErrorr
   rL   urlconf_modulerX   r   r   rN   inspect	signaturebind	TypeErrorr0   r9   )
r2   r   r   Znum_parametershandlerr{   rp   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   )&rL   r   r   r   r   reversedrD   rF   rK   r_   rM   r   r   addr   r   Z
appendlistr   r   rC   rY   	_populater+   
setdefaultr   r-   reverse_dictgetlistr   namespace_dictrn   r   app_dictrr   r   r   r   r   )r2   Zlookupsr,   ZappsrT   Zurl_patternZ	p_patternbitsrY   matchespatdefaultsrC   Znew_matchesr-   prefixZsub_patternZcurrent_convertersr+   Znamespace_listr   r   r   r     sj   







zURLResolver._populatec                 C   "   t  }|| jvr|   | j| S r4   )r   r   r   r   r   r   r   r        

zURLResolver.reverse_dictc                 C   r   r4   )r   r   r   r   r   r   r   r     r   zURLResolver.namespace_dictc                 C   r   r4   )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 r4   r   )r   trF   r   r   	<genexpr>  s    z,URLResolver._extend_tried.<locals>.<genexpr>)r   rr   )r(   rF   Z	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)r_   )Zroute1Zroute2r   r   r   _join_route  s
   
zURLResolver._join_routec                 C   s   | j s|   || jv S r4   )r   r   r   )r2   rY   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(   rp   rp   )rN   rF   rq   rD   r   r   r   r$   getr   r   r%   rM   r   r(   r   r#   r&   r+   r)   r-   r,   r   r'   r   )r2   rp   r(   rq   r   r$   r%   rF   Z	sub_matchr{   Zsub_match_dictZsub_match_argsZcurrent_router   r   r   r   &  sB   

&

zURLResolver.resolvec                 C   s   t | jtrt| jS | jS r4   )rM   r   rN   r   r7   r   r   r   r   L  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 )NZ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.)rY   )rL   r   iterr   r	   rX   r   )r2   patternsr{   r   r   r   r   rD   S  s   
zURLResolver.url_patternsc                 C   s8   t | jd| d }|sddlm} t |d| }t|S )Nr   r   )urls)rL   r   django.confr   r   )r2   Z	view_typer   r   r   r   r   r   b  s
   z!URLResolver.resolve_error_handlerc                 O   s   | j |dg|R i |S )Nr   )_reverse_with_prefix)r2   lookup_viewr$   r%   r   r   r   reversek  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 r4   )r   rg   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   _rF   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   rB   zipr   symmetric_difference
differenceanyrn   Zto_urlrN   replacerx   rk   r   r   r   r   rL   r   )r2   r   _prefixr$   r%   possibilitiesZpossibilityrF   r   rC   resultparamsZcandidate_subsZtext_candidate_subsrq   rh   ri   Zcandidate_paturlmnZlookup_view_sr   Zarg_msgr   r   r   r   r   n  sr   &
	z URLResolver._reverse_with_prefix)NNNr4   )r!   r0   r9   r3   r8   rv   r   r   r   r   r   r   staticmethodr   r   r   r   r   r   rD   r   r   r   r   r   r   r   r@   {  s2    
=



&

	r@   r4   )F)6__doc__r   r   rx   r   	importlibr   urllib.parser   Zasgiref.localr   r   r   Zdjango.core.checksr   r   Zdjango.core.checks.urlsr   Zdjango.core.exceptionsr	   r
   Zdjango.utils.datastructuresr   Zdjango.utils.functionalr   Zdjango.utils.httpr   r   Zdjango.utils.regex_helperr   r   Zdjango.utils.translationr   rC   r   
exceptionsr   r   utilsr   r   r<   	lru_cacher:   rG   rH   rV   rA   r   r   r   r   r   r@   r   r   r   r   <module>   sJ    
$



!6
.+"3