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
e0dddbc5
Commit
e0dddbc5
authored
Jun 27, 2013
by
Tom Christie
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'master' of
https://github.com/tomchristie/django-rest-framework
parents
96f41fd1
7e67ad66
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
36 additions
and
0 deletions
+36
-0
rest_framework/routers.py
+14
-0
rest_framework/tests/test_routers.py
+22
-0
No files found.
rest_framework/routers.py
View file @
e0dddbc5
...
@@ -15,7 +15,9 @@ For example, you might have a `urls.py` that looks something like this:
...
@@ -15,7 +15,9 @@ For example, you might have a `urls.py` that looks something like this:
"""
"""
from
__future__
import
unicode_literals
from
__future__
import
unicode_literals
import
itertools
from
collections
import
namedtuple
from
collections
import
namedtuple
from
django.core.exceptions
import
ImproperlyConfigured
from
rest_framework
import
views
from
rest_framework
import
views
from
rest_framework.compat
import
patterns
,
url
from
rest_framework.compat
import
patterns
,
url
from
rest_framework.response
import
Response
from
rest_framework.response
import
Response
...
@@ -38,6 +40,13 @@ def replace_methodname(format_string, methodname):
...
@@ -38,6 +40,13 @@ def replace_methodname(format_string, methodname):
return
ret
return
ret
def
flatten
(
list_of_lists
):
"""
Takes an iterable of iterables, returns a single iterable containing all items
"""
return
itertools
.
chain
(
*
list_of_lists
)
class
BaseRouter
(
object
):
class
BaseRouter
(
object
):
def
__init__
(
self
):
def
__init__
(
self
):
self
.
registry
=
[]
self
.
registry
=
[]
...
@@ -130,12 +139,17 @@ class SimpleRouter(BaseRouter):
...
@@ -130,12 +139,17 @@ 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
])
# Determine any `@action` or `@link` decorated methods on the viewset
# Determine any `@action` or `@link` decorated methods on the viewset
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
)
if
httpmethods
:
if
httpmethods
:
if
methodname
in
known_actions
:
raise
ImproperlyConfigured
(
'Cannot use @action or @link decorator on '
'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
))
dynamic_routes
.
append
((
httpmethods
,
methodname
))
...
...
rest_framework/tests/test_routers.py
View file @
e0dddbc5
...
@@ -2,6 +2,7 @@ from __future__ import unicode_literals
...
@@ -2,6 +2,7 @@ from __future__ import unicode_literals
from
django.db
import
models
from
django.db
import
models
from
django.test
import
TestCase
from
django.test
import
TestCase
from
django.test.client
import
RequestFactory
from
django.test.client
import
RequestFactory
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
...
@@ -191,3 +192,24 @@ class TestActionKeywordArgs(TestCase):
...
@@ -191,3 +192,24 @@ class TestActionKeywordArgs(TestCase):
response
.
data
,
response
.
data
,
{
'permission_classes'
:
[
permissions
.
AllowAny
]}
{
'permission_classes'
:
[
permissions
.
AllowAny
]}
)
)
class
TestActionAppliedToExistingRoute
(
TestCase
):
"""
Ensure `@action` decorator raises an except when applied
to an existing route
"""
def
test_exception_raised_when_action_applied_to_existing_route
(
self
):
class
TestViewSet
(
viewsets
.
ModelViewSet
):
@action
()
def
retrieve
(
self
,
request
,
*
args
,
**
kwargs
):
return
Response
({
'hello'
:
'world'
})
self
.
router
=
SimpleRouter
()
self
.
router
.
register
(
r'test'
,
TestViewSet
,
base_name
=
'test'
)
with
self
.
assertRaises
(
ImproperlyConfigured
):
self
.
router
.
urls
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