Skip to content

Improve CSV Logging and Fix Image Import Handling - #197

Open
hmshuv wants to merge 5 commits into
wagtail:mainfrom
hmshuv:fix/logging-and-image-imports
Open

Improve CSV Logging and Fix Image Import Handling#197
hmshuv wants to merge 5 commits into
wagtail:mainfrom
hmshuv:fix/logging-and-image-imports

Conversation

@hmshuv

@hmshuv hmshuv commented Dec 22, 2025

Copy link
Copy Markdown

Improve CSV Logging and Fix Image Import Handling

Closes #193
Closes #142
Closes #145
Closes #171

Description

This PR addresses the "Make the logging to CSV better" issue and resolves several sub-issues related to failures and missing logs during image import.

Key Changes:

  1. Refactored CSV Logging (logger.py):

    • Consolidated CSV writing logic into a new _write_csv helper method to reduce code duplication.
    • Fix: Corrected save_csv_pagelink_errors_report which was previously iterating over self.images (causing incorrect data or crashes) instead of self.page_link_errors. It now correctly extracts and logs page link errors.
  2. Robust Image Import Handling:

    • Fix Image can be None in construct_block function #142 (Crash on None Image): Updated CaptionHandler to gracefully handle cases where get_or_save_image fails (returns None) instead of crashing with an AttributeError. A fallback HTML comment is now generated in the stream block.
    • Fix get_or_save_image can fail if the downloaded image's is corrupted #145 (Corrupted Image): Wrapped the image save operation in get_or_save_image with a try/except block to catch IntegrityError (e.g., width=0) or other exceptions caused by corrupted files.
    • Fix Images Won't Import #171 (Missing Logs): Refactored BlockBuilder and default builder functions to accept and pass the Logger instance down the call stack. This ensures that deep failures (like in get_or_save_image or shortcode handlers) are now properly recorded in the CSV logs instead of being printed to stdout or lost.

Testing & Verification

I have verified these changes using custom reproduction scripts to simulate the failure scenarios:

  • CSV Output: Verified pagelink_errors-report.csv now contains the correct data for broken links.
  • Zero-width / Corrupted Images: Verified that the importer no longer crashes and instead logs the error to images-report.csv.
  • Missing Images in Captions: Verified that the import continues and a raw HTML placeholder is inserted.

Checklist

@hmshuv

hmshuv commented Dec 22, 2025

Copy link
Copy Markdown
Author

I've resolved the pre-commit CI failures. isort needed a version bump to fix a build error with newer tools, and I fixed a few legacy syntax issues (backticks) caught by flake8. All checks are passing now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant