From 3faacffacaaa3fdd474f12c1768544393aa22970 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Wed, 28 Apr 2021 13:28:07 -0700 Subject: [PATCH] Associated expanded editions with correct work --- bookwyrm/connectors/abstract_connector.py | 27 +++++++++++++---------- bookwyrm/connectors/inventaire.py | 2 +- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/bookwyrm/connectors/abstract_connector.py b/bookwyrm/connectors/abstract_connector.py index fd2b2707..48c05182 100644 --- a/bookwyrm/connectors/abstract_connector.py +++ b/bookwyrm/connectors/abstract_connector.py @@ -73,7 +73,7 @@ class AbstractMinimalConnector(ABC): return get_data(remote_id, **kwargs) @abstractmethod - def get_or_create_book(self, remote_id): + def get_or_create_book(self, remote_id, work=None): """pull up a book record by whatever means possible""" @abstractmethod @@ -109,7 +109,7 @@ class AbstractConnector(AbstractMinimalConnector): return False return True - def get_or_create_book(self, remote_id): + def get_or_create_book(self, remote_id, work=None): """translate arbitrary json into an Activitypub dataclass""" # first, check if we have the origin_id saved existing = models.Edition.find_existing_by_remote_id( @@ -123,6 +123,7 @@ class AbstractConnector(AbstractMinimalConnector): # load the json data = self.get_book_data(remote_id) mapped_data = dict_from_mappings(data, self.book_mappings) + work_data = edition_data = None if self.is_work_data(data): try: edition_data = self.get_edition_from_work_data(data) @@ -132,21 +133,23 @@ class AbstractConnector(AbstractMinimalConnector): edition_data = data work_data = mapped_data else: - try: - work_data = self.get_work_from_edition_data(data) - work_data = dict_from_mappings(work_data, self.book_mappings) - except (KeyError, ConnectorException): - work_data = mapped_data edition_data = data + if not work: + try: + work_data = self.get_work_from_edition_data(data) + work_data = dict_from_mappings(work_data, self.book_mappings) + except (KeyError, ConnectorException): + work_data = mapped_data - if not work_data or not edition_data: + if (not work_data and not work) or not edition_data: raise ConnectorException("Unable to load book data: %s" % remote_id) with transaction.atomic(): - # create activitypub object - work_activity = activitypub.Work(**work_data) - # this will dedupe automatically - work = work_activity.to_model(model=models.Work) + if not work: + # create activitypub object + work_activity = activitypub.Work(**work_data) + # this will dedupe automatically + work = work_activity.to_model(model=models.Work) for author in self.get_authors_from_data(data): work.authors.add(author) diff --git a/bookwyrm/connectors/inventaire.py b/bookwyrm/connectors/inventaire.py index ae6fb862..c91ac937 100644 --- a/bookwyrm/connectors/inventaire.py +++ b/bookwyrm/connectors/inventaire.py @@ -137,7 +137,7 @@ class Connector(AbstractConnector): for edition_uri in edition_options.get("uris"): remote_id = self.get_remote_id(edition_uri) - self.get_or_create_book(remote_id) + self.get_or_create_book(remote_id, work=work) def get_cover_url(self, cover_blob, *_): """format the relative cover url into an absolute one: