Skip to content

Commit

Permalink
chore: Remove code for old django versions
Browse files Browse the repository at this point in the history
  • Loading branch information
last-partizan committed Jun 1, 2024
1 parent 9d9a2ab commit d0b8e70
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 65 deletions.
8 changes: 0 additions & 8 deletions modeltranslation/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
from modeltranslation._typing import monkeypatch

try:
from django import VERSION as _django_version

if _django_version < (3, 2):
default_app_config = "modeltranslation.apps.ModeltranslationConfig"
except ImportError:
pass

# monkeypatch generic classes at runtime
monkeypatch()
23 changes: 2 additions & 21 deletions modeltranslation/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import Any, cast
from collections.abc import Sequence

from django import VERSION, forms
from django import forms
from django.core.exceptions import ImproperlyConfigured
from django.db.models import Model, fields
from django.utils.encoding import force_str
Expand Down Expand Up @@ -49,8 +49,6 @@
fields.related.ManyToManyField,
)

NEW_RELATED_API = VERSION >= (1, 9)


class NONE:
"""
Expand Down Expand Up @@ -220,24 +218,7 @@ def __init__(
if hasattr(self.remote_field.model._meta, "_related_objects_cache"):
del self.remote_field.model._meta._related_objects_cache

# ForeignKey support - rewrite related_name
elif not NEW_RELATED_API and self.rel and self.related and not self.rel.is_hidden():
current = self.related.get_accessor_name()
self.rel = copy.copy(self.rel) # Since fields cannot share the same rel object.
# self.related doesn't need to be copied, as it will be recreated in
# ``RelatedField.do_related_class``

if self.rel.related_name is None:
# For implicit related_name use different query field name
loc_related_query_name = build_localized_fieldname(
self.related_query_name(), self.language
)
self.related_query_name = lambda: loc_related_query_name
self.rel.related_name = build_localized_fieldname(current, self.language)
self.rel.field = self
if hasattr(self.rel.to._meta, "_related_objects_cache"):
del self.rel.to._meta._related_objects_cache
elif NEW_RELATED_API and self.remote_field and not self.remote_field.is_hidden():
elif self.remote_field and not self.remote_field.is_hidden():
current = self.remote_field.get_accessor_name()
# Since fields cannot share the same rel object:
self.remote_field = copy.copy(self.remote_field)
Expand Down
21 changes: 3 additions & 18 deletions modeltranslation/management/commands/sync_translation_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from typing import Any, cast
from collections.abc import Iterator

from django import VERSION
from django.core.management.base import BaseCommand, CommandParser
from django.core.management.color import no_style
from django.db import connection
Expand Down Expand Up @@ -59,30 +58,16 @@ class Command(BaseCommand):
" languages or undeclared fields."
)

if VERSION < (1, 8):
from optparse import make_option

option_list = BaseCommand.option_list + ( # type: ignore
make_option(
def add_arguments(self, parser: CommandParser) -> None:
(
parser.add_argument(
"--noinput",
action="store_false",
dest="interactive",
default=True,
help="Do NOT prompt the user for input of any kind.",
),
)
else:

def add_arguments(self, parser: CommandParser) -> None:
(
parser.add_argument(
"--noinput",
action="store_false",
dest="interactive",
default=True,
help="Do NOT prompt the user for input of any kind.",
),
)

def handle(self, *args: Any, **options: Any) -> None:
"""
Expand Down
15 changes: 3 additions & 12 deletions modeltranslation/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from typing import Any, Literal, TypeVar, cast, overload
from collections.abc import Container, Iterator, Sequence, Iterable

from django import VERSION
from django.contrib.admin.utils import get_model_from_relation
from django.core.exceptions import FieldDoesNotExist
from django.db import models
Expand Down Expand Up @@ -328,17 +327,9 @@ def _rewrite_filter_or_exclude(self, args: Any, kwargs: Any) -> tuple[Any, Any]:
kwargs[new_key] = self._rewrite_f(val)
return args, kwargs

if VERSION >= (3, 2):

def _filter_or_exclude(self, negate: bool, args: Any, kwargs: Any) -> Self:
args, kwargs = self._rewrite_filter_or_exclude(args, kwargs)
return super()._filter_or_exclude(negate, args, kwargs)

else:

def _filter_or_exclude(self, negate: bool, *args: Any, **kwargs: Any) -> Self:
args, kwargs = self._rewrite_filter_or_exclude(args, kwargs)
return super()._filter_or_exclude(negate, *args, **kwargs)
def _filter_or_exclude(self, negate: bool, args: Any, kwargs: Any) -> Self:
args, kwargs = self._rewrite_filter_or_exclude(args, kwargs)
return super()._filter_or_exclude(negate, args, kwargs)

def _get_original_fields(self) -> list[str]:
source = (
Expand Down
10 changes: 4 additions & 6 deletions modeltranslation/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from typing import Any, Callable, ClassVar, cast
from collections.abc import Collection, Iterable, Sequence

import django
from django.core.exceptions import ImproperlyConfigured
from django.db.models import (
Field,
Expand Down Expand Up @@ -460,11 +459,10 @@ def patch_related_object_descriptor_caching(ro_descriptor):
class NewSingleObjectDescriptor(LanguageCacheSingleObjectDescriptor, ro_descriptor.__class__):
pass

if django.VERSION[0] >= 2:
ro_descriptor.related.get_cache_name = partial(
NewSingleObjectDescriptor.get_cache_name,
ro_descriptor,
)
ro_descriptor.related.get_cache_name = partial(
NewSingleObjectDescriptor.get_cache_name,
ro_descriptor,
)

ro_descriptor.accessor = ro_descriptor.related.get_accessor_name()
ro_descriptor.__class__ = NewSingleObjectDescriptor
Expand Down

0 comments on commit d0b8e70

Please sign in to comment.