1
0
mirror of https://github.com/django/django.git synced 2025-05-07 15:36:29 +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.forms.utils import to_current_timezone
from django.templatetags.static import static from django.templatetags.static import static
from django.utils import formats from django.utils import formats
from django.utils.datastructures import OrderedSet
from django.utils.dates import MONTHS from django.utils.dates import MONTHS
from django.utils.formats import get_format from django.utils.formats import get_format
from django.utils.html import format_html, html_safe 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. global or in CSS you might want to override a style.
""" """
ts = TopologicalSorter() ts = TopologicalSorter()
all_items = OrderedSet() for head, *tail in filter(None, lists):
for list_ in filter(None, lists): ts.add(head) # Ensure that the first items are included.
head = list_[0] for item in tail:
# The first items depend on nothing but have to be part of the if head != item: # Avoid circular dependency to self.
# 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:
ts.add(item, head) ts.add(item, head)
head = item head = item
try: try:
@ -173,7 +166,7 @@ class Media:
), ),
MediaOrderConflictWarning, MediaOrderConflictWarning,
) )
return list(all_items) return list(dict.fromkeys(chain.from_iterable(filter(None, lists))))
def __add__(self, other): def __add__(self, other):
combined = Media() combined = Media()

View File

@ -606,7 +606,7 @@ class FormsMediaTestCase(SimpleTestCase):
def test_merge_warning(self): def test_merge_warning(self):
msg = "Detected duplicate Media files in an opposite order: [1, 2], [2, 1]" msg = "Detected duplicate Media files in an opposite order: [1, 2], [2, 1]"
with self.assertWarnsMessage(RuntimeWarning, msg): 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): def test_merge_js_three_way(self):
""" """