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
112b52f5
Commit
112b52f5
authored
May 24, 2013
by
Alex Burgel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Allow action decorator to handle multiple http methods
parent
02b29267
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
20 additions
and
15 deletions
+20
-15
rest_framework/decorators.py
+3
-3
rest_framework/routers.py
+5
-5
rest_framework/tests/routers.py
+12
-7
No files found.
rest_framework/decorators.py
View file @
112b52f5
...
@@ -112,18 +112,18 @@ def link(**kwargs):
...
@@ -112,18 +112,18 @@ def link(**kwargs):
Used to mark a method on a ViewSet that should be routed for GET requests.
Used to mark a method on a ViewSet that should be routed for GET requests.
"""
"""
def
decorator
(
func
):
def
decorator
(
func
):
func
.
bind_to_method
=
'get'
func
.
bind_to_method
s
=
[
'get'
]
func
.
kwargs
=
kwargs
func
.
kwargs
=
kwargs
return
func
return
func
return
decorator
return
decorator
def
action
(
**
kwargs
):
def
action
(
methods
=
[
'post'
],
**
kwargs
):
"""
"""
Used to mark a method on a ViewSet that should be routed for POST requests.
Used to mark a method on a ViewSet that should be routed for POST requests.
"""
"""
def
decorator
(
func
):
def
decorator
(
func
):
func
.
bind_to_method
=
'post'
func
.
bind_to_method
s
=
methods
func
.
kwargs
=
kwargs
func
.
kwargs
=
kwargs
return
func
return
func
return
decorator
return
decorator
rest_framework/routers.py
View file @
112b52f5
...
@@ -131,20 +131,20 @@ class SimpleRouter(BaseRouter):
...
@@ -131,20 +131,20 @@ class SimpleRouter(BaseRouter):
dynamic_routes
=
[]
dynamic_routes
=
[]
for
methodname
in
dir
(
viewset
):
for
methodname
in
dir
(
viewset
):
attr
=
getattr
(
viewset
,
methodname
)
attr
=
getattr
(
viewset
,
methodname
)
httpmethod
=
getattr
(
attr
,
'bind_to_method
'
,
None
)
httpmethod
s
=
getattr
(
attr
,
'bind_to_methods
'
,
None
)
if
httpmethod
:
if
httpmethod
s
:
dynamic_routes
.
append
((
httpmethod
,
methodname
))
dynamic_routes
.
append
((
httpmethod
s
,
methodname
))
ret
=
[]
ret
=
[]
for
route
in
self
.
routes
:
for
route
in
self
.
routes
:
if
route
.
mapping
==
{
'{httpmethod}'
:
'{methodname}'
}:
if
route
.
mapping
==
{
'{httpmethod}'
:
'{methodname}'
}:
# Dynamic routes (@link or @action decorator)
# Dynamic routes (@link or @action decorator)
for
httpmethod
,
methodname
in
dynamic_routes
:
for
httpmethod
s
,
methodname
in
dynamic_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
(
url
=
replace_methodname
(
route
.
url
,
methodname
),
url
=
replace_methodname
(
route
.
url
,
methodname
),
mapping
=
{
httpmethod
:
methodname
}
,
mapping
=
dict
((
httpmethod
,
methodname
)
for
httpmethod
in
httpmethods
)
,
name
=
replace_methodname
(
route
.
name
,
methodname
),
name
=
replace_methodname
(
route
.
name
,
methodname
),
initkwargs
=
initkwargs
,
initkwargs
=
initkwargs
,
))
))
...
...
rest_framework/tests/routers.py
View file @
112b52f5
...
@@ -23,6 +23,10 @@ class BasicViewSet(viewsets.ViewSet):
...
@@ -23,6 +23,10 @@ class BasicViewSet(viewsets.ViewSet):
def
action2
(
self
,
request
,
*
args
,
**
kwargs
):
def
action2
(
self
,
request
,
*
args
,
**
kwargs
):
return
Response
({
'method'
:
'action2'
})
return
Response
({
'method'
:
'action2'
})
@action
(
methods
=
[
'post'
,
'delete'
])
def
action3
(
self
,
request
,
*
args
,
**
kwargs
):
return
Response
({
'method'
:
'action2'
})
@link
()
@link
()
def
link1
(
self
,
request
,
*
args
,
**
kwargs
):
def
link1
(
self
,
request
,
*
args
,
**
kwargs
):
return
Response
({
'method'
:
'link1'
})
return
Response
({
'method'
:
'link1'
})
...
@@ -40,16 +44,17 @@ class TestSimpleRouter(TestCase):
...
@@ -40,16 +44,17 @@ class TestSimpleRouter(TestCase):
routes
=
self
.
router
.
get_routes
(
BasicViewSet
)
routes
=
self
.
router
.
get_routes
(
BasicViewSet
)
decorator_routes
=
routes
[
2
:]
decorator_routes
=
routes
[
2
:]
# Make sure all these endpoints exist and none have been clobbered
# Make sure all these endpoints exist and none have been clobbered
for
i
,
endpoint
in
enumerate
([
'action1'
,
'action2'
,
'link1'
,
'link2'
]):
for
i
,
endpoint
in
enumerate
([
'action1'
,
'action2'
,
'
action3'
,
'
link1'
,
'link2'
]):
route
=
decorator_routes
[
i
]
route
=
decorator_routes
[
i
]
# check url listing
# check url listing
self
.
assertEqual
(
route
.
url
,
self
.
assertEqual
(
route
.
url
,
'^{{prefix}}/{{lookup}}/{0}/$'
.
format
(
endpoint
))
'^{{prefix}}/{{lookup}}/{0}/$'
.
format
(
endpoint
))
# check method to function mapping
# check method to function mapping
if
endpoint
.
startswith
(
'action'
):
if
endpoint
==
'action3'
:
method_map
=
'post'
methods_map
=
[
'post'
,
'delete'
]
elif
endpoint
.
startswith
(
'action'
):
methods_map
=
[
'post'
]
else
:
else
:
method_map
=
'get'
methods_map
=
[
'get'
]
self
.
assertEqual
(
route
.
mapping
[
method_map
],
endpoint
)
for
method
in
methods_map
:
self
.
assertEqual
(
route
.
mapping
[
method
],
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