mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-07-17 19:52:25 +03:00
Use comprehensions
This commit is contained in:
parent
a677b09729
commit
c383cf2947
|
@ -380,7 +380,7 @@ class AutoSchema(ViewInspector):
|
||||||
manual_fields = self.get_manual_fields(path, method)
|
manual_fields = self.get_manual_fields(path, method)
|
||||||
fields = self.update_fields(fields, manual_fields)
|
fields = self.update_fields(fields, manual_fields)
|
||||||
|
|
||||||
if fields and any([field.location in ('form', 'body') for field in fields]):
|
if fields and any(field.location in ('form', 'body') for field in fields):
|
||||||
encoding = self.get_encoding(path, method)
|
encoding = self.get_encoding(path, method)
|
||||||
else:
|
else:
|
||||||
encoding = None
|
encoding = None
|
||||||
|
|
|
@ -67,7 +67,7 @@ def get_unique_validators(field_name, model_field):
|
||||||
"""
|
"""
|
||||||
Returns a list of UniqueValidators that should be applied to the field.
|
Returns a list of UniqueValidators that should be applied to the field.
|
||||||
"""
|
"""
|
||||||
field_set = set([field_name])
|
field_set = {field_name}
|
||||||
conditions = {
|
conditions = {
|
||||||
c.condition
|
c.condition
|
||||||
for c in model_field.model._meta.constraints
|
for c in model_field.model._meta.constraints
|
||||||
|
|
|
@ -354,10 +354,10 @@ class APIView(View):
|
||||||
Check if request should be throttled.
|
Check if request should be throttled.
|
||||||
Raises an appropriate exception if the request is throttled.
|
Raises an appropriate exception if the request is throttled.
|
||||||
"""
|
"""
|
||||||
throttle_durations = []
|
throttle_durations = [
|
||||||
for throttle in self.get_throttles():
|
throttle.wait() for throttle in self.get_throttles()
|
||||||
if not throttle.allow_request(request, self):
|
if not throttle.allow_request(request, self)
|
||||||
throttle_durations.append(throttle.wait())
|
]
|
||||||
|
|
||||||
if throttle_durations:
|
if throttle_durations:
|
||||||
# Filter out `None` values which may happen in case of config / rate
|
# Filter out `None` values which may happen in case of config / rate
|
||||||
|
|
|
@ -262,7 +262,7 @@ class TestOperationIntrospection(TestCase):
|
||||||
|
|
||||||
components = inspector.get_components(path, method)
|
components = inspector.get_components(path, method)
|
||||||
assert components['Item']['required'] == ['text']
|
assert components['Item']['required'] == ['text']
|
||||||
assert sorted(list(components['Item']['properties'].keys())) == ['read_only', 'text']
|
assert sorted(components['Item']['properties'].keys()) == ['read_only', 'text']
|
||||||
|
|
||||||
def test_invalid_serializer_class_name(self):
|
def test_invalid_serializer_class_name(self):
|
||||||
path = '/'
|
path = '/'
|
||||||
|
@ -366,7 +366,7 @@ class TestOperationIntrospection(TestCase):
|
||||||
|
|
||||||
components = inspector.get_components(path, method)
|
components = inspector.get_components(path, method)
|
||||||
assert sorted(components['Item']['required']) == ['text', 'write_only']
|
assert sorted(components['Item']['required']) == ['text', 'write_only']
|
||||||
assert sorted(list(components['Item']['properties'].keys())) == ['text', 'write_only']
|
assert sorted(components['Item']['properties'].keys()) == ['text', 'write_only']
|
||||||
assert 'description' in responses['201']
|
assert 'description' in responses['201']
|
||||||
|
|
||||||
def test_response_body_nested_serializer(self):
|
def test_response_body_nested_serializer(self):
|
||||||
|
@ -398,7 +398,7 @@ class TestOperationIntrospection(TestCase):
|
||||||
|
|
||||||
schema = components['Item']
|
schema = components['Item']
|
||||||
assert sorted(schema['required']) == ['nested', 'text']
|
assert sorted(schema['required']) == ['nested', 'text']
|
||||||
assert sorted(list(schema['properties'].keys())) == ['nested', 'text']
|
assert sorted(schema['properties'].keys()) == ['nested', 'text']
|
||||||
assert schema['properties']['nested']['type'] == 'object'
|
assert schema['properties']['nested']['type'] == 'object'
|
||||||
assert list(schema['properties']['nested']['properties'].keys()) == ['number']
|
assert list(schema['properties']['nested']['properties'].keys()) == ['number']
|
||||||
assert schema['properties']['nested']['required'] == ['number']
|
assert schema['properties']['nested']['required'] == ['number']
|
||||||
|
|
|
@ -232,9 +232,7 @@ class TestRegularFieldMappings(TestCase):
|
||||||
model = NullableBooleanChoicesModel
|
model = NullableBooleanChoicesModel
|
||||||
fields = ['field']
|
fields = ['field']
|
||||||
|
|
||||||
serializer = NullableBooleanChoicesSerializer(data=dict(
|
serializer = NullableBooleanChoicesSerializer(data={'field': None})
|
||||||
field=None,
|
|
||||||
))
|
|
||||||
self.assertTrue(serializer.is_valid())
|
self.assertTrue(serializer.is_valid())
|
||||||
self.assertEqual(serializer.errors, {})
|
self.assertEqual(serializer.errors, {})
|
||||||
|
|
||||||
|
|
|
@ -55,16 +55,12 @@ class HeadersView(APIView):
|
||||||
|
|
||||||
class SessionView(APIView):
|
class SessionView(APIView):
|
||||||
def get(self, request):
|
def get(self, request):
|
||||||
return Response({
|
return Response(dict(request.session.items()))
|
||||||
key: value for key, value in request.session.items()
|
|
||||||
})
|
|
||||||
|
|
||||||
def post(self, request):
|
def post(self, request):
|
||||||
for key, value in request.data.items():
|
for key, value in request.data.items():
|
||||||
request.session[key] = value
|
request.session[key] = value
|
||||||
return Response({
|
return Response(dict(request.session.items()))
|
||||||
key: value for key, value in request.session.items()
|
|
||||||
})
|
|
||||||
|
|
||||||
|
|
||||||
class AuthView(APIView):
|
class AuthView(APIView):
|
||||||
|
|
|
@ -115,13 +115,13 @@ class TestUniquenessValidation(TestCase):
|
||||||
instance = AnotherUniquenessModel.objects.create(code='100')
|
instance = AnotherUniquenessModel.objects.create(code='100')
|
||||||
serializer = AnotherUniquenessSerializer(instance)
|
serializer = AnotherUniquenessSerializer(instance)
|
||||||
assert all(
|
assert all(
|
||||||
["Unique" not in repr(v) for v in AnotherUniquenessModel._meta.get_field('code').validators]
|
"Unique" not in repr(v) for v in AnotherUniquenessModel._meta.get_field('code').validators
|
||||||
)
|
)
|
||||||
|
|
||||||
# Accessing data shouldn't effect validators on the model
|
# Accessing data shouldn't effect validators on the model
|
||||||
serializer.data
|
serializer.data
|
||||||
assert all(
|
assert all(
|
||||||
["Unique" not in repr(v) for v in AnotherUniquenessModel._meta.get_field('code').validators]
|
"Unique" not in repr(v) for v in AnotherUniquenessModel._meta.get_field('code').validators
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_related_model_is_unique(self):
|
def test_related_model_is_unique(self):
|
||||||
|
|
|
@ -27,10 +27,8 @@ class MockQueryset:
|
||||||
|
|
||||||
def get(self, **lookup):
|
def get(self, **lookup):
|
||||||
for item in self.items:
|
for item in self.items:
|
||||||
if all([
|
if all(attrgetter(key.replace('__', '.'))(item) == value
|
||||||
attrgetter(key.replace('__', '.'))(item) == value
|
for key, value in lookup.items()):
|
||||||
for key, value in lookup.items()
|
|
||||||
]):
|
|
||||||
return item
|
return item
|
||||||
raise ObjectDoesNotExist()
|
raise ObjectDoesNotExist()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user