From 03a0d93a8f28f3978baec932a411621ba6cecd6f Mon Sep 17 00:00:00 2001 From: Frankie Dintino Date: Thu, 11 Jul 2024 12:18:37 -0400 Subject: [PATCH] fix: autocomplete in django 5.0 --- nested_admin/compat.py | 15 +++++++++++---- nested_admin/tests/admin_widgets/tests.py | 6 +++++- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/nested_admin/compat.py b/nested_admin/compat.py index 270b898..487863d 100644 --- a/nested_admin/compat.py +++ b/nested_admin/compat.py @@ -4,6 +4,7 @@ See https://github.com/django/django/commit/c19b56f633e172b3c02094cbe12d28865ee57772 and https://code.djangoproject.com/ticket/28377 """ + from collections import defaultdict, OrderedDict import warnings @@ -23,17 +24,23 @@ class MediaOrderConflictWarning(RuntimeWarning): MergeSafeMedia = django.forms.Media try: - from django.utils.topological_sort import ( + from django.utils.topological_sort import ( # noqa: F401 CyclicDependencyError, stable_topological_sort, ) + + has_topological_sort = True except ImportError: + try: + from django.forms.widgets import TopologicalSorter # noqa: F401 + except ImportError: + has_topological_sort = False + else: + has_topological_sort = True class CyclicDependencyError(ValueError): pass - stable_topological_sort = None - class OrderedSet(_OrderedSet): def __sub__(self, other): @@ -46,7 +53,7 @@ def __repr__(self): return "OrderedSet(%r)" % list(self) -if MergeSafeMedia is None or stable_topological_sort is None: +if MergeSafeMedia is None or not has_topological_sort: def linearize_as_needed(l, dependency_graph): # Algorithm: DFS Topological sort diff --git a/nested_admin/tests/admin_widgets/tests.py b/nested_admin/tests/admin_widgets/tests.py index 8f4527f..7be80dd 100644 --- a/nested_admin/tests/admin_widgets/tests.py +++ b/nested_admin/tests/admin_widgets/tests.py @@ -406,7 +406,11 @@ def test_nested_autocomplete_extra(self): self.add_inline([0, 1, [0]]) select_field = self.get_field("fk3", indexes=[0, 1, [0, 0]]) select_parent = select_field.find_element(By.XPATH, "parent::*") - select_parent.click() + self.selenium.execute_script( + "return arguments[0].querySelector('.select2-selection') || arguments[0]", + select_parent, + ).click() + select2_is_active = self.selenium.execute_script( 'return $(".select2-search__field").length > 0' )