Skip to content

Commit 56f5fab

Browse files
committed
Removed the occurances of django>=(5,1) checking and usaage of pytz.
1 parent 2841bf9 commit 56f5fab

7 files changed

Lines changed: 5 additions & 75 deletions

File tree

rest_framework/compat.py

Lines changed: 2 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22
The `compat` module provides support for backwards compatibility with older
33
versions of Django/Python, and compatibility wrappers around optional packages.
44
"""
5-
import django
6-
from django.db import models
7-
from django.db.models.constants import LOOKUP_SEP
8-
from django.db.models.sql.query import Node
95
from django.views.generic import View
106

117

@@ -141,52 +137,8 @@ def md_filter_add_syntax_highlight(md):
141137
return False
142138

143139

144-
if django.VERSION >= (5, 1):
145-
# Django 5.1+: use the stock ip_address_validators function
146-
# Note: Before Django 5.1, ip_address_validators returns a tuple containing
147-
# 1) the list of validators and 2) the error message. Starting from
148-
# Django 5.1 ip_address_validators only returns the list of validators
149-
from django.core.validators import ip_address_validators
150-
151-
def get_referenced_base_fields_from_q(q):
152-
return q.referenced_base_fields
153-
154-
else:
155-
# Django <= 5.1: create a compatibility shim for ip_address_validators
156-
from django.core.validators import \
157-
ip_address_validators as _ip_address_validators
158-
159-
def ip_address_validators(protocol, unpack_ipv4):
160-
return _ip_address_validators(protocol, unpack_ipv4)[0]
161-
162-
# Django < 5.1: create a compatibility shim for Q.referenced_base_fields
163-
# https://github.com/django/django/blob/5.1a1/django/db/models/query_utils.py#L179
164-
def _get_paths_from_expression(expr):
165-
if isinstance(expr, models.F):
166-
yield expr.name
167-
elif hasattr(expr, 'flatten'):
168-
for child in expr.flatten():
169-
if isinstance(child, models.F):
170-
yield child.name
171-
elif isinstance(child, models.Q):
172-
yield from _get_children_from_q(child)
173-
174-
def _get_children_from_q(q):
175-
for child in q.children:
176-
if isinstance(child, Node):
177-
yield from _get_children_from_q(child)
178-
elif isinstance(child, tuple):
179-
lhs, rhs = child
180-
yield lhs
181-
if hasattr(rhs, 'resolve_expression'):
182-
yield from _get_paths_from_expression(rhs)
183-
elif hasattr(child, 'resolve_expression'):
184-
yield from _get_paths_from_expression(child)
185-
186-
def get_referenced_base_fields_from_q(q):
187-
return {
188-
child.split(LOOKUP_SEP, 1)[0] for child in _get_children_from_q(q)
189-
}
140+
def get_referenced_base_fields_from_q(q):
141+
return q.referenced_base_fields
190142

191143

192144
# `separators` argument to `json.dumps()` differs between 2.x and 3.x

rest_framework/fields.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from django.core.validators import (
1717
EmailValidator, MaxLengthValidator, MaxValueValidator, MinLengthValidator,
1818
MinValueValidator, ProhibitNullCharactersValidator, RegexValidator,
19-
URLValidator
19+
URLValidator, ip_address_validators
2020
)
2121
from django.forms import FilePathField as DjangoFilePathField
2222
from django.forms import ImageField as DjangoImageField
@@ -30,13 +30,7 @@
3030
from django.utils.ipv6 import clean_ipv6_address
3131
from django.utils.translation import gettext_lazy as _
3232

33-
try:
34-
import pytz
35-
except ImportError:
36-
pytz = None
37-
3833
from rest_framework import DJANGO_DURATION_FORMAT, ISO_8601
39-
from rest_framework.compat import ip_address_validators
4034
from rest_framework.exceptions import ErrorDetail, ValidationError
4135
from rest_framework.settings import api_settings
4236
from rest_framework.utils import html, humanize_datetime, json, representation
@@ -1185,8 +1179,6 @@ def enforce_timezone(self, value):
11851179
self.fail('make_aware', timezone=field_timezone)
11861180
return dt
11871181
except Exception as e:
1188-
if pytz and isinstance(e, pytz.exceptions.InvalidTimeError):
1189-
self.fail('make_aware', timezone=field_timezone)
11901182
raise e
11911183
elif (field_timezone is None) and timezone.is_aware(value):
11921184
return timezone.make_naive(value, datetime.timezone.utc)

rest_framework/views.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""
22
Provides an APIView class that is the base of all views in REST framework.
33
"""
4-
from django import VERSION as DJANGO_VERSION
54
from django.conf import settings
65
from django.core.exceptions import PermissionDenied
76
from django.db import connections, models
@@ -142,8 +141,7 @@ def force_evaluation():
142141

