Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
django-rest-framework
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
django-rest-framework
Commits
ca7ba07b
Commit
ca7ba07b
authored
Jul 13, 2013
by
Alex Burgel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Introduce DynamicDetailRoute and DynamicListRoute to distinguish between different route types
parent
e14cbaf6
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
8 additions
and
18 deletions
+8
-18
rest_framework/routers.py
+8
-18
No files found.
rest_framework/routers.py
View file @
ca7ba07b
...
@@ -25,7 +25,9 @@ from rest_framework.reverse import reverse
...
@@ -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):
...
@@ -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):
...
@@ -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):
...
@@ -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):
...
@@ -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):
...
@@ -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
),
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment