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
ce1a04a6
Commit
ce1a04a6
authored
May 14, 2013
by
Tom Christie
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'master' of
https://github.com/avinash240/django-rest-framework
parents
2cff6e69
5e2d8052
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
58 additions
and
3 deletions
+58
-3
rest_framework/routers.py
+3
-3
rest_framework/tests/routers.py
+55
-0
No files found.
rest_framework/routers.py
View file @
ce1a04a6
...
...
@@ -127,18 +127,18 @@ class SimpleRouter(BaseRouter):
"""
# Determine any `@action` or `@link` decorated methods on the viewset
dynamic_routes
=
{}
dynamic_routes
=
[]
for
methodname
in
dir
(
viewset
):
attr
=
getattr
(
viewset
,
methodname
)
httpmethod
=
getattr
(
attr
,
'bind_to_method'
,
None
)
if
httpmethod
:
dynamic_routes
[
httpmethod
]
=
methodname
dynamic_routes
.
append
((
httpmethod
,
methodname
))
ret
=
[]
for
route
in
self
.
routes
:
if
route
.
mapping
==
{
'{httpmethod}'
:
'{methodname}'
}:
# Dynamic routes (@link or @action decorator)
for
httpmethod
,
methodname
in
dynamic_routes
.
items
()
:
for
httpmethod
,
methodname
in
dynamic_routes
:
initkwargs
=
route
.
initkwargs
.
copy
()
initkwargs
.
update
(
getattr
(
viewset
,
methodname
)
.
kwargs
)
ret
.
append
(
Route
(
...
...
rest_framework/tests/routers.py
0 → 100644
View file @
ce1a04a6
from
__future__
import
unicode_literals
from
django.test
import
TestCase
from
django.test.client
import
RequestFactory
from
rest_framework
import
status
from
rest_framework.response
import
Response
from
rest_framework
import
viewsets
from
rest_framework.decorators
import
link
,
action
from
rest_framework.routers
import
SimpleRouter
import
copy
factory
=
RequestFactory
()
class
BasicViewSet
(
viewsets
.
ViewSet
):
def
list
(
self
,
request
,
*
args
,
**
kwargs
):
return
Response
({
'method'
:
'list'
})
@action
()
def
action1
(
self
,
request
,
*
args
,
**
kwargs
):
return
Response
({
'method'
:
'action1'
})
@action
()
def
action2
(
self
,
request
,
*
args
,
**
kwargs
):
return
Response
({
'method'
:
'action2'
})
@link
()
def
link1
(
self
,
request
,
*
args
,
**
kwargs
):
return
Response
({
'method'
:
'link1'
})
@link
()
def
link2
(
self
,
request
,
*
args
,
**
kwargs
):
return
Response
({
'method'
:
'link2'
})
class
TestSimpleRouter
(
TestCase
):
def
setUp
(
self
):
self
.
router
=
SimpleRouter
()
def
test_link_and_action_decorator
(
self
):
routes
=
self
.
router
.
get_routes
(
BasicViewSet
)
decorator_routes
=
routes
[
2
:]
# Make sure all these endpoints exist and none have been clobbered
for
i
,
endpoint
in
enumerate
([
'action1'
,
'action2'
,
'link1'
,
'link2'
]):
route
=
decorator_routes
[
i
]
# check url listing
self
.
assertEqual
(
route
.
url
,
'^{{prefix}}/{{lookup}}/{0}/$'
.
format
(
endpoint
))
# check method to function mapping
if
endpoint
.
startswith
(
'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