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
fe9647ce
Commit
fe9647ce
authored
Dec 16, 2014
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
AcceptHeaderVersioning to return unicode strings.
parent
4e91ec61
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
37 additions
and
5 deletions
+37
-5
rest_framework/compat.py
+9
-4
rest_framework/versioning.py
+28
-1
No files found.
rest_framework/compat.py
View file @
fe9647ce
...
@@ -5,15 +5,13 @@ versions of django/python, and compatibility wrappers around optional packages.
...
@@ -5,15 +5,13 @@ versions of django/python, and compatibility wrappers around optional packages.
# flake8: noqa
# flake8: noqa
from
__future__
import
unicode_literals
from
__future__
import
unicode_literals
import
inspect
from
django.core.exceptions
import
ImproperlyConfigured
from
django.core.exceptions
import
ImproperlyConfigured
from
django.conf
import
settings
from
django.utils.encoding
import
force_text
from
django.utils.encoding
import
force_text
from
django.utils.six.moves.urllib
import
parse
as
urlparse
from
django.utils.six.moves.urllib
import
parse
as
urlparse
from
django.conf
import
settings
from
django.utils
import
six
from
django.utils
import
six
import
django
import
django
import
inspect
def
unicode_repr
(
instance
):
def
unicode_repr
(
instance
):
...
@@ -33,6 +31,13 @@ def unicode_to_repr(value):
...
@@ -33,6 +31,13 @@ def unicode_to_repr(value):
return
value
return
value
def
unicode_http_header
(
value
):
# Coerce HTTP header value to unicode.
if
isinstance
(
value
,
six
.
binary_type
):
return
value
.
decode
(
'iso-8859-1'
)
return
value
# OrderedDict only available in Python 2.7.
# OrderedDict only available in Python 2.7.
# This will always be the case in Django 1.7 and above, as these versions
# This will always be the case in Django 1.7 and above, as these versions
# no longer support Python 2.6.
# no longer support Python 2.6.
...
...
rest_framework/versioning.py
View file @
fe9647ce
# coding: utf-8
# coding: utf-8
from
__future__
import
unicode_literals
from
__future__
import
unicode_literals
from
rest_framework.compat
import
unicode_http_header
from
rest_framework.reverse
import
_reverse
from
rest_framework.reverse
import
_reverse
from
rest_framework.templatetags.rest_framework
import
replace_query_param
from
rest_framework.templatetags.rest_framework
import
replace_query_param
from
rest_framework.utils.mediatypes
import
_MediaType
from
rest_framework.utils.mediatypes
import
_MediaType
...
@@ -69,7 +70,8 @@ class AcceptHeaderVersioning(BaseVersioning):
...
@@ -69,7 +70,8 @@ class AcceptHeaderVersioning(BaseVersioning):
def
determine_version
(
self
,
request
,
*
args
,
**
kwargs
):
def
determine_version
(
self
,
request
,
*
args
,
**
kwargs
):
media_type
=
_MediaType
(
request
.
accepted_media_type
)
media_type
=
_MediaType
(
request
.
accepted_media_type
)
return
media_type
.
params
.
get
(
self
.
version_param
,
self
.
default_version
)
version
=
media_type
.
params
.
get
(
self
.
version_param
,
self
.
default_version
)
return
unicode_http_header
(
version
)
# We don't need to implement `reverse`, as the versioning is based
# We don't need to implement `reverse`, as the versioning is based
# on the `Accept` header, not on the request URL.
# on the `Accept` header, not on the request URL.
...
@@ -77,6 +79,17 @@ class AcceptHeaderVersioning(BaseVersioning):
...
@@ -77,6 +79,17 @@ class AcceptHeaderVersioning(BaseVersioning):
class
URLPathVersioning
(
BaseVersioning
):
class
URLPathVersioning
(
BaseVersioning
):
"""
"""
To the client this is the same style as `NamespaceVersioning`.
The difference is in the backend - this implementation uses
Django's URL keyword arguments to determine the version.
An example URL conf for two views that accept two different versions.
urlpatterns = [
url(r'^(?P<version>{v1,v2})/users/$', users_list, name='users-list'),
url(r'^(?P<version>{v1,v2})/users/(?P<pk>[0-9]+)/$', users_detail, name='users-detail')
]
GET /1.0/something/ HTTP/1.1
GET /1.0/something/ HTTP/1.1
Host: example.com
Host: example.com
Accept: application/json
Accept: application/json
...
@@ -103,6 +116,20 @@ class NamespaceVersioning(BaseVersioning):
...
@@ -103,6 +116,20 @@ class NamespaceVersioning(BaseVersioning):
The difference is in the backend - this implementation uses
The difference is in the backend - this implementation uses
Django's URL namespaces to determine the version.
Django's URL namespaces to determine the version.
An example URL conf that is namespaced into two seperate versions
# users/urls.py
urlpatterns = [
url(r'^/users/$', users_list, name='users-list'),
url(r'^/users/(?P<pk>[0-9]+)/$', users_detail, name='users-detail')
]
# urls.py
urlpatterns = [
url(r'^v1/', include('users.urls', namespace='v1')),
url(r'^v2/', include('users.urls', namespace='v2'))
]
GET /1.0/something/ HTTP/1.1
GET /1.0/something/ HTTP/1.1
Host: example.com
Host: example.com
Accept: application/json
Accept: application/json
...
...
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