
    _}dgc6                     N   d 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 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g d          Z edg d          Zd Zd Z G d d          Z G d de          Z G d dej                  Z G d de          Z dS )a  
Routers provide a convenient and consistent way of automatically
determining the URL conf for your API.

They are used by simply instantiating a Router class, and then registering
all the required ViewSets with that router.

For example, you might have a `urls.py` that looks something like this:

    router = routers.DefaultRouter()
    router.register('users', UserViewSet, 'user')
    router.register('accounts', AccountViewSet, 'account')

    urlpatterns = router.urls
    N)
namedtuple)ImproperlyConfigured)NoReverseMatchpathre_path)views)Response)reverse)SchemaGenerator)
SchemaView)api_settings)format_suffix_patternsRouteurlmappingnamedetail
initkwargsDynamicRouter   r   r   r   c                 V    |                      dd                               dd          S )zK
    Double brackets in regex of url_path for escape string formatting
    {z{{}z}})replace)url_paths    U/var/www/surfInsights/venv3-11/lib/python3.11/site-packages/rest_framework/routers.pyescape_curly_bracketsr   "   s*     C&&..sD999    c                     t          j        |  S )zX
    Takes an iterable of iterables, returns a single iterable containing all items
    )	itertoolschain)list_of_listss    r   flattenr$   )   s     ?M**r   c                   D    e Zd Zd ZddZd Zd Zd Zed             Z	dS )	
BaseRouterc                     g | _         d S N)registryselfs    r   __init__zBaseRouter.__init__1   s    r   Nc                     ||                      |          }|                     |          rd| d| d}t          |          | j                            |||f           t          | d          r| `d S d S )NzRouter with basename "zG" is already registered. Please provide a unique basename for viewset ""_urls)get_default_basenameis_already_registeredr   r)   appendhasattrr/   )r+   prefixviewsetbasenamemsgs        r   registerzBaseRouter.register4   s    0099H%%h// 	,OH O ODKO O OC&s+++fgx8999 4!! 	


	 	r   c                 D    t          fd| j        D                       S )z;
        Check if `basename` is already registered
        c              3   ,   K   | ]\  }}}|k    V  d S r(    ).0_prefix_viewsetr6   new_basenames       r   	<genexpr>z3BaseRouter.is_already_registered.<locals>.<genexpr>G   s/      ]]0K88|+]]]]]]r   )anyr)   )r+   r?   s    `r   r1   z BaseRouter.is_already_registeredC   s*     ]]]]t}]]]]]]r   c                      t          d          )q
        If `basename` is not specified, attempt to automatically determine
        it from the viewset.
        z'get_default_basename must be overriddenNotImplementedError)r+   r5   s     r   r0   zBaseRouter.get_default_basenameI   s    
 ""KLLLr   c                      t          d          )zO
        Return a list of URL patterns, given the registered viewsets.
        zget_urls must be overriddenrD   r*   s    r   get_urlszBaseRouter.get_urlsP   s     ""?@@@r   c                 b    t          | d          s|                                 | _        | j        S )Nr/   )r3   rG   r/   r*   s    r   urlszBaseRouter.urlsV   s+    tW%% 	)DJzr   r(   )
__name__
__module____qualname__r,   r8   r1   r0   rG   propertyrI   r;   r   r   r&   r&   0   s             ^ ^ ^M M MA A A   X  r   r&   c            
            e Zd Z eddddddddi	           ed
ddi            eddddddddddi	           edddi           gZd fd	Zd Zd Zd Z	d Z
d dZd Z xZS )!SimpleRouterz^{prefix}{trailing_slash}$listcreate)getpostz{basename}-listFsuffixListr   z%^{prefix}/{url_path}{trailing_slash}$z{basename}-{url_name}r   z#^{prefix}/{lookup}{trailing_slash}$retrieveupdatepartial_updatedestroy)rR   putpatchdeletez{basename}-detailTInstancez.^{prefix}/{lookup}/{url_path}{trailing_slash}$c                    |rdnd| _         || _        |rd| _        d| _        t          | _        nd| _        d| _        t          | _        g }| j        D ]^}|j        }|d         dk    r
|d	d          }|d
         dk    r
|d d
         }|	                    |
                    |                     _|| _        t                                                       d S )N/ z5(?P<{lookup_prefix}{lookup_url_kwarg}>{lookup_value})z[^/.]+z2<{lookup_value}:{lookup_prefix}{lookup_url_kwarg}>strr   ^   $)r   )trailing_slash
