summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Pope <nick@nickpope.me.uk>2021-03-08 13:27:19 +0000
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2023-01-19 06:33:39 +0100
commit1e62a6420230c512b78433265ae8003f27c6eaeb (patch)
treedddc821551c833a46958c03ba2d797f7475edf52
parent1282b5e4207440af659ef0e0e0c486fdfba8e7b7 (diff)
Refs #32528 -- Simplified Media.merge().
This avoids building up a second datastructure for the duplicate files warning case and simply flatten and strip duplicates if that case ever arises.
-rw-r--r--django/forms/widgets.py17
-rw-r--r--tests/forms_tests/tests/test_media.py2
2 files changed, 6 insertions, 13 deletions
diff --git a/django/forms/widgets.py b/django/forms/widgets.py
index a1c67bb863..a53569c5a3 100644
--- a/django/forms/widgets.py
+++ b/django/forms/widgets.py
@@ -12,7 +12,6 @@ from itertools import chain
from django.forms.utils import to_current_timezone
from django.templatetags.static import static
from django.utils import formats
-from django.utils.datastructures import OrderedSet
from django.utils.dates import MONTHS
from django.utils.formats import get_format
from django.utils.html import format_html, html_safe
@@ -152,16 +151,10 @@ class Media:
global or in CSS you might want to override a style.
"""
ts = TopologicalSorter()
- all_items = OrderedSet()
- for list_ in filter(None, lists):
- head = list_[0]
- # The first items depend on nothing but have to be part of the
- # dependency graph to be included in the result.
- ts.add(head)
- for item in list_:
- all_items.add(item)
- # No self dependencies
- if head != item:
+ for head, *tail in filter(None, lists):
+ ts.add(head) # Ensure that the first items are included.
+ for item in tail:
+ if head != item: # Avoid circular dependency to self.
ts.add(item, head)
head = item
try:
@@ -173,7 +166,7 @@ class Media:
),
MediaOrderConflictWarning,
)
- return list(all_items)
+ return list(dict.fromkeys(chain.from_iterable(filter(None, lists))))
def __add__(self, other):
combined = Media()
diff --git a/tests/forms_tests/tests/test_media.py b/tests/forms_tests/tests/test_media.py
index aa508cb415..061adc09ca 100644
--- a/tests/forms_tests/tests/test_media.py
+++ b/tests/forms_tests/tests/test_media.py
@@ -606,7 +606,7 @@ class FormsMediaTestCase(SimpleTestCase):
def test_merge_warning(self):
msg = "Detected duplicate Media files in an opposite order: [1, 2], [2, 1]"
with self.assertWarnsMessage(RuntimeWarning, msg):
- self.assertEqual(Media.merge([1, 2], [2, 1]), [1, 2])
+ self.assertEqual(Media.merge([1, 2], [2, 1], None), [1, 2])
def test_merge_js_three_way(self):
"""