Skip to content

Commit e5184a0

Browse files
feat!: update finder method to work with django 42 and 52 (#37131)
1 parent 4cba98c commit e5184a0

3 files changed

Lines changed: 39 additions & 8 deletions

File tree

openedx/core/djangoapps/theming/finders.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,21 @@ def list(self, ignore_patterns):
6565
for path in utils.get_files(storage, ignore_patterns):
6666
yield path, storage
6767

68-
def find(self, path, all=False): # pylint: disable=redefined-builtin
68+
def find(self, path, *args, **kwargs): # pylint: disable=redefined-builtin
6969
"""
7070
Looks for files in the theme directories.
7171
"""
72+
if 'all' in kwargs:
73+
# Note this method signature where we accept all and find_all is being used so that we can be
74+
# compatible with both Django 4.2 and Django 5.2 at the same time. After we have fully
75+
# dropped Django 4.2 support, the method signature can be updated to just consume the
76+
# `find_all` paramater.
77+
find_all = kwargs.get('all', False)
78+
elif 'find_all' in kwargs:
79+
find_all = kwargs.get('find_all', False)
80+
else:
81+
find_all = args[0] if args else False
82+
7283
matches = []
7384
theme_dir_name = path.split("/", 1)[0]
7485

@@ -79,7 +90,7 @@ def find(self, path, all=False): # pylint: disable=redefined-builtin
7990
path = "/".join(path.split("/")[1:])
8091
match = self.find_in_theme(theme.theme_dir_name, path)
8192
if match:
82-
if not all:
93+
if not find_all:
8394
return match
8495
matches.append(match)
8596
return matches

openedx/core/djangoapps/theming/tests/test_finders.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,24 @@ def test_find_all_themed_asset(self):
3535
Verify Theme Finder returns themed assets
3636
"""
3737
themes_dir = settings.COMPREHENSIVE_THEME_DIRS[1]
38+
expected_path = (((((themes_dir / 'test-theme') / 'lms') / 'static') / 'images') / 'logo.png')
3839

3940
asset = "test-theme/images/logo.png"
40-
matches = self.finder.find(asset, all=True)
41+
matches_test_1 = self.finder.find(asset, True)
42+
matches_test_2 = self.finder.find(asset, all=True)
43+
matches_test_3 = self.finder.find(asset, find_all=True)
4144

42-
# Make sure only first match was returned
43-
assert 1 == len(matches)
45+
# 1 Make sure only first match was returned
46+
assert 1 == len(matches_test_1)
47+
assert matches_test_1[0] == expected_path
4448

45-
assert matches[0] == (((((themes_dir / 'test-theme') / 'lms') / 'static') / 'images') / 'logo.png')
49+
# 2 Make sure only first match was returned
50+
assert 1 == len(matches_test_2)
51+
assert matches_test_2[0] == expected_path
52+
53+
# 3 Make sure only first match was returned
54+
assert 1 == len(matches_test_3)
55+
assert matches_test_3[0] == expected_path
4656

4757
def test_find_in_theme(self):
4858
"""

openedx/core/lib/xblock_pipeline/finder.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,15 +144,25 @@ def list(self, ignore_patterns):
144144
for path in utils.get_files(storage, ignore_patterns):
145145
yield path, storage
146146

147-
def find(self, path, all=False): # pylint: disable=redefined-builtin
147+
def find(self, path, *args, **kwargs): # pylint: disable=redefined-builtin
148148
"""
149149
Looks for files in the xblock package directories.
150150
"""
151+
if 'all' in kwargs:
152+
# Note this method signature where we accept all and find_all is being used so that we can be
153+
# compatible with both Django 4.2 and Django 5.2 at the same time. After we have fully
154+
# dropped Django 4.2 support, the method signature can be updated to just consume the
155+
# `find_all` paramater.
156+
find_all = kwargs.get('all', False)
157+
elif 'find_all' in kwargs:
158+
find_all = kwargs.get('find_all', False)
159+
else:
160+
find_all = args[0] if args else False
151161
matches = []
152162
for storage in self.package_storages:
153163
if storage.exists(path):
154164
match = storage.path(path)
155-
if not all:
165+
if not find_all:
156166
return match
157167
matches.append(match)
158168
return matches

0 commit comments

Comments
 (0)