diff --git a/rest_framework/exceptions.py b/rest_framework/exceptions.py index ba0c9458a..345a40524 100644 --- a/rest_framework/exceptions.py +++ b/rest_framework/exceptions.py @@ -7,7 +7,7 @@ In addition Django's built in 403 and 404 exceptions are handled. import math from django.http import JsonResponse -from django.utils.encoding import force_text +from django.utils.encoding import force_str from django.utils.translation import gettext_lazy as _ from django.utils.translation import ngettext @@ -36,7 +36,7 @@ def _get_error_details(data, default_code=None): return ReturnDict(ret, serializer=data.serializer) return ret - text = force_text(data) + text = force_str(data) code = getattr(data, 'code', default_code) return ErrorDetail(text, code) @@ -191,7 +191,7 @@ class MethodNotAllowed(APIException): def __init__(self, method, detail=None, code=None): if detail is None: - detail = force_text(self.default_detail).format(method=method) + detail = force_str(self.default_detail).format(method=method) super().__init__(detail, code) @@ -212,7 +212,7 @@ class UnsupportedMediaType(APIException): def __init__(self, media_type, detail=None, code=None): if detail is None: - detail = force_text(self.default_detail).format(media_type=media_type) + detail = force_str(self.default_detail).format(media_type=media_type) super().__init__(detail, code) @@ -225,14 +225,14 @@ class Throttled(APIException): def __init__(self, wait=None, detail=None, code=None): if detail is None: - detail = force_text(self.default_detail) + detail = force_str(self.default_detail) if wait is not None: wait = math.ceil(wait) detail = ' '.join(( detail, - force_text(ngettext(self.extra_detail_singular.format(wait=wait), - self.extra_detail_plural.format(wait=wait), - wait)))) + force_str(ngettext(self.extra_detail_singular.format(wait=wait), + self.extra_detail_plural.format(wait=wait), + wait)))) self.wait = wait super().__init__(detail, code) diff --git a/rest_framework/filters.py b/rest_framework/filters.py index c0c708d26..c15723ec3 100644 --- a/rest_framework/filters.py +++ b/rest_framework/filters.py @@ -10,7 +10,7 @@ from django.db import models from django.db.models.constants import LOOKUP_SEP from django.db.models.sql.constants import ORDER_PATTERN from django.template import loader -from django.utils.encoding import force_text +from django.utils.encoding import force_str from django.utils.translation import gettext_lazy as _ from rest_framework.compat import coreapi, coreschema, distinct @@ -151,8 +151,8 @@ class SearchFilter(BaseFilterBackend): required=False, location='query', schema=coreschema.String( - title=force_text(self.search_title), - description=force_text(self.search_description) + title=force_str(self.search_title), + description=force_str(self.search_description) ) ) ] @@ -163,7 +163,7 @@ class SearchFilter(BaseFilterBackend): 'name': self.search_param, 'required': False, 'in': 'query', - 'description': force_text(self.search_description), + 'description': force_str(self.search_description), 'schema': { 'type': 'string', }, @@ -295,8 +295,8 @@ class OrderingFilter(BaseFilterBackend): required=False, location='query', schema=coreschema.String( - title=force_text(self.ordering_title), - description=force_text(self.ordering_description) + title=force_str(self.ordering_title), + description=force_str(self.ordering_description) ) ) ] @@ -307,7 +307,7 @@ class OrderingFilter(BaseFilterBackend): 'name': self.ordering_param, 'required': False, 'in': 'query', - 'description': force_text(self.ordering_description), + 'description': force_str(self.ordering_description), 'schema': { 'type': 'string', }, diff --git a/rest_framework/metadata.py b/rest_framework/metadata.py index 42442f91c..8a44f2aad 100644 --- a/rest_framework/metadata.py +++ b/rest_framework/metadata.py @@ -10,7 +10,7 @@ from collections import OrderedDict from django.core.exceptions import PermissionDenied from django.http import Http404 -from django.utils.encoding import force_text +from django.utils.encoding import force_str from rest_framework import exceptions, serializers from rest_framework.request import clone_request @@ -130,7 +130,7 @@ class SimpleMetadata(BaseMetadata): for attr in attrs: value = getattr(field, attr, None) if value is not None and value != '': - field_info[attr] = force_text(value, strings_only=True) + field_info[attr] = force_str(value, strings_only=True) if getattr(field, 'child', None): field_info['child'] = self.get_field_info(field.child) @@ -143,7 +143,7 @@ class SimpleMetadata(BaseMetadata): field_info['choices'] = [ { 'value': choice_value, - 'display_name': force_text(choice_name, strings_only=True) + 'display_name': force_str(choice_name, strings_only=True) } for choice_value, choice_name in field.choices.items() ] diff --git a/rest_framework/pagination.py b/rest_framework/pagination.py index 4d65d080a..a68e8ab12 100644 --- a/rest_framework/pagination.py +++ b/rest_framework/pagination.py @@ -9,7 +9,7 @@ from urllib import parse from django.core.paginator import InvalidPage from django.core.paginator import Paginator as DjangoPaginator from django.template import loader -from django.utils.encoding import force_text +from django.utils.encoding import force_str from django.utils.translation import gettext_lazy as _ from rest_framework.compat import coreapi, coreschema @@ -286,7 +286,7 @@ class PageNumberPagination(BasePagination): location='query', schema=coreschema.Integer( title='Page', - description=force_text(self.page_query_description) + description=force_str(self.page_query_description) ) ) ] @@ -298,7 +298,7 @@ class PageNumberPagination(BasePagination): location='query', schema=coreschema.Integer( title='Page size', - description=force_text(self.page_size_query_description) + description=force_str(self.page_size_query_description) ) ) ) @@ -310,7 +310,7 @@ class PageNumberPagination(BasePagination): 'name': self.page_query_param, 'required': False, 'in': 'query', - 'description': force_text(self.page_query_description), + 'description': force_str(self.page_query_description), 'schema': { 'type': 'integer', }, @@ -322,7 +322,7 @@ class PageNumberPagination(BasePagination): 'name': self.page_size_query_param, 'required': False, 'in': 'query', - 'description': force_text(self.page_size_query_description), + 'description': force_str(self.page_size_query_description), 'schema': { 'type': 'integer', }, @@ -478,7 +478,7 @@ class LimitOffsetPagination(BasePagination): location='query', schema=coreschema.Integer( title='Limit', - description=force_text(self.limit_query_description) + description=force_str(self.limit_query_description) ) ), coreapi.Field( @@ -487,7 +487,7 @@ class LimitOffsetPagination(BasePagination): location='query', schema=coreschema.Integer( title='Offset', - description=force_text(self.offset_query_description) + description=force_str(self.offset_query_description) ) ) ] @@ -498,7 +498,7 @@ class LimitOffsetPagination(BasePagination): 'name': self.limit_query_param, 'required': False, 'in': 'query', - 'description': force_text(self.limit_query_description), + 'description': force_str(self.limit_query_description), 'schema': { 'type': 'integer', }, @@ -507,7 +507,7 @@ class LimitOffsetPagination(BasePagination): 'name': self.offset_query_param, 'required': False, 'in': 'query', - 'description': force_text(self.offset_query_description), + 'description': force_str(self.offset_query_description), 'schema': { 'type': 'integer', }, @@ -861,7 +861,7 @@ class CursorPagination(BasePagination): location='query', schema=coreschema.String( title='Cursor', - description=force_text(self.cursor_query_description) + description=force_str(self.cursor_query_description) ) ) ] @@ -873,7 +873,7 @@ class CursorPagination(BasePagination): location='query', schema=coreschema.Integer( title='Page size', - description=force_text(self.page_size_query_description) + description=force_str(self.page_size_query_description) ) ) ) @@ -885,7 +885,7 @@ class CursorPagination(BasePagination): 'name': self.cursor_query_param, 'required': False, 'in': 'query', - 'description': force_text(self.cursor_query_description), + 'description': force_str(self.cursor_query_description), 'schema': { 'type': 'integer', }, @@ -897,7 +897,7 @@ class CursorPagination(BasePagination): 'name': self.page_size_query_param, 'required': False, 'in': 'query', - 'description': force_text(self.page_size_query_description), + 'description': force_str(self.page_size_query_description), 'schema': { 'type': 'integer', }, diff --git a/rest_framework/parsers.py b/rest_framework/parsers.py index 978576a71..fc4eb1428 100644 --- a/rest_framework/parsers.py +++ b/rest_framework/parsers.py @@ -14,7 +14,7 @@ from django.http.multipartparser import ChunkIter from django.http.multipartparser import \ MultiPartParser as DjangoMultiPartParser from django.http.multipartparser import MultiPartParserError, parse_header -from django.utils.encoding import force_text +from django.utils.encoding import force_str from rest_framework import renderers from rest_framework.exceptions import ParseError @@ -205,7 +205,7 @@ class FileUploadParser(BaseParser): filename_parm = disposition[1] if 'filename*' in filename_parm: return self.get_encoded_filename(filename_parm) - return force_text(filename_parm['filename']) + return force_str(filename_parm['filename']) except (AttributeError, KeyError, ValueError): pass @@ -214,10 +214,10 @@ class FileUploadParser(BaseParser): Handle encoded filenames per RFC6266. See also: https://tools.ietf.org/html/rfc2231#section-4 """ - encoded_filename = force_text(filename_parm['filename*']) + encoded_filename = force_str(filename_parm['filename*']) try: charset, lang, filename = encoded_filename.split('\'', 2) filename = parse.unquote(filename) except (ValueError, LookupError): - filename = force_text(filename_parm['filename']) + filename = force_str(filename_parm['filename']) return filename diff --git a/rest_framework/schemas/coreapi.py b/rest_framework/schemas/coreapi.py index a2252ff79..8d9099f2a 100644 --- a/rest_framework/schemas/coreapi.py +++ b/rest_framework/schemas/coreapi.py @@ -4,7 +4,7 @@ from collections import Counter, OrderedDict from urllib import parse from django.db import models -from django.utils.encoding import force_text, smart_text +from django.utils.encoding import force_str, smart_text from rest_framework import exceptions, serializers from rest_framework.compat import coreapi, coreschema, uritemplate @@ -255,8 +255,8 @@ class SchemaGenerator(BaseSchemaGenerator): def field_to_schema(field): - title = force_text(field.label) if field.label else '' - description = force_text(field.help_text) if field.help_text else '' + title = force_str(field.label) if field.label else '' + description = force_str(field.help_text) if field.help_text else '' if isinstance(field, (serializers.ListSerializer, serializers.ListField)): child_schema = field_to_schema(field.child) @@ -457,10 +457,10 @@ class AutoSchema(ViewInspector): model_field = None if model_field is not None and model_field.verbose_name: - title = force_text(model_field.verbose_name) + title = force_str(model_field.verbose_name) if model_field is not None and model_field.help_text: - description = force_text(model_field.help_text) + description = force_str(model_field.help_text) elif model_field is not None and model_field.primary_key: description = get_pk_description(model, model_field) diff --git a/rest_framework/schemas/openapi.py b/rest_framework/schemas/openapi.py index 4050bc260..504689796 100644 --- a/rest_framework/schemas/openapi.py +++ b/rest_framework/schemas/openapi.py @@ -6,7 +6,7 @@ from django.core.validators import ( MinLengthValidator, MinValueValidator, RegexValidator, URLValidator ) from django.db import models -from django.utils.encoding import force_text +from django.utils.encoding import force_str from rest_framework import exceptions, serializers from rest_framework.compat import uritemplate @@ -162,7 +162,7 @@ class AutoSchema(ViewInspector): model_field = None if model_field is not None and model_field.help_text: - description = force_text(model_field.help_text) + description = force_str(model_field.help_text) elif model_field is not None and model_field.primary_key: description = get_pk_description(model, model_field) diff --git a/rest_framework/templatetags/rest_framework.py b/rest_framework/templatetags/rest_framework.py index e754c76c0..79dd953ff 100644 --- a/rest_framework/templatetags/rest_framework.py +++ b/rest_framework/templatetags/rest_framework.py @@ -4,7 +4,7 @@ from collections import OrderedDict from django import template from django.template import loader from django.urls import NoReverseMatch, reverse -from django.utils.encoding import force_text, iri_to_uri +from django.utils.encoding import force_str, iri_to_uri from django.utils.html import escape, format_html, smart_urlquote from django.utils.safestring import SafeData, mark_safe @@ -339,7 +339,7 @@ def urlize_quoted_links(text, trim_url_limit=None, nofollow=True, autoescape=Tru def conditional_escape(text): return escape(text) if autoescape and not safe_input else text - words = word_split_re.split(force_text(text)) + words = word_split_re.split(force_str(text)) for i, word in enumerate(words): if '.' in word or '@' in word or ':' in word: # Deal with punctuation. diff --git a/rest_framework/utils/encoders.py b/rest_framework/utils/encoders.py index dfd205d40..27293b725 100644 --- a/rest_framework/utils/encoders.py +++ b/rest_framework/utils/encoders.py @@ -8,7 +8,7 @@ import uuid from django.db.models.query import QuerySet from django.utils import timezone -from django.utils.encoding import force_text +from django.utils.encoding import force_str from django.utils.functional import Promise from rest_framework.compat import coreapi @@ -23,7 +23,7 @@ class JSONEncoder(json.JSONEncoder): # 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_text(obj) + return force_str(obj) elif isinstance(obj, datetime.datetime): representation = obj.isoformat() if representation.endswith('+00:00'): diff --git a/rest_framework/utils/formatting.py b/rest_framework/utils/formatting.py index e96cc6719..c5917fd41 100644 --- a/rest_framework/utils/formatting.py +++ b/rest_framework/utils/formatting.py @@ -3,7 +3,7 @@ Utility functions to return a formatted name and description for a given view. """ import re -from django.utils.encoding import force_text +from django.utils.encoding import force_str from django.utils.html import escape from django.utils.safestring import mark_safe @@ -29,7 +29,7 @@ def dedent(content): as it fails to dedent multiline docstrings that include unindented text on the initial line. """ - content = force_text(content) + content = force_str(content) lines = [line for line in content.splitlines()[1:] if line.lstrip()] # unindent the content if needed diff --git a/rest_framework/utils/representation.py b/rest_framework/utils/representation.py index 3916eb686..6f2efee16 100644 --- a/rest_framework/utils/representation.py +++ b/rest_framework/utils/representation.py @@ -5,7 +5,7 @@ of serializer classes and serializer fields. import re from django.db import models -from django.utils.encoding import force_text +from django.utils.encoding import force_str from django.utils.functional import Promise @@ -28,7 +28,7 @@ def smart_repr(value): return manager_repr(value) if isinstance(value, Promise) and value._delegate_text: - value = force_text(value) + value = force_str(value) value = repr(value) diff --git a/rest_framework/utils/serializer_helpers.py b/rest_framework/utils/serializer_helpers.py index 80aea27d3..b18fbe0df 100644 --- a/rest_framework/utils/serializer_helpers.py +++ b/rest_framework/utils/serializer_helpers.py @@ -1,7 +1,7 @@ from collections import OrderedDict from collections.abc import MutableMapping -from django.utils.encoding import force_text +from django.utils.encoding import force_str from rest_framework.utils import json @@ -123,7 +123,7 @@ class NestedBoundField(BoundField): if isinstance(value, (list, dict)): values[key] = value else: - values[key] = '' if (value is None or value is False) else force_text(value) + values[key] = '' if (value is None or value is False) else force_str(value) return self.__class__(self._field, values, self.errors, self._prefix)