143142
# Exempt all DRF views from Django's LoginRequiredMiddleware. Users should set
144143
# DEFAULT_PERMISSION_CLASSES to 'rest_framework.permissions.IsAuthenticated' instead
145-
if DJANGO_VERSION >= (5, 1):
146-
view.login_required = False
144+
view.login_required = False
147145

148146
# Note: session based authentication is explicitly CSRF validated,
149147
# all other authentication is CSRF exempt.

rest_framework/viewsets.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
from functools import update_wrapper
2020
from inspect import getmembers
2121

22-
from django import VERSION as DJANGO_VERSION
2322
from django.urls import NoReverseMatch
2423
from django.utils.decorators import classonlymethod
2524
from django.views.decorators.csrf import csrf_exempt
@@ -140,8 +139,7 @@ def view(request, *args, **kwargs):
140139

141140
# Exempt from Django's LoginRequiredMiddleware. Users should set
142141
# DEFAULT_PERMISSION_CLASSES to 'rest_framework.permissions.IsAuthenticated' instead
143-
if DJANGO_VERSION >= (5, 1):
144-
view.login_required = False
142+
view.login_required = False
145143

146144
return csrf_exempt(view)
147145

tests/test_middleware.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import unittest
2-
3-
import django
41
from django.contrib.auth.models import User
52
from django.http import HttpRequest
63
from django.test import override_settings
@@ -113,7 +110,6 @@ def test_middleware_can_access_request_post_when_processing_response(self):
113110
assert response.status_code == 200
114111

115112

116-
@unittest.skipUnless(django.VERSION >= (5, 1), 'Only for Django 5.1+')
117113
@override_settings(
118114
ROOT_URLCONF='tests.test_middleware',
119115
MIDDLEWARE=(

tests/test_views.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import copy
2-
import unittest
32

4-
from django import VERSION as DJANGO_VERSION
53
from django.test import TestCase
64

75
from rest_framework import status
@@ -140,7 +138,6 @@ def test_get_exception_handler(self):
140138
assert response.data == {'error': 'SyntaxError'}
141139

142140

143-
@unittest.skipUnless(DJANGO_VERSION >= (5, 1), 'Only for Django 5.1+')
144141
class TestLoginRequiredMiddlewareCompat(TestCase):
145142
def test_class_based_view_opted_out(self):
146143
class_based_view = BasicView.as_view()

tests/test_viewsets.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import unittest
21
from functools import wraps
32

43
import pytest
5-
from django import VERSION as DJANGO_VERSION
64
from django.db import models
75
from django.test import TestCase, override_settings
86
from django.urls import include, path
@@ -198,7 +196,6 @@ def test_viewset_action_attr_for_extra_action(self):
198196
assert get.view.action == 'list_action'
199197
assert head.view.action == 'list_action'
200198

201-
@unittest.skipUnless(DJANGO_VERSION >= (5, 1), 'Only for Django 5.1+')
202199
def test_login_required_middleware_compat(self):
203200
view = ActionViewSet.as_view(actions={'get': 'list'})
204201
assert view.login_required is False

0 commit comments

Comments
 (0)