Skip to content

Commit 41c7830

Browse files
authored
Merge pull request #12641 from jimchamp/update-unlink-record-query
Update edition matching query in unlink handler
1 parent a15b632 commit 41c7830

1 file changed

Lines changed: 11 additions & 16 deletions

File tree

  • openlibrary/plugins/openlibrary

openlibrary/plugins/openlibrary/api.py

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -871,28 +871,22 @@ def POST(self):
871871
ocaid, _ts = parts
872872

873873
# Fetch affected editions
874-
if not (
875-
edition_keys := web.ctx.site.things(
876-
{"type": "/type/edition", "ocaid": ocaid}
874+
edition_keys = web.ctx.site.things({"type": "/type/edition", "ocaid": ocaid})
875+
edition_keys.extend(
876+
web.ctx.site.things(
877+
{"type": "/type/edition", "source_records": f"ia:{ocaid}"}
877878
)
878-
):
879+
)
880+
edition_keys = list(set(edition_keys))
881+
if not edition_keys:
879882
raise web.HTTPError("404 Not Found", {"Content-Type": "application/json"})
880883

881884
editions = [web.ctx.site.get(key) for key in edition_keys]
882-
if len(editions) > 1:
883-
raise web.HTTPError(
884-
"409 Conflict",
885-
{"Content-Type": "application/json"},
886-
data=json.dumps(
887-
{"error": "Multiple editions associated with given ocaid"}
888-
),
889-
)
890-
891-
edition = editions[0]
892885

893886
# Update records
894887
try:
895-
self.make_dark(edition, ocaid)
888+
for edition in editions:
889+
self.make_dark(edition, ocaid)
896890
except ClientException as e:
897891
logger.error(
898892
f"Failed to disassociate record with key {edition.key}", exc_info=True
@@ -908,7 +902,8 @@ def POST(self):
908902
@staticmethod
909903
def make_dark(edition, ocaid):
910904
data = edition.dict()
911-
del data["ocaid"]
905+
if "ocaid" in data and data["ocaid"] == ocaid:
906+
del data["ocaid"]
912907
source_records = data.get("source_records", [])
913908
data["source_records"] = [rec for rec in source_records if rec != f"ia:{ocaid}"]
914909
if not data["source_records"]:

0 commit comments

Comments
 (0)