[compat] Remove PY2 compat artefacts

- No need to specific types
- Remove __future__ imports
This commit is contained in:
Bastien Vallet
2020-08-04 14:43:07 +02:00
parent 85469082d0
commit 68605ba2ba
13 changed files with 9 additions and 58 deletions
+3 -15
View File
@@ -1,8 +1,6 @@
"""
The parent admin displays the list view of the base model.
"""
import sys
from django.contrib import admin
from django.contrib.admin.helpers import AdminErrorList, AdminForm
from django.contrib.admin.templatetags.admin_urls import add_preserved_filters
@@ -11,6 +9,7 @@ from django.core.exceptions import ImproperlyConfigured, PermissionDenied
from django.db import models
from django.http import Http404, HttpResponseRedirect
from django.template.response import TemplateResponse
from django.urls import URLResolver
from django.utils.encoding import force_text
from django.utils.http import urlencode
from django.utils.safestring import mark_safe
@@ -20,17 +19,6 @@ from polymorphic.utils import get_base_polymorphic_model
from .forms import PolymorphicModelChoiceForm
try:
# Django 2.0+
from django.urls import URLResolver
except ImportError:
# Django < 2.0
from django.urls import RegexURLResolver as URLResolver
if sys.version_info[0] >= 3:
long = int
class RegistrationClosed(RuntimeError):
"The admin model can't be registered anymore at this point."
@@ -293,9 +281,9 @@ class PolymorphicParentModelAdmin(admin.ModelAdmin):
try:
pos = path.find("/")
if pos == -1:
object_id = long(path)
object_id = int(path)
else:
object_id = long(path[0:pos])
object_id = int(path[0:pos])
except ValueError:
raise Http404(
"No ct_id parameter, unable to find admin subclass for path '{0}'.".format(
-2
View File
@@ -2,8 +2,6 @@
"""
PolymorphicModel Meta Class
"""
from __future__ import absolute_import
import inspect
import os
import sys
-18
View File
@@ -1,22 +1,4 @@
"""Compatibility with Python 2 (taken from 'django.utils.six')"""
import sys
PY2 = sys.version_info[0] == 2
PY3 = sys.version_info[0] == 3
if PY3:
string_types = (str,)
integer_types = (int,)
class_types = (type,)
text_type = str
binary_type = bytes
MAXSIZE = sys.maxsize
else:
string_types = (basestring,)
integer_types = (int, long)
def with_metaclass(meta, *bases):
class metaclass(type):
-2
View File
@@ -4,8 +4,6 @@ The ``extra_views.advanced`` provides a method to combine that with a create/upd
This package provides classes that support both options for polymorphic formsets.
"""
from __future__ import absolute_import
import extra_views
from django.core.exceptions import ImproperlyConfigured
-1
View File
@@ -2,7 +2,6 @@
"""
The manager class for use in the models.
"""
from __future__ import unicode_literals
from django.db import models
-2
View File
@@ -2,8 +2,6 @@
"""
Seamless Polymorphic Inheritance for Django Models
"""
from __future__ import absolute_import
from django.contrib.contenttypes.models import ContentType
from django.db import models
from django.db.models.fields.related import (
+1 -3
View File
@@ -2,8 +2,6 @@
"""
QuerySet for PolymorphicModel
"""
from __future__ import absolute_import
import copy
from collections import defaultdict
@@ -177,7 +175,7 @@ class PolymorphicQuerySet(QuerySet):
"""translate the field paths in the args, then call vanilla order_by."""
field_names = [
translate_polymorphic_field_path(self.model, a)
if isinstance(a, compat.string_types)
if isinstance(a, str)
else a # allow expressions to pass unchanged
for a in field_names
]
+1 -3
View File
@@ -2,8 +2,6 @@
"""
PolymorphicQuerySet support functions
"""
from __future__ import absolute_import
import copy
from collections import deque
@@ -144,7 +142,7 @@ def translate_polymorphic_field_path(queryset_model, field_path):
into modela__modelb__modelc__field3.
Returns: translated path (unchanged, if no translation needed)
"""
if not isinstance(field_path, compat.string_types):
if not isinstance(field_path, str):
raise ValueError("Expected field name as string: {0}".format(field_path))
classname, sep, pure_field_path = field_path.partition("___")
+1 -1
View File
@@ -40,7 +40,7 @@ class ShowFieldBase(object):
out += content.__class__.__name__
elif issubclass(field_type, models.ManyToManyField):
out += "%d" % content.count()
elif isinstance(content, compat.integer_types):
elif isinstance(content, int):
out += str(content)
elif content is None:
out += "None"
@@ -31,7 +31,7 @@ class BreadcrumbScope(Node):
# Instead, have an assignment tag that inserts that in the template.
base_opts = self.base_opts.resolve(context)
new_vars = {}
if base_opts and not isinstance(base_opts, compat.string_types):
if base_opts and not isinstance(base_opts, str):
new_vars = {
"app_label": base_opts.app_label, # What this is all about
"opts": base_opts,
-2
View File
@@ -1,5 +1,3 @@
from __future__ import print_function
from django.contrib.contenttypes.models import ContentType
from django.db.models import Q
from django.test import TestCase
+2 -7
View File
@@ -1,5 +1,3 @@
import sys
from django.contrib.contenttypes.models import ContentType
from django.db import DEFAULT_DB_ALIAS
@@ -59,12 +57,9 @@ def sort_by_subclass(*classes):
"""
Sort a series of models by their inheritance order.
"""
if sys.version_info[0] == 2:
return sorted(classes, cmp=_compare_mro)
else:
from functools import cmp_to_key
from functools import cmp_to_key
return sorted(classes, key=cmp_to_key(_compare_mro))
return sorted(classes, key=cmp_to_key(_compare_mro))
def get_base_polymorphic_model(ChildModel, allow_abstract=False):