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
d72603bc
Commit
d72603bc
authored
Jun 05, 2013
by
Alex Burgel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add support for collection routes to SimpleRouter
parent
82145e2b
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
103 additions
and
4 deletions
+103
-4
rest_framework/decorators.py
+26
-0
rest_framework/routers.py
+30
-3
rest_framework/tests/test_routers.py
+47
-1
No files found.
rest_framework/decorators.py
View file @
d72603bc
...
@@ -113,6 +113,7 @@ def link(**kwargs):
...
@@ -113,6 +113,7 @@ def link(**kwargs):
"""
"""
def
decorator
(
func
):
def
decorator
(
func
):
func
.
bind_to_methods
=
[
'get'
]
func
.
bind_to_methods
=
[
'get'
]
func
.
collection
=
False
func
.
kwargs
=
kwargs
func
.
kwargs
=
kwargs
return
func
return
func
return
decorator
return
decorator
...
@@ -124,6 +125,31 @@ def action(methods=['post'], **kwargs):
...
@@ -124,6 +125,31 @@ def action(methods=['post'], **kwargs):
"""
"""
def
decorator
(
func
):
def
decorator
(
func
):
func
.
bind_to_methods
=
methods
func
.
bind_to_methods
=
methods
func
.
collection
=
False
func
.
kwargs
=
kwargs
return
func
return
decorator
def
collection_link
(
**
kwargs
):
"""
Used to mark a method on a ViewSet that should be routed for GET requests.
"""
def
decorator
(
func
):
func
.
bind_to_methods
=
[
'get'
]
func
.
collection
=
True
func
.
kwargs
=
kwargs
return
func
return
decorator
def
collection_action
(
methods
=
[
'post'
],
**
kwargs
):
"""
Used to mark a method on a ViewSet that should be routed for POST requests.
"""
def
decorator
(
func
):
func
.
bind_to_methods
=
methods
func
.
collection
=
True
func
.
kwargs
=
kwargs
func
.
kwargs
=
kwargs
return
func
return
func
return
decorator
return
decorator
rest_framework/routers.py
View file @
d72603bc
...
@@ -88,6 +88,17 @@ class SimpleRouter(BaseRouter):
...
@@ -88,6 +88,17 @@ class SimpleRouter(BaseRouter):
name
=
'{basename}-list'
,
name
=
'{basename}-list'
,
initkwargs
=
{
'suffix'
:
'List'
}
initkwargs
=
{
'suffix'
:
'List'
}
),
),
# Dynamically generated collection routes.
# Generated using @collection_action or @collection_link decorators
# on methods of the viewset.
Route
(
url
=
r'^{prefix}/{methodname}{trailing_slash}$'
,
mapping
=
{
'{httpmethod}'
:
'{methodname}'
,
},
name
=
'{basename}-collection-{methodnamehyphen}'
,
initkwargs
=
{}
),
# Detail route.
# Detail route.
Route
(
Route
(
url
=
r'^{prefix}/{lookup}{trailing_slash}$'
,
url
=
r'^{prefix}/{lookup}{trailing_slash}$'
,
...
@@ -107,7 +118,7 @@ class SimpleRouter(BaseRouter):
...
@@ -107,7 +118,7 @@ class SimpleRouter(BaseRouter):
mapping
=
{
mapping
=
{
'{httpmethod}'
:
'{methodname}'
,
'{httpmethod}'
:
'{methodname}'
,
},
},
name
=
'{basename}-{methodnamehyphen}'
,
name
=
'{basename}-
dynamic-
{methodnamehyphen}'
,
initkwargs
=
{}
initkwargs
=
{}
),
),
]
]
...
@@ -142,20 +153,25 @@ class SimpleRouter(BaseRouter):
...
@@ -142,20 +153,25 @@ class SimpleRouter(BaseRouter):
known_actions
=
flatten
([
route
.
mapping
.
values
()
for
route
in
self
.
routes
])
known_actions
=
flatten
([
route
.
mapping
.
values
()
for
route
in
self
.
routes
])
# Determine any `@action` or `@link` decorated methods on the viewset
# Determine any `@action` or `@link` decorated methods on the viewset
collection_routes
=
[]
dynamic_routes
=
[]
dynamic_routes
=
[]
for
methodname
in
dir
(
viewset
):
for
methodname
in
dir
(
viewset
):
attr
=
getattr
(
viewset
,
methodname
)
attr
=
getattr
(
viewset
,
methodname
)
httpmethods
=
getattr
(
attr
,
'bind_to_methods'
,
None
)
httpmethods
=
getattr
(
attr
,
'bind_to_methods'
,
None
)
collection
=
getattr
(
attr
,
'collection'
,
False
)
if
httpmethods
:
if
httpmethods
:
if
methodname
in
known_actions
:
if
methodname
in
known_actions
:
raise
ImproperlyConfigured
(
'Cannot use @action or @link decorator on '
raise
ImproperlyConfigured
(
'Cannot use @action or @link decorator on '
'method "
%
s" as it is an existing route'
%
methodname
)
'method "
%
s" as it is an existing route'
%
methodname
)
httpmethods
=
[
method
.
lower
()
for
method
in
httpmethods
]
httpmethods
=
[
method
.
lower
()
for
method
in
httpmethods
]
dynamic_routes
.
append
((
httpmethods
,
methodname
))
if
collection
:
collection_routes
.
append
((
httpmethods
,
methodname
))
else
:
dynamic_routes
.
append
((
httpmethods
,
methodname
))
ret
=
[]
ret
=
[]
for
route
in
self
.
routes
:
for
route
in
self
.
routes
:
if
route
.
mapping
==
{
'{httpmethod}'
:
'{methodname}'
}
:
if
route
.
name
==
'{basename}-dynamic-{methodnamehyphen}'
:
# Dynamic routes (@link or @action decorator)
# Dynamic routes (@link or @action decorator)
for
httpmethods
,
methodname
in
dynamic_routes
:
for
httpmethods
,
methodname
in
dynamic_routes
:
initkwargs
=
route
.
initkwargs
.
copy
()
initkwargs
=
route
.
initkwargs
.
copy
()
...
@@ -166,6 +182,17 @@ class SimpleRouter(BaseRouter):
...
@@ -166,6 +182,17 @@ class SimpleRouter(BaseRouter):
name
=
replace_methodname
(
route
.
name
,
methodname
),
name
=
replace_methodname
(
route
.
name
,
methodname
),
initkwargs
=
initkwargs
,
initkwargs
=
initkwargs
,
))
))
elif
route
.
name
==
'{basename}-collection-{methodnamehyphen}'
:
# Dynamic routes (@collection_link or @collection_action decorator)
for
httpmethods
,
methodname
in
collection_routes
:
initkwargs
=
route
.
initkwargs
.
copy
()
initkwargs
.
update
(
getattr
(
viewset
,
methodname
)
.
kwargs
)
ret
.
append
(
Route
(
url
=
replace_methodname
(
route
.
url
,
methodname
),
mapping
=
dict
((
httpmethod
,
methodname
)
for
httpmethod
in
httpmethods
),
name
=
replace_methodname
(
route
.
name
,
methodname
),
initkwargs
=
initkwargs
,
))
else
:
else
:
# Standard route
# Standard route
ret
.
append
(
route
)
ret
.
append
(
route
)
...
...
rest_framework/tests/test_routers.py
View file @
d72603bc
...
@@ -4,7 +4,7 @@ from django.test import TestCase
...
@@ -4,7 +4,7 @@ from django.test import TestCase
from
django.core.exceptions
import
ImproperlyConfigured
from
django.core.exceptions
import
ImproperlyConfigured
from
rest_framework
import
serializers
,
viewsets
,
permissions
from
rest_framework
import
serializers
,
viewsets
,
permissions
from
rest_framework.compat
import
include
,
patterns
,
url
from
rest_framework.compat
import
include
,
patterns
,
url
from
rest_framework.decorators
import
link
,
action
from
rest_framework.decorators
import
link
,
action
,
collection_link
,
collection_action
from
rest_framework.response
import
Response
from
rest_framework.response
import
Response
from
rest_framework.routers
import
SimpleRouter
,
DefaultRouter
from
rest_framework.routers
import
SimpleRouter
,
DefaultRouter
from
rest_framework.test
import
APIRequestFactory
from
rest_framework.test
import
APIRequestFactory
...
@@ -214,3 +214,49 @@ class TestActionAppliedToExistingRoute(TestCase):
...
@@ -214,3 +214,49 @@ class TestActionAppliedToExistingRoute(TestCase):
with
self
.
assertRaises
(
ImproperlyConfigured
):
with
self
.
assertRaises
(
ImproperlyConfigured
):
self
.
router
.
urls
self
.
router
.
urls
class
StaticAndDynamicViewSet
(
viewsets
.
ViewSet
):
def
list
(
self
,
request
,
*
args
,
**
kwargs
):
return
Response
({
'method'
:
'list'
})
@collection_action
()
def
collection_action
(
self
,
request
,
*
args
,
**
kwargs
):
return
Response
({
'method'
:
'action1'
})
@action
()
def
dynamic_action
(
self
,
request
,
*
args
,
**
kwargs
):
return
Response
({
'method'
:
'action2'
})
@collection_link
()
def
collection_link
(
self
,
request
,
*
args
,
**
kwargs
):
return
Response
({
'method'
:
'link1'
})
@link
()
def
dynamic_link
(
self
,
request
,
*
args
,
**
kwargs
):
return
Response
({
'method'
:
'link2'
})
class
TestStaticAndDynamicRouter
(
TestCase
):
def
setUp
(
self
):
self
.
router
=
SimpleRouter
()
def
test_link_and_action_decorator
(
self
):
routes
=
self
.
router
.
get_routes
(
StaticAndDynamicViewSet
)
decorator_routes
=
[
r
for
r
in
routes
if
not
(
r
.
name
.
endswith
(
'-list'
)
or
r
.
name
.
endswith
(
'-detail'
))]
# Make sure all these endpoints exist and none have been clobbered
for
i
,
endpoint
in
enumerate
([
'collection_action'
,
'collection_link'
,
'dynamic_action'
,
'dynamic_link'
]):
route
=
decorator_routes
[
i
]
# check url listing
if
endpoint
.
startswith
(
'collection_'
):
self
.
assertEqual
(
route
.
url
,
'^{{prefix}}/{0}{{trailing_slash}}$'
.
format
(
endpoint
))
else
:
self
.
assertEqual
(
route
.
url
,
'^{{prefix}}/{{lookup}}/{0}{{trailing_slash}}$'
.
format
(
endpoint
))
# check method to function mapping
if
endpoint
.
endswith
(
'action'
):
method_map
=
'post'
else
:
method_map
=
'get'
self
.
assertEqual
(
route
.
mapping
[
method_map
],
endpoint
)
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