_use_regex_base_pattern_default_value_patternr   	_url_confr   routesr   r2   _replacesuperr,   )r+   rf   use_regex_path_routesroute	url_param	__class__s         r   r,   zSimpleRouter.__init__   s    %3;cc( 	"!XD*2D'$DNN!UD*/D'!DNG > >!I	Q<3&& )!""IR=C'' )#2#Iu~~)~<<====!DKr   c                     t          |dd          }|
J d            |j        j        j                                        S )rC   querysetNz`basename` argument not specified, and could not automatically determine the name from the viewset, as it does not have a `.queryset` attribute.)getattrmodel_metaobject_namelower)r+   r5   rt   s      r   r0   z!SimpleRouter.get_default_basename   sH    
 7J55## &8### ~#/55777r   c                 &   	 t          t          d  j        D                                 |                                }fd|D             }|r'd}t	          |d                    |          z            d |D             }d |D             }g } j        D ]u	t          	t                    r	j        r|	 fd|D             z  }1t          	t                    r	j        s|	 fd|D             z  }`|	                    	           v|S )	z
        Augment `self.routes` with any dynamically generated routes.

        Returns a list of the Route namedtuple.
        c                 j    g | ]0}t          |t                    |j                                        1S r;   )
isinstancer   r   values)r<   rp   s     r   
<listcomp>z+SimpleRouter.get_routes.<locals>.<listcomp>   s8    %r%r%rYcdikpYqYq%rem&:&:&<&<%r%r%rr   c                 0    g | ]}|j         v |j         S r;   )rJ   )r<   actionknown_actionss     r   r~   z+SimpleRouter.get_routes.<locals>.<listcomp>   s2     
 
 
 &-// O///r   zZCannot use the @action decorator on the following methods, as they are existing routes: %sz, c                      g | ]}|j         	|S r;   r   r<   r   s     r   r~   z+SimpleRouter.get_routes.<locals>.<listcomp>   s    NNNVN&NNNr   c                      g | ]}|j         	|S r;   r   r   s     r   r~   z+SimpleRouter.get_routes.<locals>.<listcomp>   s    PPP6&-PPPPr   c                 <    g | ]}                     |          S r;   _get_dynamic_router<   r   rp   r+   s     r   r~   z+SimpleRouter.get_routes.<locals>.<listcomp>   s)    ___f4225&AA___r   c                 <    g | ]}                     |          S r;   r   r   s     r   r~   z+SimpleRouter.get_routes.<locals>.<listcomp>   s)    ]]]f4225&AA]]]r   )
rP   r$   rk   get_extra_actionsr   joinr|   r   r   r2   )
r+   r5   extra_actionsnot_allowedr7   detail_actionslist_actionsrk   r   rp   s
   `       @@r   
get_routeszSimpleRouter.get_routes   sp    W%r%r$+%r%r%rsstt1133
 
 
 
*7
 
 
  	E>C&sTYY{-C-C'CDDD ON}NNNPP]PPP[ 	% 	%E%.. %5< %_____P^____E<00 % %]]]]]P\]]]]e$$$$r   c                 :   |j                                         }|                    |j                   t	          |j                  }t          |j                            d|          |j	        |j
                            d|j                  |j        |          S )Nz
{url_path}z
{url_name}r   )r   copyrW   kwargsr   r   r   r   r   r   r   url_namer   )r+   rp   r   r   r   s        r   r   zSimpleRouter._get_dynamic_route   s    %**,,
&-((((99	!!,99N##L&/BB<!
 
 
 	
r   c                 h    i }|                                 D ]\  }}t          ||          r|||<   |S )z
        Given a viewset, and a mapping of http methods to actions,
        return a new mapping which only includes any mappings that
        are actually implemented by the viewset.
        )itemsr3   )r+   r5   
method_mapbound_methodsmethodr   s         r   get_method_mapzSimpleRouter.get_method_map   sK     (..00 	/ 	/NFFw'' /(.f%r   r`   c                     t          |dd          }t          |dd          p|}d}| j        st          |dd          }|t          |d| j                  }| j                            |||          S )a  
        Given a viewset, return the portion of URL regex that is used
        to match against a single instance.

        Note that lookup_prefix is not used directly inside REST rest_framework
        itself, but is required in order to nicely support nested router
        implementations, such as drf-nested-routers.

        https://github.com/alanjds/drf-nested-routers
        lookup_fieldpklookup_url_kwargNlookup_value_converterlookup_value_regex)lookup_prefixr   lookup_value)ru   rg   ri   rh   format)r+   r5   r   r   r   r   s         r   get_lookup_regexzSimpleRouter.get_lookup_regex   s     w=="7,>EEU 	L"7,DdKKL"7,@$B]^^L!(('-% ) 
 
 	
r   c           	         g }| j         D ]D\  }}}|                     |          }|                     |          }|D ]}|                     ||j                  }|s!|j                            ||| j                  }	|s@| j        t          u r|	d         dk    r
|	dd         }	n|	dd         dk    rd|	dd         z   }	|j
                                        }
