Skip to content

Commit

Permalink
Merge pull request #567 from akx/version-checks
Browse files Browse the repository at this point in the history
Remove unnecessary version checks
  • Loading branch information
j-antunes authored Dec 30, 2023
2 parents 836a445 + 8ea4932 commit f188df0
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 81 deletions.
17 changes: 1 addition & 16 deletions polymorphic/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import sys
import warnings

import django
from django.db import models
from django.db.models.base import ModelBase

Expand Down Expand Up @@ -53,21 +52,10 @@ class PolymorphicModelBase(ModelBase):
"""

def __new__(self, model_name, bases, attrs, **kwargs):
# print; print '###', model_name, '- bases:', bases

# Workaround compatibility issue with six.with_metaclass() and custom Django model metaclasses:
if not attrs and model_name == "NewBase":
return super().__new__(self, model_name, bases, attrs, **kwargs)

# Make sure that manager_inheritance_from_future is set, since django-polymorphic 1.x already
# simulated that behavior on the polymorphic manager to all subclasses behave like polymorphics
if django.VERSION < (2, 0):
if "Meta" in attrs:
if not hasattr(attrs["Meta"], "manager_inheritance_from_future"):
attrs["Meta"].manager_inheritance_from_future = True
else:
attrs["Meta"] = type("Meta", (object,), {"manager_inheritance_from_future": True})

# create new model
new_class = self.call_superclass_new_method(model_name, bases, attrs, **kwargs)

Expand Down Expand Up @@ -130,10 +118,7 @@ def validate_model_manager(self, manager, model_name, manager_name):
and its querysets from PolymorphicQuerySet - throw AssertionError if not"""

