Skip to content

Commit

Permalink
Merge pull request #295 from NYPL/HOTFIX_FixFormatFilter500Error
Browse files Browse the repository at this point in the history
HOTFIX_FixFormatFilter500Error
  • Loading branch information
mitri-slory authored Mar 5, 2024
2 parents 6d76454 + 6f2aab2 commit 4526b29
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
- Fixed 500 error with search endpoint for filtering by format by adjusting item sorting

## 2023-09-05 version -- v0.12.3
## Removed
Expand Down
23 changes: 18 additions & 5 deletions api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,9 +365,22 @@ def formatEdition(

editionDict['items'].append(itemDict)

editionDict['items']\
.sort(key=lambda x: (cls.SOURCE_PRIORITY[x['source']], cls.sortByMediaType(x['links'][0])))

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']:
Expand Down Expand Up @@ -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 = {
Expand Down
29 changes: 28 additions & 1 deletion tests/unit/test_api_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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):
Expand Down Expand Up @@ -514,6 +524,7 @@ def test_formatEdition_v2_reader_flag(self, testEdition, testWebpubItem, testPDF
testEdition.items.append(testPDFItem)
testEdition.items.append(testWebpubItem)


formattedEdition = APIUtils.formatEdition(testEdition, reader='v2')

assert len(formattedEdition['items']) == 3
Expand All @@ -524,7 +535,23 @@ def test_formatEdition_v2_reader_flag(self, testEdition, testWebpubItem, testPDF
'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'},
Expand Down

0 comments on commit 4526b29

Please sign in to comment.