|
                    ||j        d	            |j        |fi |
}|j                            |
          }|                    |                     |	||                     F|S )zQ
        Use the registered viewsets to generate a list of URL patterns.
        )r4   lookuprf   r   r_   rc   N   z^/rb   )r6   r   r6   r   )r)   r   r   r   r   r   r   rf   rj   r   r   r   rW   r   as_viewr   r2   )r+   retr4   r5   r6   r   rk   rp   r   regexr   viewr   s                r   rG   zSimpleRouter.get_urls
  s    )- %	C %	C%FGX**733F__W--F !C !C --gu}EE  	((!!#'#6 )    0~-- 8s??$)!""IErrd** #eABBi"-2244
!! (#l# #   
 'ww==*==z((((;;

4>>%D>AABBBBC!CF 
r   )TT)r`   )rJ   rK   rL   r   r   rk   r,   r0   r   r   r   r   rG   __classcell__rr   s   @r   rO   rO   ]   sb        	-   # &)		
 		
 		
 	8(		
 	
 	
 	6!)#	  % *-	
 	
 	
 	A(		
 	
 	
G)FV     28 8 8" " "H
 
 

 
 

 
 
 
8- - - - - - -r   rO   c                   $    e Zd ZdZdZdZdZd ZdS )APIRootViewz7
    The default basic root view for DefaultRouter
    TNc           
         i }|j         j        }| j                                        D ]J\  }}|r|dz   |z   }	 t	          |||||                    d                    ||<   ;# t          $ r Y Gw xY wt          |          S )N:r   )argsr   requestr   )resolver_match	namespaceapi_root_dictr   r
   rR   r   r	   )r+   r   r   r   r   r   keyr   s           r   rR   zAPIRootView.getB  s    *4	!/5577 	 	MC 6$s?X5
"!#!::h//  C "    }}s   *A##
A0/A0)rJ   rK   rL   __doc___ignore_model_permissionsschemar   rR   r;   r   r   r   r   :  s?          !%FM    r   r   c                   P     e Zd ZdZdZdZdZdZeZe	Z
eZ fdZddZ fdZ xZS )	DefaultRouterz
    The default router extends the SimpleRouter, but also adds in a default
    API root view, and adds format suffix patterns to the URLs.
    Tzapi-rootNc                     d|v r|                     d          | _        nt          t          j                  | _         t                      j        |i | d S )Nroot_renderers)popr   rP   r   DEFAULT_RENDERER_CLASSESrm   r,   )r+   r   r   rr   s      r   r,   zDefaultRouter.__init__e  s[    v%%"(**-=">">D"&|'L"M"MD$)&)))))r   c                     i }| j         d         j        }| j        D ]\  }}}|                    |          ||<    | j                            |          S )z+
        Return a basic root view.
        r   r   )r   )rk   r   r)   r   r   r   )r+   api_urlsr   	list_namer4   r5   r6   s          r   get_api_root_viewzDefaultRouter.get_api_root_viewl  si     KN'	)- 	H 	H%FGX$-$4$4h$4$G$GM&!!''m'DDDr   c                    t                                                      }| j        rB|                     |          }t	          d|| j                  }|                    |           | j        rt          |          }|S )z
        Generate the list of URL patterns, including a default root view
        for the API, and appending `.json` style format suffixes.
        )r   r`   r   )	rm   rG   include_root_viewr   r   root_view_namer2   include_format_suffixesr   )r+   rI   r   root_urlrr   s       r   rG   zDefaultRouter.get_urlsw  s    
 ww!!! 	"))4)88DB4+>???HKK!!!' 	0)$//Dr   r(   )rJ   rK   rL   r   r   r   r   default_schema_renderersr   r   APISchemaViewr   r,   r   rG   r   r   s   @r   r   r   X  s          "N#KM%O* * * * *	E 	E 	E 	E        r   r   )!r   r!   collectionsr   django.core.exceptionsr   django.urlsr   r   r   rest_frameworkr   rest_framework.responser	   rest_framework.reverser
   rest_framework.schemasr   rest_framework.schemas.viewsr   rest_framework.settingsr   rest_framework.urlpatternsr   r   r   r   r$   r&   rO   APIViewr   r   r;   r   r   <module>r      s        " " " " " " 7 7 7 7 7 7 5 5 5 5 5 5 5 5 5 5             , , , , , , * * * * * * 2 2 2 2 2 2 3 3 3 3 3 3 0 0 0 0 0 0 = = = = = =
7NNNOOz.*Q*Q*QRR: : :+ + +* * * * * * * *ZZ Z Z Z Z: Z Z Zz    %-   <. . . . .L . . . . .r   