mirror of
				https://github.com/encode/django-rest-framework.git
				synced 2025-10-30 23:47:53 +03:00 
			
		
		
		
	Introduce DynamicDetailRoute and DynamicListRoute to distinguish between different route types
This commit is contained in:
		
							parent
							
								
									e14cbaf696
								
							
						
					
					
						commit
						ca7ba07b4e
					
				|  | @ -25,7 +25,9 @@ from rest_framework.reverse import reverse | ||||||
| from rest_framework.urlpatterns import format_suffix_patterns | from rest_framework.urlpatterns import format_suffix_patterns | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| Route = namedtuple('Route', ['key', 'url', 'mapping', 'name', 'initkwargs']) | Route = namedtuple('Route', ['url', 'mapping', 'name', 'initkwargs']) | ||||||
|  | DynamicDetailRoute = namedtuple('DynamicDetailRoute', ['url', 'name', 'initkwargs']) | ||||||
|  | DynamicListRoute = namedtuple('DynamicListRoute', ['url', 'name', 'initkwargs']) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| def replace_methodname(format_string, methodname): | def replace_methodname(format_string, methodname): | ||||||
|  | @ -80,7 +82,6 @@ class SimpleRouter(BaseRouter): | ||||||
|     routes = [ |     routes = [ | ||||||
|         # List route. |         # List route. | ||||||
|         Route( |         Route( | ||||||
|             key='list', |  | ||||||
|             url=r'^{prefix}{trailing_slash}$', |             url=r'^{prefix}{trailing_slash}$', | ||||||
|             mapping={ |             mapping={ | ||||||
|                 'get': 'list', |                 'get': 'list', | ||||||
|  | @ -92,18 +93,13 @@ class SimpleRouter(BaseRouter): | ||||||
|         # Dynamically generated list routes. |         # Dynamically generated list routes. | ||||||
|         # Generated using @list_action or @list_link decorators |         # Generated using @list_action or @list_link decorators | ||||||
|         # on methods of the viewset. |         # on methods of the viewset. | ||||||
|         Route( |         DynamicListRoute( | ||||||
|             key='collection', |  | ||||||
|             url=r'^{prefix}/{methodname}{trailing_slash}$', |             url=r'^{prefix}/{methodname}{trailing_slash}$', | ||||||
|             mapping={ |  | ||||||
|                 '{httpmethod}': '{methodname}', |  | ||||||
|             }, |  | ||||||
|             name='{basename}-{methodnamehyphen}', |             name='{basename}-{methodnamehyphen}', | ||||||
|             initkwargs={} |             initkwargs={} | ||||||
|         ), |         ), | ||||||
|         # Detail route. |         # Detail route. | ||||||
|         Route( |         Route( | ||||||
|             key='detail', |  | ||||||
|             url=r'^{prefix}/{lookup}{trailing_slash}$', |             url=r'^{prefix}/{lookup}{trailing_slash}$', | ||||||
|             mapping={ |             mapping={ | ||||||
|                 'get': 'retrieve', |                 'get': 'retrieve', | ||||||
|  | @ -116,12 +112,8 @@ class SimpleRouter(BaseRouter): | ||||||
|         ), |         ), | ||||||
|         # Dynamically generated detail routes. |         # Dynamically generated detail routes. | ||||||
|         # Generated using @action or @link decorators on methods of the viewset. |         # Generated using @action or @link decorators on methods of the viewset. | ||||||
|         Route( |         DynamicDetailRoute( | ||||||
|             key='dynamic', |  | ||||||
|             url=r'^{prefix}/{lookup}/{methodname}{trailing_slash}$', |             url=r'^{prefix}/{lookup}/{methodname}{trailing_slash}$', | ||||||
|             mapping={ |  | ||||||
|                 '{httpmethod}': '{methodname}', |  | ||||||
|             }, |  | ||||||
|             name='{basename}-{methodnamehyphen}', |             name='{basename}-{methodnamehyphen}', | ||||||
|             initkwargs={} |             initkwargs={} | ||||||
|         ), |         ), | ||||||
|  | @ -154,7 +146,7 @@ class SimpleRouter(BaseRouter): | ||||||
|         Returns a list of the Route namedtuple. |         Returns a list of the Route namedtuple. | ||||||
|         """ |         """ | ||||||
| 
 | 
 | ||||||
|         known_actions = flatten([route.mapping.values() for route in self.routes]) |         known_actions = flatten([route.mapping.values() for route in self.routes if isinstance(route, Route)]) | ||||||
| 
 | 
 | ||||||
|         # Determine any `@action` or `@link` decorated methods on the viewset |         # Determine any `@action` or `@link` decorated methods on the viewset | ||||||
|         detail_routes = [] |         detail_routes = [] | ||||||
|  | @ -176,25 +168,23 @@ class SimpleRouter(BaseRouter): | ||||||
| 
 | 
 | ||||||
|         ret = [] |         ret = [] | ||||||
|         for route in self.routes: |         for route in self.routes: | ||||||
|             if route.key == 'dynamic': |             if isinstance(route, DynamicDetailRoute): | ||||||
|                 # Dynamic detail routes (@link or @action decorator) |                 # Dynamic detail routes (@link or @action decorator) | ||||||
|                 for httpmethods, methodname in detail_routes: |                 for httpmethods, methodname in detail_routes: | ||||||
|                     initkwargs = route.initkwargs.copy() |                     initkwargs = route.initkwargs.copy() | ||||||
|                     initkwargs.update(getattr(viewset, methodname).kwargs) |                     initkwargs.update(getattr(viewset, methodname).kwargs) | ||||||
|                     ret.append(Route( |                     ret.append(Route( | ||||||
|                         key=route.key, |  | ||||||
|                         url=replace_methodname(route.url, methodname), |                         url=replace_methodname(route.url, methodname), | ||||||
|                         mapping=dict((httpmethod, methodname) for httpmethod in httpmethods), |                         mapping=dict((httpmethod, methodname) for httpmethod in httpmethods), | ||||||
|                         name=replace_methodname(route.name, methodname), |                         name=replace_methodname(route.name, methodname), | ||||||
|                         initkwargs=initkwargs, |                         initkwargs=initkwargs, | ||||||
|                     )) |                     )) | ||||||
|             elif route.key == 'collection': |             elif isinstance(route, DynamicListRoute): | ||||||
|                 # Dynamic list routes (@list_link or @list_action decorator) |                 # Dynamic list routes (@list_link or @list_action decorator) | ||||||
|                 for httpmethods, methodname in list_routes: |                 for httpmethods, methodname in list_routes: | ||||||
|                     initkwargs = route.initkwargs.copy() |                     initkwargs = route.initkwargs.copy() | ||||||
|                     initkwargs.update(getattr(viewset, methodname).kwargs) |                     initkwargs.update(getattr(viewset, methodname).kwargs) | ||||||
|                     ret.append(Route( |                     ret.append(Route( | ||||||
|                         key=route.key, |  | ||||||
|                         url=replace_methodname(route.url, methodname), |                         url=replace_methodname(route.url, methodname), | ||||||
|                         mapping=dict((httpmethod, methodname) for httpmethod in httpmethods), |                         mapping=dict((httpmethod, methodname) for httpmethod in httpmethods), | ||||||
|                         name=replace_methodname(route.name, methodname), |                         name=replace_methodname(route.name, methodname), | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user