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
2e178bc9
Commit
2e178bc9
authored
Oct 17, 2015
by
Omer Katz
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Replaced all dict and set conversions from lists to dict and set literals.
parent
df025f3d
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
44 additions
and
51 deletions
+44
-51
rest_framework/exceptions.py
+3
-3
rest_framework/fields.py
+18
-18
rest_framework/metadata.py
+1
-1
rest_framework/pagination.py
+1
-5
rest_framework/routers.py
+1
-1
rest_framework/serializers.py
+11
-14
rest_framework/validators.py
+9
-9
No files found.
rest_framework/exceptions.py
View file @
2e178bc9
...
@@ -30,10 +30,10 @@ def _force_text_recursive(data):
...
@@ -30,10 +30,10 @@ def _force_text_recursive(data):
return
ReturnList
(
ret
,
serializer
=
data
.
serializer
)
return
ReturnList
(
ret
,
serializer
=
data
.
serializer
)
return
data
return
data
elif
isinstance
(
data
,
dict
):
elif
isinstance
(
data
,
dict
):
ret
=
dict
([
ret
=
{
(
key
,
_force_text_recursive
(
value
)
)
key
:
_force_text_recursive
(
value
)
for
key
,
value
in
data
.
items
()
for
key
,
value
in
data
.
items
()
])
}
if
isinstance
(
data
,
ReturnDict
):
if
isinstance
(
data
,
ReturnDict
):
return
ReturnDict
(
ret
,
serializer
=
data
.
serializer
)
return
ReturnDict
(
ret
,
serializer
=
data
.
serializer
)
return
data
return
data
...
...
rest_framework/fields.py
View file @
2e178bc9
...
@@ -604,8 +604,8 @@ class BooleanField(Field):
...
@@ -604,8 +604,8 @@ class BooleanField(Field):
}
}
default_empty_html
=
False
default_empty_html
=
False
initial
=
False
initial
=
False
TRUE_VALUES
=
set
((
't'
,
'T'
,
'true'
,
'True'
,
'TRUE'
,
'1'
,
1
,
True
))
TRUE_VALUES
=
{
't'
,
'T'
,
'true'
,
'True'
,
'TRUE'
,
'1'
,
1
,
True
}
FALSE_VALUES
=
set
((
'f'
,
'F'
,
'false'
,
'False'
,
'FALSE'
,
'0'
,
0
,
0.0
,
False
))
FALSE_VALUES
=
{
'f'
,
'F'
,
'false'
,
'False'
,
'FALSE'
,
'0'
,
0
,
0.0
,
False
}
def
__init__
(
self
,
**
kwargs
):
def
__init__
(
self
,
**
kwargs
):
assert
'allow_null'
not
in
kwargs
,
'`allow_null` is not a valid option. Use `NullBooleanField` instead.'
assert
'allow_null'
not
in
kwargs
,
'`allow_null` is not a valid option. Use `NullBooleanField` instead.'
...
@@ -634,9 +634,9 @@ class NullBooleanField(Field):
...
@@ -634,9 +634,9 @@ class NullBooleanField(Field):
'invalid'
:
_
(
'"{input}" is not a valid boolean.'
)
'invalid'
:
_
(
'"{input}" is not a valid boolean.'
)
}
}
initial
=
None
initial
=
None
TRUE_VALUES
=
set
((
't'
,
'T'
,
'true'
,
'True'
,
'TRUE'
,
'1'
,
1
,
True
))
TRUE_VALUES
=
{
't'
,
'T'
,
'true'
,
'True'
,
'TRUE'
,
'1'
,
1
,
True
}
FALSE_VALUES
=
set
((
'f'
,
'F'
,
'false'
,
'False'
,
'FALSE'
,
'0'
,
0
,
0.0
,
False
))
FALSE_VALUES
=
{
'f'
,
'F'
,
'false'
,
'False'
,
'FALSE'
,
'0'
,
0
,
0.0
,
False
}
NULL_VALUES
=
set
((
'n'
,
'N'
,
'null'
,
'Null'
,
'NULL'
,
''
,
None
))
NULL_VALUES
=
{
'n'
,
'N'
,
'null'
,
'Null'
,
'NULL'
,
''
,
None
}
def
__init__
(
self
,
**
kwargs
):
def
__init__
(
self
,
**
kwargs
):
assert
'allow_null'
not
in
kwargs
,
'`allow_null` is not a valid option.'
assert
'allow_null'
not
in
kwargs
,
'`allow_null` is not a valid option.'
...
@@ -1241,9 +1241,9 @@ class ChoiceField(Field):
...
@@ -1241,9 +1241,9 @@ class ChoiceField(Field):
# Map the string representation of choices to the underlying value.
# Map the string representation of choices to the underlying value.
# Allows us to deal with eg. integer choices while supporting either
# Allows us to deal with eg. integer choices while supporting either
# integer or string input, but still get the correct datatype out.
# integer or string input, but still get the correct datatype out.
self
.
choice_strings_to_values
=
dict
([
self
.
choice_strings_to_values
=
{
(
six
.
text_type
(
key
),
key
)
for
key
in
self
.
choices
.
keys
()
six
.
text_type
(
key
):
key
for
key
in
self
.
choices
.
keys
()
])
}
self
.
allow_blank
=
kwargs
.
pop
(
'allow_blank'
,
False
)
self
.
allow_blank
=
kwargs
.
pop
(
'allow_blank'
,
False
)
...
@@ -1302,15 +1302,15 @@ class MultipleChoiceField(ChoiceField):
...
@@ -1302,15 +1302,15 @@ class MultipleChoiceField(ChoiceField):
if
not
self
.
allow_empty
and
len
(
data
)
==
0
:
if
not
self
.
allow_empty
and
len
(
data
)
==
0
:
self
.
fail
(
'empty'
)
self
.
fail
(
'empty'
)
return
set
([
return
{
super
(
MultipleChoiceField
,
self
)
.
to_internal_value
(
item
)
super
(
MultipleChoiceField
,
self
)
.
to_internal_value
(
item
)
for
item
in
data
for
item
in
data
])
}
def
to_representation
(
self
,
value
):
def
to_representation
(
self
,
value
):
return
set
([
return
{
self
.
choice_strings_to_values
.
get
(
six
.
text_type
(
item
),
item
)
for
item
in
value
self
.
choice_strings_to_values
.
get
(
six
.
text_type
(
item
),
item
)
for
item
in
value
])
}
class
FilePathField
(
ChoiceField
):
class
FilePathField
(
ChoiceField
):
...
@@ -1508,19 +1508,19 @@ class DictField(Field):
...
@@ -1508,19 +1508,19 @@ class DictField(Field):
data
=
html
.
parse_html_dict
(
data
)
data
=
html
.
parse_html_dict
(
data
)
if
not
isinstance
(
data
,
dict
):
if
not
isinstance
(
data
,
dict
):
self
.
fail
(
'not_a_dict'
,
input_type
=
type
(
data
)
.
__name__
)
self
.
fail
(
'not_a_dict'
,
input_type
=
type
(
data
)
.
__name__
)
return
dict
([
return
{
(
six
.
text_type
(
key
),
self
.
child
.
run_validation
(
value
)
)
six
.
text_type
(
key
):
self
.
child
.
run_validation
(
value
)
for
key
,
value
in
data
.
items
()
for
key
,
value
in
data
.
items
()
])
}
def
to_representation
(
self
,
value
):
def
to_representation
(
self
,
value
):
"""
"""
List of object instances -> List of dicts of primitive datatypes.
List of object instances -> List of dicts of primitive datatypes.
"""
"""
return
dict
([
return
{
(
six
.
text_type
(
key
),
self
.
child
.
to_representation
(
val
)
)
six
.
text_type
(
key
):
self
.
child
.
to_representation
(
val
)
for
key
,
val
in
value
.
items
()
for
key
,
val
in
value
.
items
()
])
}
class
JSONField
(
Field
):
class
JSONField
(
Field
):
...
...
rest_framework/metadata.py
View file @
2e178bc9
...
@@ -77,7 +77,7 @@ class SimpleMetadata(BaseMetadata):
...
@@ -77,7 +77,7 @@ class SimpleMetadata(BaseMetadata):
the fields that are accepted for 'PUT' and 'POST' methods.
the fields that are accepted for 'PUT' and 'POST' methods.
"""
"""
actions
=
{}
actions
=
{}
for
method
in
set
([
'PUT'
,
'POST'
])
&
set
(
view
.
allowed_methods
):
for
method
in
{
'PUT'
,
'POST'
}
&
set
(
view
.
allowed_methods
):
view
.
request
=
clone_request
(
request
,
method
)
view
.
request
=
clone_request
(
request
,
method
)
try
:
try
:
# Test global permissions
# Test global permissions
...
...
rest_framework/pagination.py
View file @
2e178bc9
...
@@ -79,11 +79,7 @@ def _get_displayed_page_numbers(current, final):
...
@@ -79,11 +79,7 @@ def _get_displayed_page_numbers(current, final):
# We always include the first two pages, last two pages, and
# We always include the first two pages, last two pages, and
# two pages either side of the current page.
# two pages either side of the current page.
included
=
set
((
included
=
{
1
,
current
-
1
,
current
,
current
+
1
,
final
}
1
,
current
-
1
,
current
,
current
+
1
,
final
))
# If the break would only exclude a single page number then we
# If the break would only exclude a single page number then we
# may as well include the page number instead of the break.
# may as well include the page number instead of the break.
...
...
rest_framework/routers.py
View file @
2e178bc9
...
@@ -174,7 +174,7 @@ class SimpleRouter(BaseRouter):
...
@@ -174,7 +174,7 @@ class SimpleRouter(BaseRouter):
url_path
=
initkwargs
.
pop
(
"url_path"
,
None
)
or
methodname
url_path
=
initkwargs
.
pop
(
"url_path"
,
None
)
or
methodname
ret
.
append
(
Route
(
ret
.
append
(
Route
(
url
=
replace_methodname
(
route
.
url
,
url_path
),
url
=
replace_methodname
(
route
.
url
,
url_path
),
mapping
=
dict
((
httpmethod
,
methodname
)
for
httpmethod
in
httpmethods
)
,
mapping
=
{
httpmethod
:
methodname
for
httpmethod
in
httpmethods
}
,
name
=
replace_methodname
(
route
.
name
,
url_path
),
name
=
replace_methodname
(
route
.
name
,
url_path
),
initkwargs
=
initkwargs
,
initkwargs
=
initkwargs
,
))
))
...
...
rest_framework/serializers.py
View file @
2e178bc9
...
@@ -125,10 +125,10 @@ class BaseSerializer(Field):
...
@@ -125,10 +125,10 @@ class BaseSerializer(Field):
}
}
if
allow_empty
is
not
None
:
if
allow_empty
is
not
None
:
list_kwargs
[
'allow_empty'
]
=
allow_empty
list_kwargs
[
'allow_empty'
]
=
allow_empty
list_kwargs
.
update
(
dict
([
list_kwargs
.
update
(
{
(
key
,
value
)
for
key
,
value
in
kwargs
.
items
()
key
:
value
for
key
,
value
in
kwargs
.
items
()
if
key
in
LIST_SERIALIZER_KWARGS
if
key
in
LIST_SERIALIZER_KWARGS
])
)
}
)
meta
=
getattr
(
cls
,
'Meta'
,
None
)
meta
=
getattr
(
cls
,
'Meta'
,
None
)
list_serializer_class
=
getattr
(
meta
,
'list_serializer_class'
,
ListSerializer
)
list_serializer_class
=
getattr
(
meta
,
'list_serializer_class'
,
ListSerializer
)
return
list_serializer_class
(
*
args
,
**
list_kwargs
)
return
list_serializer_class
(
*
args
,
**
list_kwargs
)
...
@@ -305,10 +305,10 @@ def get_validation_error_detail(exc):
...
@@ -305,10 +305,10 @@ def get_validation_error_detail(exc):
elif
isinstance
(
exc
.
detail
,
dict
):
elif
isinstance
(
exc
.
detail
,
dict
):
# If errors may be a dict we use the standard {key: list of values}.
# If errors may be a dict we use the standard {key: list of values}.
# Here we ensure that all the values are *lists* of errors.
# Here we ensure that all the values are *lists* of errors.
return
dict
([
return
{
(
key
,
value
if
isinstance
(
value
,
list
)
else
[
value
])
key
:
value
if
isinstance
(
value
,
list
)
else
[
value
]
for
key
,
value
in
exc
.
detail
.
items
()
for
key
,
value
in
exc
.
detail
.
items
()
])
}
elif
isinstance
(
exc
.
detail
,
list
):
elif
isinstance
(
exc
.
detail
,
list
):
# Errors raised as a list are non-field errors.
# Errors raised as a list are non-field errors.
return
{
return
{
...
@@ -1237,13 +1237,10 @@ class ModelSerializer(Serializer):
...
@@ -1237,13 +1237,10 @@ class ModelSerializer(Serializer):
for
model_field
in
model_fields
.
values
():
for
model_field
in
model_fields
.
values
():
# Include each of the `unique_for_*` field names.
# Include each of the `unique_for_*` field names.
unique_constraint_names
|=
set
([
unique_constraint_names
|=
{
model_field
.
unique_for_date
,
model_field
.
unique_for_month
,
model_field
.
unique_for_date
,
model_field
.
unique_for_year
}
model_field
.
unique_for_month
,
model_field
.
unique_for_year
])
unique_constraint_names
-=
set
([
None
])
unique_constraint_names
-=
{
None
}
# Include each of the `unique_together` field names,
# Include each of the `unique_together` field names,
# so long as all the field names are included on the serializer.
# so long as all the field names are included on the serializer.
...
@@ -1357,10 +1354,10 @@ class ModelSerializer(Serializer):
...
@@ -1357,10 +1354,10 @@ class ModelSerializer(Serializer):
# which may map onto a model field. Any dotted field name lookups
# which may map onto a model field. Any dotted field name lookups
# cannot map to a field, and must be a traversal, so we're not
# cannot map to a field, and must be a traversal, so we're not
# including those.
# including those.
field_names
=
set
([
field_names
=
{
field
.
source
for
field
in
self
.
fields
.
values
()
field
.
source
for
field
in
self
.
fields
.
values
()
if
(
field
.
source
!=
'*'
)
and
(
'.'
not
in
field
.
source
)
if
(
field
.
source
!=
'*'
)
and
(
'.'
not
in
field
.
source
)
])
}
# Note that we make sure to check `unique_together` both on the
# Note that we make sure to check `unique_together` both on the
# base model class, but also on any parent classes.
# base model class, but also on any parent classes.
...
...
rest_framework/validators.py
View file @
2e178bc9
...
@@ -100,11 +100,11 @@ class UniqueTogetherValidator(object):
...
@@ -100,11 +100,11 @@ class UniqueTogetherValidator(object):
if
self
.
instance
is
not
None
:
if
self
.
instance
is
not
None
:
return
return
missing
=
dict
([
missing
=
{
(
field_name
,
self
.
missing_message
)
field_name
:
self
.
missing_message
for
field_name
in
self
.
fields
for
field_name
in
self
.
fields
if
field_name
not
in
attrs
if
field_name
not
in
attrs
])
}
if
missing
:
if
missing
:
raise
ValidationError
(
missing
)
raise
ValidationError
(
missing
)
...
@@ -120,10 +120,10 @@ class UniqueTogetherValidator(object):
...
@@ -120,10 +120,10 @@ class UniqueTogetherValidator(object):
attrs
[
field_name
]
=
getattr
(
self
.
instance
,
field_name
)
attrs
[
field_name
]
=
getattr
(
self
.
instance
,
field_name
)
# Determine the filter keyword arguments and filter the queryset.
# Determine the filter keyword arguments and filter the queryset.
filter_kwargs
=
dict
([
filter_kwargs
=
{
(
field_name
,
attrs
[
field_name
])
field_name
:
attrs
[
field_name
]
for
field_name
in
self
.
fields
for
field_name
in
self
.
fields
])
}
return
queryset
.
filter
(
**
filter_kwargs
)
return
queryset
.
filter
(
**
filter_kwargs
)
def
exclude_current_instance
(
self
,
attrs
,
queryset
):
def
exclude_current_instance
(
self
,
attrs
,
queryset
):
...
@@ -184,11 +184,11 @@ class BaseUniqueForValidator(object):
...
@@ -184,11 +184,11 @@ class BaseUniqueForValidator(object):
The `UniqueFor<Range>Validator` classes always force an implied
The `UniqueFor<Range>Validator` classes always force an implied
'required' state on the fields they are applied to.
'required' state on the fields they are applied to.
"""
"""
missing
=
dict
([
missing
=
{
(
field_name
,
self
.
missing_message
)
field_name
:
self
.
missing_message
for
field_name
in
[
self
.
field
,
self
.
date_field
]
for
field_name
in
[
self
.
field
,
self
.
date_field
]
if
field_name
not
in
attrs
if
field_name
not
in
attrs
])
}
if
missing
:
if
missing
:
raise
ValidationError
(
missing
)
raise
ValidationError
(
missing
)
...
...
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