[compat] Remove PY2 compat artefacts

- No need to specific types
- Remove __future__ imports
fix_request_path_info^2
Bastien Vallet 2020-08-04 14:43:07 +02:00
parent 85469082d0
commit 68605ba2ba
13 changed files with 9 additions and 58 deletions

View File

@ -29,7 +29,6 @@ can be included in a single Django migration. For example:
.. code-block:: python .. code-block:: python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models from django.db import migrations, models

View File

@ -1,8 +1,6 @@
""" """
The parent admin displays the list view of the base model. The parent admin displays the list view of the base model.
""" """
import sys
from django.contrib import admin from django.contrib import admin
from django.contrib.admin.helpers import AdminErrorList, AdminForm from django.contrib.admin.helpers import AdminErrorList, AdminForm
from django.contrib.admin.templatetags.admin_urls import add_preserved_filters 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.db import models
from django.http import Http404, HttpResponseRedirect from django.http import Http404, HttpResponseRedirect
from django.template.response import TemplateResponse from django.template.response import TemplateResponse
from django.urls import URLResolver
from django.utils.encoding import force_text from django.utils.encoding import force_text
from django.utils.http import urlencode from django.utils.http import urlencode
from django.utils.safestring import mark_safe from django.utils.safestring import mark_safe
@ -20,17 +19,6 @@ from polymorphic.utils import get_base_polymorphic_model
from .forms import PolymorphicModelChoiceForm 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): class RegistrationClosed(RuntimeError):
"The admin model can't be registered anymore at this point." "The admin model can't be registered anymore at this point."
@ -293,9 +281,9 @@ class PolymorphicParentModelAdmin(admin.ModelAdmin):
try: try:
pos = path.find("/") pos = path.find("/")
if pos == -1: if pos == -1:
object_id = long(path) object_id = int(path)
else: else:
object_id = long(path[0:pos]) object_id = int(path[0:pos])
except ValueError: except ValueError:
raise Http404( raise Http404(
"No ct_id parameter, unable to find admin subclass for path '{0}'.".format( "No ct_id parameter, unable to find admin subclass for path '{0}'.".format(

View File

@ -2,8 +2,6 @@
""" """
PolymorphicModel Meta Class PolymorphicModel Meta Class
""" """
from __future__ import absolute_import
import inspect import inspect
import os import os
import sys import sys

View File

@ -1,22 +1,4 @@
"""Compatibility with Python 2 (taken from 'django.utils.six')""" """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): def with_metaclass(meta, *bases):
class metaclass(type): class metaclass(type):

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. This package provides classes that support both options for polymorphic formsets.
""" """
from __future__ import absolute_import
import extra_views import extra_views
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured

View File

@ -2,7 +2,6 @@
""" """
The manager class for use in the models. The manager class for use in the models.
""" """
from __future__ import unicode_literals
from django.db import models from django.db import models

View File

@ -2,8 +2,6 @@
""" """
Seamless Polymorphic Inheritance for Django Models Seamless Polymorphic Inheritance for Django Models
""" """
from __future__ import absolute_import
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.db import models from django.db import models
from django.db.models.fields.related import ( from django.db.models.fields.related import (

View File

@ -2,8 +2,6 @@
""" """
QuerySet for PolymorphicModel QuerySet for PolymorphicModel
""" """
from __future__ import absolute_import
import copy import copy
from collections import defaultdict from collections import defaultdict
@ -177,7 +175,7 @@ class PolymorphicQuerySet(QuerySet):
"""translate the field paths in the args, then call vanilla order_by.""" """translate the field paths in the args, then call vanilla order_by."""
field_names = [ field_names = [
translate_polymorphic_field_path(self.model, a) translate_polymorphic_field_path(self.model, a)
if isinstance(a, compat.string_types) if isinstance(a, str)
else a # allow expressions to pass unchanged else a # allow expressions to pass unchanged
for a in field_names for a in field_names
] ]

View File

@ -2,8 +2,6 @@
""" """
PolymorphicQuerySet support functions PolymorphicQuerySet support functions
""" """
from __future__ import absolute_import
import copy import copy
from collections import deque from collections import deque
@ -144,7 +142,7 @@ def translate_polymorphic_field_path(queryset_model, field_path):
into modela__modelb__modelc__field3. into modela__modelb__modelc__field3.
Returns: translated path (unchanged, if no translation needed) 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)) raise ValueError("Expected field name as string: {0}".format(field_path))
classname, sep, pure_field_path = field_path.partition("___") classname, sep, pure_field_path = field_path.partition("___")

View File

@ -40,7 +40,7 @@ class ShowFieldBase(object):
out += content.__class__.__name__ out += content.__class__.__name__
elif issubclass(field_type, models.ManyToManyField): elif issubclass(field_type, models.ManyToManyField):
out += "%d" % content.count() out += "%d" % content.count()
elif isinstance(content, compat.integer_types): elif isinstance(content, int):
out += str(content) out += str(content)
elif content is None: elif content is None:
out += "None" out += "None"

View File

@ -31,7 +31,7 @@ class BreadcrumbScope(Node):
# Instead, have an assignment tag that inserts that in the template. # Instead, have an assignment tag that inserts that in the template.
base_opts = self.base_opts.resolve(context) base_opts = self.base_opts.resolve(context)
new_vars = {} new_vars = {}
if base_opts and not isinstance(base_opts, compat.string_types): if base_opts and not isinstance(base_opts, str):
new_vars = { new_vars = {
"app_label": base_opts.app_label, # What this is all about "app_label": base_opts.app_label, # What this is all about
"opts": base_opts, "opts": base_opts,

View File

@ -1,5 +1,3 @@
from __future__ import print_function
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.db.models import Q from django.db.models import Q
from django.test import TestCase from django.test import TestCase

View File

@ -1,5 +1,3 @@
import sys
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.db import DEFAULT_DB_ALIAS 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. Sort a series of models by their inheritance order.
""" """
if sys.version_info[0] == 2: from functools import cmp_to_key
return sorted(classes, cmp=_compare_mro)
else:
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): def get_base_polymorphic_model(ChildModel, allow_abstract=False):