From 20853a23763ee2db8d57dd842eed128717d0ecd8 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 28 Feb 2024 13:46:14 -0500 Subject: [PATCH 1/3] HOTFIX_FixFormatFilter500Error --- CHANGELOG.md | 1 + api/utils.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3c904bd845..15ab359f84 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ - Bardo CCE API and Hathi DataFiles URL updated - Deleted Tugboat configuration as Tugboat is no longer used and no longer builds - Adjusted sorting for item source and media type priority +- Removed item media type priority to fix 500 error with search endpoint for filtering by format ## 2023-09-05 version -- v0.12.3 ## Removed diff --git a/api/utils.py b/api/utils.py index 51411c12b5..60f7afada1 100644 --- a/api/utils.py +++ b/api/utils.py @@ -366,7 +366,7 @@ def formatEdition( editionDict['items'].append(itemDict) editionDict['items']\ - .sort(key=lambda x: (cls.SOURCE_PRIORITY[x['source']], cls.sortByMediaType(x['links'][0]))) + .sort(key=lambda x: (cls.SOURCE_PRIORITY[x['source']])) if records is not None: itemsByLink = {} From 147330c9bb865ab09be01e9d141e8db5e8d6d119 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 28 Feb 2024 13:55:24 -0500 Subject: [PATCH 2/3] Removed test --- CHANGELOG.md | 2 +- tests/unit/test_api_utils.py | 15 --------------- 2 files changed, 1 insertion(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 15ab359f84..af3dc1eb2e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,7 +16,7 @@ - Bardo CCE API and Hathi DataFiles URL updated - Deleted Tugboat configuration as Tugboat is no longer used and no longer builds - Adjusted sorting for item source and media type priority -- Removed item media type priority to fix 500 error with search endpoint for filtering by format +- Removed item media type sorting to fix 500 error with search endpoint for filtering by format ## 2023-09-05 version -- v0.12.3 ## Removed diff --git a/tests/unit/test_api_utils.py b/tests/unit/test_api_utils.py index 75c3b6f70a..21accc2948 100644 --- a/tests/unit/test_api_utils.py +++ b/tests/unit/test_api_utils.py @@ -510,21 +510,6 @@ def test_formatEdition_v1_reader_flag(self, testEdition, testWebpubItem): assert formattedEdition['items'][1]['links'][0]['mediaType'] ==\ 'application/epub+xml' - def test_formatEdition_v2_reader_flag(self, testEdition, testWebpubItem, testPDFItem): - testEdition.items.append(testPDFItem) - testEdition.items.append(testWebpubItem) - - formattedEdition = APIUtils.formatEdition(testEdition, reader='v2') - - assert len(formattedEdition['items']) == 3 - assert formattedEdition['items'][0]['item_id'] == 'it2' - assert formattedEdition['items'][0]['links'][0]['mediaType'] ==\ - 'application/webpub+json' - assert formattedEdition['items'][1]['links'][0]['mediaType'] ==\ - 'application/pdf' - assert formattedEdition['items'][2]['links'][0]['flags']['reader'] is\ - False - def test_formatRecord(self, testRecord, mocker): testLinkItems = { 'url1': {'item_id': 1, 'url': 'url1'}, From 6f2aab2aa22e3ee333978e11aa2b4715237a2d7b Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 1 Mar 2024 16:48:40 -0500 Subject: [PATCH 3/3] Modified sorting and added tests --- CHANGELOG.md | 2 +- api/utils.py | 23 +++++++++++++++---- tests/unit/test_api_utils.py | 44 +++++++++++++++++++++++++++++++++++- 3 files changed, 62 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index af3dc1eb2e..cf55d7012f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,7 +16,7 @@ - Bardo CCE API and Hathi DataFiles URL updated - Deleted Tugboat configuration as Tugboat is no longer used and no longer builds - Adjusted sorting for item source and media type priority -- Removed item media type sorting to fix 500 error with search endpoint for filtering by format +- Fixed 500 error with search endpoint for filtering by format by adjusting item sorting ## 2023-09-05 version -- v0.12.3 ## Removed diff --git a/api/utils.py b/api/utils.py index 60f7afada1..c4acb53bdf 100644 --- a/api/utils.py +++ b/api/utils.py @@ -365,9 +365,22 @@ def formatEdition( editionDict['items'].append(itemDict) - editionDict['items']\ - .sort(key=lambda x: (cls.SOURCE_PRIORITY[x['source']])) - + emptyListFlag = False + + #This for loop is meant to check if a empty links array exists in a item dictionary + #If one exists, then sorting by source priority and empty/non-empty priority occurs + #Otherwise, sorting by source and link media type occurs + for itemDict in editionDict['items']: + if itemDict['links'] == []: + editionDict['items']\ + .sort(key=lambda x: (cls.SOURCE_PRIORITY[x['source']], x['links'] == [])) + emptyListFlag = True + break + + if emptyListFlag == False: + editionDict['items']\ + .sort(key=lambda x: (cls.SOURCE_PRIORITY[x['source']], cls.sortByMediaType(x['links'][0]))) + if records is not None: itemsByLink = {} for item in editionDict['items']: @@ -400,8 +413,8 @@ def sortByMediaType(link): 'application/html+catalog': 6 } - return scores.get(link['mediaType'], 7) - + return scores.get(link['mediaType'], 7) + @classmethod def formatRecord(cls, record, itemsByLink): outRecord = { diff --git a/tests/unit/test_api_utils.py b/tests/unit/test_api_utils.py index 21accc2948..bb4253e2ac 100644 --- a/tests/unit/test_api_utils.py +++ b/tests/unit/test_api_utils.py @@ -129,7 +129,7 @@ def testItem(self, MockDBObject, testLink, testRights): physical_location={'name': 'test'}, source='hathitrust' ) - + @pytest.fixture def testWebpubItem(self, MockDBObject, testWebpubLink): return MockDBObject( @@ -149,6 +149,16 @@ def testPDFItem(self, MockDBObject, testPDFLink): physical_location={}, source='gutenberg' ) + + @pytest.fixture + def testEmptyItem(self, MockDBObject): + return MockDBObject( + id='it4', + links=[], + rights=[], + physical_location={}, + source='gutenberg' + ) @pytest.fixture def testEdition(self, MockDBObject, testItem, mocker): @@ -510,6 +520,38 @@ def test_formatEdition_v1_reader_flag(self, testEdition, testWebpubItem): assert formattedEdition['items'][1]['links'][0]['mediaType'] ==\ 'application/epub+xml' + def test_formatEdition_v2_reader_flag(self, testEdition, testWebpubItem, testPDFItem): + testEdition.items.append(testPDFItem) + testEdition.items.append(testWebpubItem) + + + formattedEdition = APIUtils.formatEdition(testEdition, reader='v2') + + assert len(formattedEdition['items']) == 3 + assert formattedEdition['items'][0]['item_id'] == 'it2' + assert formattedEdition['items'][0]['links'][0]['mediaType'] ==\ + 'application/webpub+json' + assert formattedEdition['items'][1]['links'][0]['mediaType'] ==\ + 'application/pdf' + assert formattedEdition['items'][2]['links'][0]['flags']['reader'] is\ + False + + def test_formatEdition_v2_reader_flag_2(self, testEdition, testWebpubItem, testPDFItem, testEmptyItem): + testEdition.items.append(testEmptyItem) + testEdition.items.append(testWebpubItem) + + + formattedEdition = APIUtils.formatEdition(testEdition, reader='v2') + + assert len(formattedEdition['items']) == 3 + assert formattedEdition['items'][0]['item_id'] == 'it2' + assert formattedEdition['items'][0]['links'][0]['mediaType'] ==\ + 'application/webpub+json' + assert formattedEdition['items'][1]['links'] ==\ + [] + assert formattedEdition['items'][2]['links'][0]['flags']['reader'] is\ + False + def test_formatRecord(self, testRecord, mocker): testLinkItems = { 'url1': {'item_id': 1, 'url': 'url1'},