if not issubclass(type(manager), PolymorphicManager):
if django.VERSION < (2, 0):
extra = "\nConsider using Meta.manager_inheritance_from_future = True for Django 1.x projects"
else:
extra = ""
extra = ""
e = (
f'PolymorphicModel: "{model_name}.{manager_name}" manager is of type "{type(manager).__name__}", '
f"but must be a subclass of PolymorphicManager.{extra} to support retrieving subclasses"
Expand Down
9 changes: 0 additions & 9 deletions polymorphic/compat.py

This file was deleted.

13 changes: 2 additions & 11 deletions polymorphic/formsets/utils.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
"""
Internal utils
"""
import django


def add_media(dest, media):
"""
Optimized version of django.forms.Media.__add__() that doesn't create new objects.
"""
if django.VERSION >= (2, 2):
dest._css_lists.extend(media._css_lists)
dest._js_lists.extend(media._js_lists)
elif django.VERSION >= (2, 0):
combined = dest + media
dest._css = combined._css
dest._js = combined._js
else:
dest.add_css(media._css)
dest.add_js(media._js)
dest._css_lists.extend(media._css_lists)
dest._js_lists.extend(media._js_lists)
4 changes: 1 addition & 3 deletions polymorphic/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
from django.db.models.fields.related import ForwardManyToOneDescriptor, ReverseOneToOneDescriptor
from django.db.utils import DEFAULT_DB_ALIAS

from polymorphic.compat import with_metaclass

from .base import PolymorphicModelBase
from .managers import PolymorphicManager
from .query_translate import translate_polymorphic_Q_object
Expand All @@ -24,7 +22,7 @@ class PolymorphicTypeInvalid(RuntimeError):
pass


class PolymorphicModel(with_metaclass(PolymorphicModelBase, models.Model)):
class PolymorphicModel(models.Model, metaclass=PolymorphicModelBase):
"""
Abstract base class that provides polymorphic behaviour
for any model directly or indirectly derived from it.
Expand Down
41 changes: 11 additions & 30 deletions polymorphic/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import copy
from collections import defaultdict

from django import get_version as get_django_version
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import FieldDoesNotExist
from django.db.models import FilteredRelation
Expand Down Expand Up @@ -157,35 +156,17 @@ def not_instance_of(self, *args):
# Implementation in _translate_polymorphic_filter_defnition."""
return self.filter(not_instance_of=args)

# Makes _filter_or_exclude compatible with the change in signature introduced in django at 9c9a3fe
if get_django_version() >= "3.2":

def _filter_or_exclude(self, negate, args, kwargs):
# We override this internal Django function as it is used for all filter member functions.
q_objects = translate_polymorphic_filter_definitions_in_args(
queryset_model=self.model, args=args, using=self.db
)
# filter_field='data'
additional_args = translate_polymorphic_filter_definitions_in_kwargs(
queryset_model=self.model, kwargs=kwargs, using=self.db
)
args = list(q_objects) + additional_args
return super()._filter_or_exclude(negate=negate, args=args, kwargs=kwargs)

else:

def _filter_or_exclude(self, negate, *args, **kwargs):
# We override this internal Django function as it is used for all filter member functions.
q_objects = translate_polymorphic_filter_definitions_in_args(
self.model, args, using=self.db
)
# filter_field='data'
additional_args = translate_polymorphic_filter_definitions_in_kwargs(
self.model, kwargs, using=self.db
)
return super()._filter_or_exclude(
negate, *(list(q_objects) + additional_args), **kwargs
)
def _filter_or_exclude(self, negate, args, kwargs):
# We override this internal Django function as it is used for all filter member functions.
q_objects = translate_polymorphic_filter_definitions_in_args(
queryset_model=self.model, args=args, using=self.db
)
# filter_field='data'
additional_args = translate_polymorphic_filter_definitions_in_kwargs(
queryset_model=self.model, kwargs=kwargs, using=self.db
)
args = list(q_objects) + additional_args
return super()._filter_or_exclude(negate=negate, args=args, kwargs=kwargs)

def order_by(self, *field_names):
"""translate the field paths in the args, then call vanilla order_by."""
Expand Down
2 changes: 1 addition & 1 deletion polymorphic/query_translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def translate_polymorphic_filter_definitions_in_kwargs(
Returns: a list of non-keyword-arguments (Q objects) to be added to the filter() query.
"""
additional_args = []
for field_path, val in kwargs.copy().items(): # Python 3 needs copy
for field_path, val in kwargs.copy().items(): # `copy` so we're not mutating the dict
new_expr = _translate_polymorphic_filter_definition(
queryset_model, field_path, val, using=using
)
Expand Down
5 changes: 1 addition & 4 deletions polymorphic/tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,7 @@ class MROBase3(models.Model):


class MRODerived(MROBase2, MROBase3):
if django.VERSION < (3, 0):

class Meta:
manager_inheritance_from_future = True
pass


class ParentModelWithManager(PolymorphicModel):
Expand Down
2 changes: 1 addition & 1 deletion polymorphic/tests/test_orm.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from django.db.utils import IntegrityError
from django.test import TransactionTestCase

from polymorphic import compat, query_translate
from polymorphic import query_translate
from polymorphic.managers import PolymorphicManager
from polymorphic.models import PolymorphicTypeInvalid, PolymorphicTypeUndefined
from polymorphic.tests.models import (
Expand Down
15 changes: 9 additions & 6 deletions polymorphic/tests/test_regression.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import django
from django.test import TestCase

from polymorphic.tests.models import Bottom, Middle, Top

transform_arg = {"transform": repr} if django.VERSION >= (3, 2) else {}


class RegressionTests(TestCase):
def test_for_query_result_incomplete_with_inheritance(self):
Expand All @@ -19,15 +16,21 @@ def test_for_query_result_incomplete_with_inheritance(self):

expected_queryset = [top, middle, bottom]
self.assertQuerySetEqual(
Top.objects.order_by("pk"), [repr(r) for r in expected_queryset], **transform_arg
Top.objects.order_by("pk"),
[repr(r) for r in expected_queryset],
transform=repr,
)

expected_queryset = [middle, bottom]
self.assertQuerySetEqual(
Middle.objects.order_by("pk"), [repr(r) for r in expected_queryset], **transform_arg
Middle.objects.order_by("pk"),
[repr(r) for r in expected_queryset],
transform=repr,
)

expected_queryset = [bottom]
self.assertQuerySetEqual(
Bottom.objects.order_by("pk"), [repr(r) for r in expected_queryset], **transform_arg
Bottom.objects.order_by("pk"),
[repr(r) for r in expected_queryset],
transform=repr,
)

0 comments on commit f188df0

Please sign in to comment.