Repository: internetarchive/openlibrary

Issue:
Reading log export fails if Work ID in `bookshelves_books` table is a redirect
<!-- What problem are we solving? What does the experience look like today? What are the symptoms? -->
If a patron has added a work to their reading log, and the work later becomes a `/type/redirect`, the patron will not be able to download their reading log `csv` file from the export page (https://openlibrary.org/account/import).

### Evidence / Screenshot (if possible)

### Relevant url?
<!-- `https://openlibrary.org/...` -->
https://openlibrary.org/account/import

### Steps to Reproduce
<!-- What steps caused you to find the bug? -->
1. Ensure that the `work_id` for at least one `bookshelves_books` entry is of type `redirect`.  You may have to update the table directly in order to achieve this precondition.
2. Navigate to https://openlibrary.org/account/import and click the download button beneath the "Export your Reading Log" heading.

<!-- What actually happened after these steps? What did you expect to happen? -->
* Actual: Error page is displayed
* Expected: A `csv` file is generated and downloaded

### Details

- **Logged in (Y/N)?** Y
- **Browser type/version?** Any
- **Operating system?** Any
- **Environment (prod/dev/local)?** prod
<!-- If not sure, put prod -->

### Proposal & Constraints
<!-- What is the proposed solution / implementation? Is there a precedent of this approach succeeding elsewhere? -->
One of the following would fix this:
1. Ensure that `bookshelves_events` and similar tables' `work_id`s are updated when a work becomes a redirect.
2. Catch such errors during `csv` generation.  Ensure that the affected rows appear in the export file.
3. The reading log export includes more data than just the `bookshelves_books` entries.  Reduce the output of this export to only include `bookshelves_books` data, and adjust the flow accordingly.

### Related files
<!-- Files related to this issue; this is super useful for new contributors who might want to help! If you're not sure, leave this blank; a maintainer will add them. -->
The `csv` formatter for these export is here:
https://github.com/internetarchive/openlibrary/blob/master/openlibrary/plugins/upstream/account.py#L895-L924

### Stakeholders
<!-- @ tag stakeholders of this bug -->



Discussion:

Resolving patch:
diff --git a/openlibrary/plugins/upstream/account.py b/openlibrary/plugins/upstream/account.py
--- a/openlibrary/plugins/upstream/account.py
+++ b/openlibrary/plugins/upstream/account.py
@@ -901,6 +901,10 @@ def format_reading_log(book: dict) -> dict:
             work: Work = web.ctx.site.get(work_key)
             if not work:
                 raise ValueError(f"No Work found for {work_key}.")
+            if work.type.key == '/type/redirect':
+                # Fetch actual work and resolve redirects before exporting:
+                work = web.ctx.site.get(work.location)
+                work.resolve_redirect_chain(work_key)
             if edition_id := book.get("edition_id") or "":
                 edition_id = f"OL{edition_id}M"
             ratings = work.get_rating_stats() or {"average": "", "count": ""}


Test patch: