mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-14 05:36:50 +03:00
Replaced all dict and set conversions from lists to dict and set literals.
This commit is contained in:
parent
df025f3d98
commit
2e178bc970
|
@ -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
|
||||||
|
|
|
@ -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):
|
||||||
'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):
|
||||||
# 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):
|
||||||
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):
|
||||||
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):
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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.
|
||||||
|
|
|
@ -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,
|
||||||
))
|
))
|
||||||
|
|
|
@ -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):
|
||||||
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):
|
||||||
|
|
||||||
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):
|
||||||
# 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.
|
||||||
|
|
|
@ -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):
|
||||||
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):
|
||||||
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)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user