mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-14 21:56:59 +03:00
9cfa4bd7cc
* fix OpenAPIRenderer for timedelta * added test for rendering openapi with timedelta * fix OpenAPIRenderer for timedelta * added test for rendering openapi with timedelta * Removed usage of field.choices that triggered full table load (#8950) Removed the `{{ field.choices|yesno:",disabled" }}` block because this triggers the loading of full database table worth of objects just to determine whether the multi-select widget should be set as disabled or not. Since this "disabled" marking feature is not present in the normal select field, then I propose to remove it also from the multi-select. * Added Deprecation Warnings for CoreAPI (#7519) * Added Deprecation Warnings for CoreAPI * Bumped removal to DRF315 * Update rest_framework/__init__.py * Update rest_framework/filters.py * Update rest_framework/filters.py * Update tests/schemas/test_coreapi.py * Update rest_framework/filters.py * Update rest_framework/filters.py * Update tests/schemas/test_coreapi.py * Update tests/schemas/test_coreapi.py * Update setup.cfg * Update rest_framework/pagination.py --------- Co-authored-by: Asif Saif Uddin <auvipy@gmail.com> * Update copy right timeline * Fix NamespaceVersioning ignoring DEFAULT_VERSION on non-None namespaces (#7278) * Fix the case where if the namespace is not None and there's no match, NamespaceVersioning always raises NotFound even if DEFAULT_VERSION is set or None is in ALLOWED_VERSIONS * Add test cases * fix OpenAPIRenderer for timedelta * added test for rendering openapi with timedelta * added testcase for rendering yaml with minvalidator for duration field (timedelta) --------- Co-authored-by: Rizwan Shaikh <rshaikh@ces-ltd.com> Co-authored-by: Lenno Nagel <lenno@namespace.ee> Co-authored-by: David Smith <39445562+smithdc1@users.noreply.github.com> Co-authored-by: Asif Saif Uddin <auvipy@gmail.com> Co-authored-by: Konstantin Kuchkov <konstantin.kuchkov@gmail.com>
79 lines
2.8 KiB
Python
79 lines
2.8 KiB
Python
"""
|
|
Helper classes for parsers.
|
|
"""
|
|
|
|
import contextlib
|
|
import datetime
|
|
import decimal
|
|
import json # noqa
|
|
import uuid
|
|
|
|
from django.db.models.query import QuerySet
|
|
from django.utils import timezone
|
|
from django.utils.encoding import force_str
|
|
from django.utils.functional import Promise
|
|
|
|
from rest_framework.compat import coreapi
|
|
|
|
|
|
class JSONEncoder(json.JSONEncoder):
|
|
"""
|
|
JSONEncoder subclass that knows how to encode date/time/timedelta,
|
|
decimal types, generators and other basic python objects.
|
|
"""
|
|
def default(self, obj):
|
|
# For Date Time string spec, see ECMA 262
|
|
# https://ecma-international.org/ecma-262/5.1/#sec-15.9.1.15
|
|
if isinstance(obj, Promise):
|
|
return force_str(obj)
|
|
elif isinstance(obj, datetime.datetime):
|
|
representation = obj.isoformat()
|
|
if representation.endswith('+00:00'):
|
|
representation = representation[:-6] + 'Z'
|
|
return representation
|
|
elif isinstance(obj, datetime.date):
|
|
return obj.isoformat()
|
|
elif isinstance(obj, datetime.time):
|
|
if timezone and timezone.is_aware(obj):
|
|
raise ValueError("JSON can't represent timezone-aware times.")
|
|
representation = obj.isoformat()
|
|
return representation
|
|
elif isinstance(obj, datetime.timedelta):
|
|
return str(obj.total_seconds())
|
|
elif isinstance(obj, decimal.Decimal):
|
|
# Serializers will coerce decimals to strings by default.
|
|
return float(obj)
|
|
elif isinstance(obj, uuid.UUID):
|
|
return str(obj)
|
|
elif isinstance(obj, QuerySet):
|
|
return tuple(obj)
|
|
elif isinstance(obj, bytes):
|
|
# Best-effort for binary blobs. See #4187.
|
|
return obj.decode()
|
|
elif hasattr(obj, 'tolist'):
|
|
# Numpy arrays and array scalars.
|
|
return obj.tolist()
|
|
elif (coreapi is not None) and isinstance(obj, (coreapi.Document, coreapi.Error)):
|
|
raise RuntimeError(
|
|
'Cannot return a coreapi object from a JSON view. '
|
|
'You should be using a schema renderer instead for this view.'
|
|
)
|
|
elif hasattr(obj, '__getitem__'):
|
|
cls = (list if isinstance(obj, (list, tuple)) else dict)
|
|
with contextlib.suppress(Exception):
|
|
return cls(obj)
|
|
elif hasattr(obj, '__iter__'):
|
|
return tuple(item for item in obj)
|
|
return super().default(obj)
|
|
|
|
|
|
class CustomScalar:
|
|
"""
|
|
CustomScalar that knows how to encode timedelta that renderer
|
|
can understand.
|
|
"""
|
|
@classmethod
|
|
def represent_timedelta(cls, dumper, data):
|
|
value = str(data.total_seconds())
|
|
return dumper.represent_scalar('tag:yaml.org,2002:str', value)
|