1
0
mirror of https://github.com/django/django.git synced 2025-03-11 01:42:35 +00:00

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.
This commit is contained in:
Nick Pope 2021-03-08 13:27:19 +00:00 committed by Mariusz Felisiak
parent 1282b5e420
commit 1e62a64202
2 changed files with 6 additions and 13 deletions

View File

@ -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()

View File

@ -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):
"""