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
73978c95
Commit
73978c95
authored
Apr 22, 2015
by
kazmiruk
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
change SortedDict to OrderedDict
parent
601b2241
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
28 additions
and
27 deletions
+28
-27
rest_framework/fields.py
+2
-3
rest_framework/routers.py
+3
-4
rest_framework/serializers.py
+15
-12
rest_framework/utils/encoders.py
+6
-6
rest_framework/views.py
+2
-2
No files found.
rest_framework/fields.py
View file @
73978c95
...
...
@@ -22,7 +22,6 @@ from django.forms import widgets
from
django.utils
import
six
,
timezone
from
django.utils.encoding
import
is_protected_type
from
django.utils.translation
import
ugettext_lazy
as
_
from
django.utils.datastructures
import
SortedDict
from
django.utils.dateparse
import
parse_date
,
parse_datetime
,
parse_time
from
rest_framework
import
ISO_8601
from
rest_framework.compat
import
(
...
...
@@ -225,7 +224,7 @@ class Field(object):
return
[
self
.
to_native
(
item
)
for
item
in
value
]
elif
isinstance
(
value
,
dict
):
# Make sure we preserve field ordering, if it exists
ret
=
Sort
edDict
()
ret
=
collections
.
Order
edDict
()
for
key
,
val
in
value
.
items
():
ret
[
key
]
=
self
.
to_native
(
val
)
return
ret
...
...
@@ -240,7 +239,7 @@ class Field(object):
return
{}
def
metadata
(
self
):
metadata
=
Sort
edDict
()
metadata
=
collections
.
Order
edDict
()
metadata
[
'type'
]
=
self
.
type_label
metadata
[
'required'
]
=
getattr
(
self
,
'required'
,
False
)
optional_attrs
=
[
'read_only'
,
'label'
,
'help_text'
,
...
...
rest_framework/routers.py
View file @
73978c95
...
...
@@ -16,11 +16,10 @@ For example, you might have a `urls.py` that looks something like this:
from
__future__
import
unicode_literals
import
itertools
from
collections
import
namedtuple
from
collections
import
namedtuple
,
OrderedDict
from
django.conf.urls
import
patterns
,
url
from
django.core.exceptions
import
ImproperlyConfigured
from
django.core.urlresolvers
import
NoReverseMatch
from
django.utils.datastructures
import
SortedDict
from
rest_framework
import
views
from
rest_framework.response
import
Response
from
rest_framework.reverse
import
reverse
...
...
@@ -278,7 +277,7 @@ class DefaultRouter(SimpleRouter):
"""
Return a view to use as the API root.
"""
api_root_dict
=
Sort
edDict
()
api_root_dict
=
Order
edDict
()
list_name
=
self
.
routes
[
0
]
.
name
for
prefix
,
viewset
,
basename
in
self
.
registry
:
api_root_dict
[
prefix
]
=
list_name
.
format
(
basename
=
basename
)
...
...
@@ -287,7 +286,7 @@ class DefaultRouter(SimpleRouter):
_ignore_model_permissions
=
True
def
get
(
self
,
request
,
*
args
,
**
kwargs
):
ret
=
Sort
edDict
()
ret
=
Order
edDict
()
for
key
,
url_name
in
api_root_dict
.
items
():
try
:
ret
[
key
]
=
reverse
(
...
...
rest_framework/serializers.py
View file @
73978c95
...
...
@@ -11,6 +11,7 @@ python primitives.
response content is handled by parsers and renderers.
"""
from
__future__
import
unicode_literals
from
collections
import
OrderedDict
import
copy
import
datetime
import
inspect
...
...
@@ -21,7 +22,6 @@ from django.core.paginator import Page
from
django.db
import
models
from
django.forms
import
widgets
from
django.utils
import
six
from
django.utils.datastructures
import
SortedDict
from
django.utils.functional
import
cached_property
from
django.core.exceptions
import
ObjectDoesNotExist
from
rest_framework.settings
import
api_settings
...
...
@@ -106,7 +106,7 @@ class DictWithMetadata(dict):
return
dict
(
self
)
class
SortedDictWithMetadata
(
Sort
edDict
):
class
OrderedDictWithMetadata
(
Order
edDict
):
"""
A sorted dict-like object, that can have additional properties attached.
"""
...
...
@@ -116,7 +116,7 @@ class SortedDictWithMetadata(SortedDict):
Overriden to remove the metadata from the dict, since it shouldn't be
pickle and may in some instances be unpickleable.
"""
return
Sort
edDict
(
self
)
.
__dict__
return
Order
edDict
(
self
)
.
__dict__
def
_is_protected_type
(
obj
):
...
...
@@ -152,7 +152,7 @@ def _get_declared_fields(bases, attrs):
if
hasattr
(
base
,
'base_fields'
):
fields
=
list
(
base
.
base_fields
.
items
())
+
fields
return
Sort
edDict
(
fields
)
return
Order
edDict
(
fields
)
class
SerializerMetaclass
(
type
):
...
...
@@ -180,7 +180,7 @@ class BaseSerializer(WritableField):
pass
_options_class
=
SerializerOptions
_dict_class
=
Sort
edDictWithMetadata
_dict_class
=
Order
edDictWithMetadata
def
__init__
(
self
,
instance
=
None
,
data
=
None
,
files
=
None
,
context
=
None
,
partial
=
False
,
many
=
False
,
...
...
@@ -229,7 +229,7 @@ class BaseSerializer(WritableField):
This will be the set of any explicitly declared fields,
plus the set of fields returned by get_default_fields().
"""
ret
=
Sort
edDict
()
ret
=
Order
edDict
()
# Get the explicitly declared fields
base_fields
=
copy
.
deepcopy
(
self
.
base_fields
)
...
...
@@ -245,7 +245,7 @@ class BaseSerializer(WritableField):
# If 'fields' is specified, use those fields, in that order.
if
self
.
opts
.
fields
:
assert
isinstance
(
self
.
opts
.
fields
,
(
list
,
tuple
)),
'`fields` must be a list or tuple'
new
=
Sort
edDict
()
new
=
Order
edDict
()
for
key
in
self
.
opts
.
fields
:
new
[
key
]
=
ret
[
key
]
ret
=
new
...
...
@@ -606,7 +606,7 @@ class BaseSerializer(WritableField):
Useful for things like responding to OPTIONS requests, or generating
API schemas for auto-documentation.
"""
return
Sort
edDict
(
return
Order
edDict
(
[
(
field_name
,
field
.
metadata
())
for
field_name
,
field
in
six
.
iteritems
(
self
.
fields
)
...
...
@@ -683,7 +683,7 @@ class ModelSerializer(Serializer):
self
.
__class__
.
__name__
)
opts
=
cls
.
_meta
.
concrete_model
.
_meta
ret
=
Sort
edDict
()
ret
=
Order
edDict
()
nested
=
bool
(
self
.
opts
.
depth
)
# Deal with adding the primary key field
...
...
@@ -985,13 +985,16 @@ class ModelSerializer(Serializer):
if
field_name
in
attrs
:
m2m_data
[
field_name
]
=
attrs
.
pop
(
field_name
)
# Forward m2m relations
for
field
in
meta
.
many_to_many
+
meta
.
virtual_fields
:
def
_inner_loop_code
(
field
):
if
isinstance
(
field
,
GenericForeignKey
):
continue
return
if
field
.
name
in
attrs
:
m2m_data
[
field
.
name
]
=
attrs
.
pop
(
field
.
name
)
# Forward m2m relations
_
=
[
_inner_loop_code
(
field
)
for
field
in
meta
.
many_to_many
]
_
=
[
_inner_loop_code
(
field
)
for
field
in
meta
.
virtual_fields
]
# Nested forward relations - These need to be marked so we can save
# them before saving the parent model instance.
for
field_name
in
attrs
.
keys
():
...
...
rest_framework/utils/encoders.py
View file @
73978c95
...
...
@@ -2,12 +2,12 @@
Helper classes for parsers.
"""
from
__future__
import
unicode_literals
from
collections
import
OrderedDict
from
django.utils
import
timezone
from
django.db.models.query
import
QuerySet
from
django.utils.datastructures
import
SortedDict
from
django.utils.functional
import
Promise
from
rest_framework.compat
import
force_text
from
rest_framework.serializers
import
DictWithMetadata
,
Sort
edDictWithMetadata
from
rest_framework.serializers
import
DictWithMetadata
,
Order
edDictWithMetadata
import
datetime
import
decimal
import
types
...
...
@@ -67,7 +67,7 @@ else:
class
SafeDumper
(
yaml
.
SafeDumper
):
"""
Handles decimals as strings.
Handles
Sort
edDicts as usual dicts, but preserves field order, rather
Handles
Order
edDicts as usual dicts, but preserves field order, rather
than the usual behaviour of sorting the keys.
"""
def
represent_decimal
(
self
,
data
):
...
...
@@ -81,7 +81,7 @@ else:
best_style
=
True
if
hasattr
(
mapping
,
'items'
):
mapping
=
list
(
mapping
.
items
())
if
not
isinstance
(
mapping
,
Sort
edDict
):
if
not
isinstance
(
mapping
,
Order
edDict
):
mapping
.
sort
()
for
item_key
,
item_value
in
mapping
:
node_key
=
self
.
represent_data
(
item_key
)
...
...
@@ -103,7 +103,7 @@ else:
SafeDumper
.
represent_decimal
)
SafeDumper
.
add_representer
(
Sort
edDict
,
Order
edDict
,
yaml
.
representer
.
SafeRepresenter
.
represent_dict
)
SafeDumper
.
add_representer
(
...
...
@@ -111,7 +111,7 @@ else:
yaml
.
representer
.
SafeRepresenter
.
represent_dict
)
SafeDumper
.
add_representer
(
Sort
edDictWithMetadata
,
Order
edDictWithMetadata
,
yaml
.
representer
.
SafeRepresenter
.
represent_dict
)
SafeDumper
.
add_representer
(
...
...
rest_framework/views.py
View file @
73978c95
...
...
@@ -3,9 +3,9 @@ Provides an APIView class that is the base of all views in REST framework.
"""
from
__future__
import
unicode_literals
from
collections
import
OrderedDict
from
django.core.exceptions
import
PermissionDenied
from
django.http
import
Http404
from
django.utils.datastructures
import
SortedDict
from
django.views.decorators.csrf
import
csrf_exempt
from
rest_framework
import
status
,
exceptions
from
rest_framework.compat
import
smart_text
,
HttpResponseBase
,
View
...
...
@@ -421,7 +421,7 @@ class APIView(View):
# By default we can't provide any form-like information, however the
# generic views override this implementation and add additional
# information for POST and PUT methods, based on the serializer.
ret
=
Sort
edDict
()
ret
=
Order
edDict
()
ret
[
'name'
]
=
self
.
get_view_name
()
ret
[
'description'
]
=
self
.
get_view_description
()
ret
[
'renders'
]
=
[
renderer
.
media_type
for
renderer
in
self
.
renderer_classes
]
...
...
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