Skip to content

Commit

Permalink
Merge pull request #107 from NatLibFi/ekirjasto-18-show-failed-items-…
Browse files Browse the repository at this point in the history
…during-opds-import

Ekirjasto 18 show failed items during opds import
  • Loading branch information
natlibfi-kaisa authored Oct 31, 2024
2 parents d8ab44f + bcf5372 commit 747f68d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
17 changes: 13 additions & 4 deletions core/opds_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -1958,20 +1958,29 @@ def _get_feeds(self) -> Iterable[tuple[str, bytes]]:
return reversed(feeds)

def run_once(self, progress: TimestampData) -> TimestampData:
"""
Import all books in a feed.
:param progress: A TimestampData object indicating the start time of the run.
:return: A new TimestampData object with an achievements field
containing details of the operation.
"""
feeds = self._get_feeds()
total_imported = 0
total_failures = 0
failure_summary = []

for link, feed in feeds:
self.log.info("Importing next feed: %s", link)
imported_editions, failures = self.import_one_feed(feed)
total_imported += len(imported_editions)
total_failures += len(failures)
for failure_id, failure_details in failures.items():
failure_summary.append(f"ISBN: {failure_id}: {failure_details}")
self._db.commit()

achievements = "Items imported: %d. Failures: %d." % (
total_imported,
total_failures,
achievements = (
"Items imported: %d. Failures: %d.\nFailed IDs and details:\n%s"
% (total_imported, total_failures, "\n".join(failure_summary))
)

return TimestampData(achievements=achievements)
6 changes: 4 additions & 2 deletions tests/core/test_opds_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -1948,7 +1948,7 @@ def test_import_one_feed(self, opds_importer_fixture: OPDSImporterFixture):
collection=collection,
)
assert "Utter failure!" in failure.exception

print(failures)
# Both failures were reported in the return value from
# import_one_feed
assert 2 == len(failures)
Expand Down Expand Up @@ -1992,7 +1992,9 @@ def import_one_feed(self, feed):
assert ["last page", "second page", "first page"] == monitor.imports

# Every page of the import had two successes and one failure.
assert "Items imported: 6. Failures: 3." == progress.achievements
assert (
"Items imported: 6. Failures: 3.\nFailed IDs and details:\nISBN: identifier: Failure\nISBN: identifier: Failure\nISBN: identifier: Failure"
) == progress.achievements

# The TimestampData returned by run_once does not include any
# timing information; that's provided by run().
Expand Down

0 comments on commit 747f68d

Please sign in to comment.