Associated expanded editions with correct work

This commit is contained in:
Mouse Reeve 2021-04-28 13:28:07 -07:00
parent 7b65291a59
commit 3faacffaca
2 changed files with 16 additions and 13 deletions

View file

@ -73,7 +73,7 @@ class AbstractMinimalConnector(ABC):
return get_data(remote_id, **kwargs) return get_data(remote_id, **kwargs)
@abstractmethod @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""" """pull up a book record by whatever means possible"""
@abstractmethod @abstractmethod
@ -109,7 +109,7 @@ class AbstractConnector(AbstractMinimalConnector):
return False return False
return True 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""" """translate arbitrary json into an Activitypub dataclass"""
# first, check if we have the origin_id saved # first, check if we have the origin_id saved
existing = models.Edition.find_existing_by_remote_id( existing = models.Edition.find_existing_by_remote_id(
@ -123,6 +123,7 @@ class AbstractConnector(AbstractMinimalConnector):
# load the json # load the json
data = self.get_book_data(remote_id) data = self.get_book_data(remote_id)
mapped_data = dict_from_mappings(data, self.book_mappings) mapped_data = dict_from_mappings(data, self.book_mappings)
work_data = edition_data = None
if self.is_work_data(data): if self.is_work_data(data):
try: try:
edition_data = self.get_edition_from_work_data(data) edition_data = self.get_edition_from_work_data(data)
@ -132,21 +133,23 @@ class AbstractConnector(AbstractMinimalConnector):
edition_data = data edition_data = data
work_data = mapped_data work_data = mapped_data
else: 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 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) raise ConnectorException("Unable to load book data: %s" % remote_id)
with transaction.atomic(): with transaction.atomic():
# create activitypub object if not work:
work_activity = activitypub.Work(**work_data) # create activitypub object
# this will dedupe automatically work_activity = activitypub.Work(**work_data)
work = work_activity.to_model(model=models.Work) # this will dedupe automatically
work = work_activity.to_model(model=models.Work)
for author in self.get_authors_from_data(data): for author in self.get_authors_from_data(data):
work.authors.add(author) work.authors.add(author)

View file

@ -137,7 +137,7 @@ class Connector(AbstractConnector):
for edition_uri in edition_options.get("uris"): for edition_uri in edition_options.get("uris"):
remote_id = self.get_remote_id(edition_uri) 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, *_): def get_cover_url(self, cover_blob, *_):
"""format the relative cover url into an absolute one: """format the relative cover url into an absolute one: