Skip to content

Commit

Permalink
Remove unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
peterthomassen committed Apr 30, 2024
1 parent 82d91a8 commit d38aab3
Show file tree
Hide file tree
Showing 13 changed files with 19 additions and 97 deletions.
6 changes: 0 additions & 6 deletions rest_framework/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
\_| \_\____/\____/ \_/ |_| |_| \__,_|_| |_| |_|\___| \_/\_/ \___/|_| |_|\_|
"""

import django

__title__ = 'Django REST framework'
__version__ = '3.15.1'
__author__ = 'Tom Christie'
Expand All @@ -25,10 +23,6 @@
ISO_8601 = 'iso-8601'


if django.VERSION < (3, 2):
default_app_config = 'rest_framework.apps.RestFrameworkConfig'


class RemovedInDRF315Warning(DeprecationWarning):
pass

Expand Down
4 changes: 0 additions & 4 deletions rest_framework/authtoken/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +0,0 @@
import django

if django.VERSION < (3, 2):
default_app_config = 'rest_framework.authtoken.apps.AuthTokenConfig'
24 changes: 0 additions & 24 deletions rest_framework/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,30 +151,6 @@ def md_filter_add_syntax_highlight(md):
return False


if django.VERSION >= (4, 2):
# Django 4.2+: use the stock parse_header_parameters function
# Note: Django 4.1 also has an implementation of parse_header_parameters
# which is slightly different from the one in 4.2, it needs
# the compatibility shim as well.
from django.utils.http import parse_header_parameters
else:
# Django <= 4.1: create a compatibility shim for parse_header_parameters
from django.http.multipartparser import parse_header

def parse_header_parameters(line):
# parse_header works with bytes, but parse_header_parameters
# works with strings. Call encode to convert the line to bytes.
main_value_pair, params = parse_header(line.encode())
return main_value_pair, {
# parse_header will convert *some* values to string.
# parse_header_parameters converts *all* values to string.
# Make sure all values are converted by calling decode on
# any remaining non-string values.
k: v if isinstance(v, str) else v.decode()
for k, v in params.items()
}


if django.VERSION >= (5, 1):
# Django 5.1+: use the stock ip_address_validators function
# Note: Before Django 5.1, ip_address_validators returns a tuple containing
Expand Down
4 changes: 0 additions & 4 deletions rest_framework/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,6 @@ def construct_search(self, field_name, queryset):
if hasattr(field, "path_infos"):
# Update opts to follow the relation.
opts = field.path_infos[-1].to_opts
# django < 4.1
elif hasattr(field, 'get_path_info'):
# Update opts to follow the relation.
opts = field.get_path_info()[-1].to_opts
# Otherwise, use the field with icontains.
lookup = 'icontains'
return LOOKUP_SEP.join([field_name, lookup])
Expand Down
2 changes: 1 addition & 1 deletion rest_framework/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
from django.http.multipartparser import \
MultiPartParser as DjangoMultiPartParser
from django.http.multipartparser import MultiPartParserError
from django.utils.http import parse_header_parameters

from rest_framework import renderers
from rest_framework.compat import parse_header_parameters
from rest_framework.exceptions import ParseError
from rest_framework.settings import api_settings
from rest_framework.utils import json
Expand Down
3 changes: 2 additions & 1 deletion rest_framework/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@
from django.template import engines, loader
from django.urls import NoReverseMatch
from django.utils.html import mark_safe
from django.utils.http import parse_header_parameters
from django.utils.safestring import SafeString

from rest_framework import VERSION, exceptions, serializers, status
from rest_framework.compat import (
INDENT_SEPARATORS, LONG_SEPARATORS, SHORT_SEPARATORS, coreapi, coreschema,
parse_header_parameters, pygments_css, yaml
pygments_css, yaml
)
from rest_framework.exceptions import ParseError
from rest_framework.request import is_form_media_type, override_method
Expand Down
2 changes: 1 addition & 1 deletion rest_framework/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
from django.http import HttpRequest, QueryDict
from django.http.request import RawPostDataException
from django.utils.datastructures import MultiValueDict
from django.utils.http import parse_header_parameters

from rest_framework import exceptions
from rest_framework.compat import parse_header_parameters
from rest_framework.settings import api_settings


Expand Down
17 changes: 2 additions & 15 deletions rest_framework/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import io
from importlib import import_module

import django
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.core.handlers.wsgi import WSGIHandler
Expand Down Expand Up @@ -394,19 +393,7 @@ def setUpClass(cls):

cls._override.enable()

if django.VERSION > (4, 0):
cls.addClassCleanup(cls._override.disable)
cls.addClassCleanup(cleanup_url_patterns, cls)
cls.addClassCleanup(cls._override.disable)
cls.addClassCleanup(cleanup_url_patterns, cls)

super().setUpClass()

if django.VERSION < (4, 0):
@classmethod
def tearDownClass(cls):
super().tearDownClass()
cls._override.disable()

if hasattr(cls, '_module_urlpatterns'):
cls._module.urlpatterns = cls._module_urlpatterns
else:
del cls._module.urlpatterns
2 changes: 1 addition & 1 deletion rest_framework/utils/mediatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
See https://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7
"""
from rest_framework.compat import parse_header_parameters
from django.utils.http import parse_header_parameters


