Repository: internetarchive/openlibrary

Issue:
Make all Work editions use/show/share same check-in (Book Page)



Discussion:

Resolving patch:
diff --git a/openlibrary/core/bookshelves_events.py b/openlibrary/core/bookshelves_events.py
--- a/openlibrary/core/bookshelves_events.py
+++ b/openlibrary/core/bookshelves_events.py
@@ -16,6 +16,7 @@ def has_value(cls, value: int) -> bool:
 class BookshelvesEvents(db.CommonExtras):
 
     TABLENAME = 'bookshelves_events'
+    NULL_EDITION_ID = -1
 
     # Create methods:
     @classmethod
@@ -33,7 +34,7 @@ def create_event(
             cls.TABLENAME,
             username=username,
             work_id=work_id,
-            edition_id=edition_id,
+            edition_id=edition_id or cls.NULL_EDITION_ID,
             event_type=event_type,
             event_date=event_date,
         )
@@ -46,20 +47,19 @@ def select_by_id(cls, pid):
         return list(oldb.select(cls.TABLENAME, where='id=$id', vars={'id': pid}))
 
     @classmethod
-    def get_latest_event_date(cls, username, work_id, edition_id, event_type):
+    def get_latest_event_date(cls, username, work_id, event_type):
         oldb = db.get_db()
 
         data = {
             'username': username,
             'work_id': work_id,
-            'edition_id': edition_id,
             'event_type': event_type,
         }
 
         query = (
             f'SELECT id, event_date FROM {cls.TABLENAME}'
             ' WHERE username=$username AND work_id=$work_id'
-            ' AND edition_id=$edition_id AND event_type=$event_type'
+            ' AND event_type=$event_type'
             ' ORDER BY event_date DESC LIMIT 1'
         )
 
@@ -131,13 +131,15 @@ def exists(cls, pid) -> bool:
 
     # Update methods:
     @classmethod
-    def update_event(cls, pid, event_date=None, data=None):
+    def update_event(cls, pid, edition_id=None, event_date=None, data=None):
         oldb = db.get_db()
         updates = {}
         if event_date:
             updates['event_date'] = event_date
         if data:
             updates['data'] = data
+        if edition_id:
+            updates['edition_id'] = edition_id
         if updates:
             return oldb.update(
                 cls.TABLENAME,
diff --git a/openlibrary/plugins/upstream/checkins.py b/openlibrary/plugins/upstream/checkins.py
--- a/openlibrary/plugins/upstream/checkins.py
+++ b/openlibrary/plugins/upstream/checkins.py
@@ -50,15 +50,14 @@ def is_valid_date(year: int, month: Optional[int], day: Optional[int]) -> bool:
 
 
 @public
-def get_latest_read_date(work_olid: str, edition_olid: str | None) -> str | None:
+def get_latest_read_date(work_olid: str) -> dict | None:
     user = get_current_user()
     username = user['key'].split('/')[-1]
 
     work_id = extract_numeric_id_from_olid(work_olid)
-    edition_id = extract_numeric_id_from_olid(edition_olid) if edition_olid else None
 
     result = BookshelvesEvents.get_latest_event_date(
-        username, work_id, edition_id, BookshelfEvent.FINISH
+        username, work_id, BookshelfEvent.FINISH
     )
     return result
 
@@ -104,7 +103,9 @@ def POST(self, work_id):
             # update existing event
             if not BookshelvesEvents.exists(event_id):
                 raise web.notfound('Check-in event unavailable for edit')
-            BookshelvesEvents.update_event_date(event_id, date_str)
+            BookshelvesEvents.update_event(
+                event_id, event_date=date_str, edition_id=edition_id
+            )
         else:
             # create new event
             result = BookshelvesEvents.create_event(


Test patch:
diff --git a/openlibrary/tests/core/test_db.py b/openlibrary/tests/core/test_db.py
--- a/openlibrary/tests/core/test_db.py
+++ b/openlibrary/tests/core/test_db.py
@@ -470,18 +470,15 @@ def test_delete_by_username(self):
 
     def test_get_latest_event_date(self):
         assert (
-            BookshelvesEvents.get_latest_event_date('@eliot_rosewater', 3, 4, 3)[
+            BookshelvesEvents.get_latest_event_date('@eliot_rosewater', 3, 3)[
                 'event_date'
             ]
             == "2019-10"
         )
         assert (
-            BookshelvesEvents.get_latest_event_date('@eliot_rosewater', 3, 4, 3)['id']
-            == 6
-        )
-        assert (
-            BookshelvesEvents.get_latest_event_date('@eliot_rosewater', 3, 4, 1) is None
+            BookshelvesEvents.get_latest_event_date('@eliot_rosewater', 3, 3)['id'] == 6
         )
+        assert BookshelvesEvents.get_latest_event_date('@eliot_rosewater', 3, 1) is None
 
 
 class TestYearlyReadingGoals: