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
bc3485ab
Commit
bc3485ab
authored
Jun 23, 2016
by
Tom Christie
Committed by
GitHub
Jun 23, 2016
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Namespace versioning with nested namespaces (#4219)
Support nested namespaces with namespaced versioning.
parent
ea92d505
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
16 additions
and
5 deletions
+16
-5
rest_framework/versioning.py
+8
-5
tests/test_versioning.py
+8
-0
No files found.
rest_framework/versioning.py
View file @
bc3485ab
...
...
@@ -112,16 +112,19 @@ class NamespaceVersioning(BaseVersioning):
Host: example.com
Accept: application/json
"""
invalid_version_message
=
_
(
'Invalid version in URL path.'
)
invalid_version_message
=
_
(
'Invalid version in URL path.
Does not match any version namespace.
'
)
def
determine_version
(
self
,
request
,
*
args
,
**
kwargs
):
resolver_match
=
getattr
(
request
,
'resolver_match'
,
None
)
if
(
resolver_match
is
None
or
not
resolver_match
.
namespace
):
return
self
.
default_version
version
=
resolver_match
.
namespace
if
not
self
.
is_allowed_version
(
version
):
raise
exceptions
.
NotFound
(
self
.
invalid_version_message
)
return
version
# Allow for possibly nested namespaces.
possible_versions
=
resolver_match
.
namespace
.
split
(
':'
)
for
version
in
possible_versions
:
if
self
.
is_allowed_version
(
version
):
return
version
raise
exceptions
.
NotFound
(
self
.
invalid_version_message
)
def
reverse
(
self
,
viewname
,
args
=
None
,
kwargs
=
None
,
request
=
None
,
format
=
None
,
**
extra
):
if
request
.
version
is
not
None
:
...
...
tests/test_versioning.py
View file @
bc3485ab
...
...
@@ -296,8 +296,12 @@ class TestHyperlinkedRelatedField(URLPatternsTestCase):
class
TestNamespaceVersioningHyperlinkedRelatedFieldScheme
(
URLPatternsTestCase
):
nested
=
[
url
(
r'^namespaced/(?P<pk>\d+)/$'
,
dummy_pk_view
,
name
=
'nested'
),
]
included
=
[
url
(
r'^namespaced/(?P<pk>\d+)/$'
,
dummy_pk_view
,
name
=
'namespaced'
),
url
(
r'^nested/'
,
include
(
nested
,
namespace
=
'nested-namespace'
))
]
urlpatterns
=
[
...
...
@@ -325,6 +329,10 @@ class TestNamespaceVersioningHyperlinkedRelatedFieldScheme(URLPatternsTestCase):
field
=
self
.
_create_field
(
'namespaced'
,
'v2'
)
assert
field
.
to_representation
(
PKOnlyObject
(
5
))
==
'http://testserver/v2/namespaced/5/'
def
test_api_url_is_properly_reversed_with_nested
(
self
):
field
=
self
.
_create_field
(
'nested'
,
'v1:nested-namespace'
)
assert
field
.
to_representation
(
PKOnlyObject
(
3
))
==
'http://testserver/v1/nested/namespaced/3/'
def
test_non_api_url_is_properly_reversed_regardless_of_the_version
(
self
):
"""
Regression test for #2711
...
...
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