def media_type_matches(lhs, rhs):
Expand Down
17 changes: 4 additions & 13 deletions tests/authentication/test_authentication.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import base64

import django
import pytest
from django.conf import settings
from django.contrib.auth.models import User
Expand Down Expand Up @@ -235,21 +234,13 @@ def test_post_form_session_auth_passing_csrf(self):
Ensure POSTing form over session authentication with CSRF token succeeds.
Regression test for #6088
"""
# Remove this shim when dropping support for Django 3.0.
if django.VERSION < (3, 1):
from django.middleware.csrf import _get_new_csrf_token
else:
from django.middleware.csrf import (
_get_new_csrf_string, _mask_cipher_secret
)

def _get_new_csrf_token():
return _mask_cipher_secret(_get_new_csrf_string())

self.csrf_client.login(username=self.username, password=self.password)

# Set the csrf_token cookie so that CsrfViewMiddleware._get_token() works
token = _get_new_csrf_token()
from django.middleware.csrf import (
_get_new_csrf_string, _mask_cipher_secret
)
token = _mask_cipher_secret(_get_new_csrf_string())
self.csrf_client.cookies[settings.CSRF_COOKIE_NAME] = token

# Post the token matching the cookie value
Expand Down
8 changes: 1 addition & 7 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ def pytest_addoption(parser):
def pytest_configure(config):
from django.conf import settings

# USE_L10N is deprecated, and will be removed in Django 5.0.
use_l10n = {"USE_L10N": True} if django.VERSION < (4, 0) else {}
settings.configure(
DEBUG_PROPAGATE_EXCEPTIONS=True,
DATABASES={
Expand Down Expand Up @@ -64,7 +62,6 @@ def pytest_configure(config):
PASSWORD_HASHERS=(
'django.contrib.auth.hashers.MD5PasswordHasher',
),
**use_l10n,
)

# guardian is optional
Expand All @@ -87,10 +84,7 @@ def pytest_configure(config):
import rest_framework
settings.STATIC_ROOT = os.path.join(os.path.dirname(rest_framework.__file__), 'static-root')
backend = 'django.contrib.staticfiles.storage.ManifestStaticFilesStorage'
if django.VERSION < (4, 2):
settings.STATICFILES_STORAGE = backend
else:
settings.STORAGES['staticfiles']['BACKEND'] = backend
settings.STORAGES['staticfiles']['BACKEND'] = backend

django.setup()

Expand Down
10 changes: 3 additions & 7 deletions tests/test_model_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import sys
import tempfile

import django
import pytest
from django.core.exceptions import ImproperlyConfigured
from django.core.serializers.json import DjangoJSONEncoder
Expand Down Expand Up @@ -453,14 +452,11 @@ class Meta:
model = ArrayFieldModel
fields = ['array_field', 'array_field_with_blank']

validators = ""
if django.VERSION < (4, 1):
validators = ", validators=[<django.core.validators.MaxLengthValidator object>]"
expected = dedent("""
TestSerializer():
array_field = ListField(allow_empty=False, child=CharField(label='Array field'%s))
array_field_with_blank = ListField(child=CharField(label='Array field with blank'%s), required=False)
""" % (validators, validators))
array_field = ListField(allow_empty=False, child=CharField(label='Array field'))
array_field_with_blank = ListField(child=CharField(label='Array field with blank'), required=False)
""")
self.assertEqual(repr(TestSerializer()), expected)

@pytest.mark.skipif(hasattr(models, 'JSONField'), reason='has models.JSONField')
Expand Down
17 changes: 4 additions & 13 deletions tests/test_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from io import BytesIO
from unittest.mock import patch

import django
from django.contrib.auth.models import User
from django.http import HttpResponseRedirect
from django.shortcuts import redirect
Expand Down Expand Up @@ -334,18 +333,10 @@ def setUpClass(cls):
super().setUpClass()
assert urlpatterns is cls.urlpatterns

if django.VERSION > (4, 0):
cls.addClassCleanup(
check_urlpatterns,
cls
)

if django.VERSION < (4, 0):
@classmethod
def tearDownClass(cls):
assert urlpatterns is cls.urlpatterns
super().tearDownClass()
assert urlpatterns is not cls.urlpatterns
cls.addClassCleanup(
check_urlpatterns,
cls
)

def test_urlpatterns(self):
assert self.client.get('/').status_code == 200
Expand Down

0 comments on commit d38aab3

Please sign in to comment.