mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-07-25 23:50:01 +03:00
replace try/except with context manager
If the desire is to simply suppress an error, rather than perform some sort of branching logic, the Python standard library has a paradigm for that: contextlib.suppress()
This commit is contained in:
parent
73aeae3a0f
commit
da500e361a
|
@ -1157,20 +1157,14 @@ class DateTimeField(Field):
|
|||
return self.enforce_timezone(value)
|
||||
|
||||
for input_format in input_formats:
|
||||
if input_format.lower() == ISO_8601:
|
||||
try:
|
||||
with contextlib.suppress(ValueError, TypeError):
|
||||
if input_format.lower() == ISO_8601:
|
||||
parsed = parse_datetime(value)
|
||||
if parsed is not None:
|
||||
return self.enforce_timezone(parsed)
|
||||
except (ValueError, TypeError):
|
||||
pass
|
||||
else:
|
||||
try:
|
||||
else:
|
||||
parsed = self.datetime_parser(value, input_format)
|
||||
return self.enforce_timezone(parsed)
|
||||
except (ValueError, TypeError):
|
||||
pass
|
||||
|
||||
humanized_format = humanize_datetime.datetime_formats(input_formats)
|
||||
self.fail('invalid', format=humanized_format)
|
||||
|
||||
|
|
|
@ -2,6 +2,8 @@
|
|||
Pagination serializers determine the structure of the output that should
|
||||
be used for paginated responses.
|
||||
"""
|
||||
|
||||
import contextlib
|
||||
from base64 import b64decode, b64encode
|
||||
from collections import OrderedDict, namedtuple
|
||||
from urllib import parse
|
||||
|
@ -257,15 +259,12 @@ class PageNumberPagination(BasePagination):
|
|||
|
||||
def get_page_size(self, request):
|
||||
if self.page_size_query_param:
|
||||
try:
|
||||
with contextlib.suppress(KeyError, ValueError):
|
||||
return _positive_int(
|
||||
request.query_params[self.page_size_query_param],
|
||||
strict=True,
|
||||
cutoff=self.max_page_size
|
||||
)
|
||||
except (KeyError, ValueError):
|
||||
pass
|
||||
|
||||
return self.page_size
|
||||
|
||||
def get_next_link(self):
|
||||
|
@ -430,15 +429,12 @@ class LimitOffsetPagination(BasePagination):
|
|||
|
||||
def get_limit(self, request):
|
||||
if self.limit_query_param:
|
||||
try:
|
||||
with contextlib.suppress(KeyError, ValueError):
|
||||
return _positive_int(
|
||||
request.query_params[self.limit_query_param],
|
||||
strict=True,
|
||||
cutoff=self.max_limit
|
||||
)
|
||||
except (KeyError, ValueError):
|
||||
pass
|
||||
|
||||
return self.default_limit
|
||||
|
||||
def get_offset(self, request):
|
||||
|
@ -680,15 +676,12 @@ class CursorPagination(BasePagination):
|
|||
|
||||
def get_page_size(self, request):
|
||||
if self.page_size_query_param:
|
||||
try:
|
||||
with contextlib.suppress(KeyError, ValueError):
|
||||
return _positive_int(
|
||||
request.query_params[self.page_size_query_param],
|
||||
strict=True,
|
||||
cutoff=self.max_page_size
|
||||
)
|
||||
except (KeyError, ValueError):
|
||||
pass
|
||||
|
||||
return self.page_size
|
||||
|
||||
def get_next_link(self):
|
||||
|
|
|
@ -4,8 +4,10 @@ Parsers are used to parse the content of incoming HTTP requests.
|
|||
They give us a generic way of being able to handle various media types
|
||||
on the request, such as form content or json encoded data.
|
||||
"""
|
||||
|
||||
import codecs
|
||||
|
||||
import contextlib
|
||||
from django.conf import settings
|
||||
from django.core.files.uploadhandler import StopFutureHandlers
|
||||
from django.http import QueryDict
|
||||
|
@ -193,17 +195,9 @@ class FileUploadParser(BaseParser):
|
|||
Detects the uploaded file name. First searches a 'filename' url kwarg.
|
||||
Then tries to parse Content-Disposition header.
|
||||
"""
|
||||
try:
|
||||
with contextlib.suppress(KeyError):
|
||||
return parser_context['kwargs']['filename']
|
||||
except KeyError:
|
||||
pass
|
||||
|
||||
try:
|
||||
with contextlib.suppress(AttributeError, KeyError, ValueError):
|
||||
meta = parser_context['request'].META
|
||||
disposition, params = parse_header_parameters(meta['HTTP_CONTENT_DISPOSITION'])
|
||||
if 'filename*' in params:
|
||||
return params['filename*']
|
||||
else:
|
||||
return params['filename']
|
||||
except (AttributeError, KeyError, ValueError):
|
||||
pass
|
||||
return params['filename*'] if 'filename*' in params else params['filename']
|
||||
|
|
|
@ -1502,7 +1502,7 @@ class ModelSerializer(Serializer):
|
|||
field = model._meta.get_field(source)
|
||||
if isinstance(field, DjangoModelField):
|
||||
model_fields[source] = field
|
||||
|
||||
|
||||
return model_fields
|
||||
|
||||
# Determine the validators to apply...
|
||||
|
|
|
@ -245,4 +245,4 @@ class ModelViewSet(mixins.CreateModelMixin,
|
|||
A viewset that provides default `create()`, `retrieve()`, `update()`,
|
||||
`partial_update()`, `destroy()` and `list()` actions.
|
||||
"""
|
||||
pass
|
||||
pass
|
||||
|
|
Loading…
Reference in New Issue
Block a user