Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix create book #411

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 15 additions & 16 deletions olclient/openlibrary.py
Original file line number Diff line number Diff line change
Expand Up @@ -784,21 +784,18 @@ def create_book(self, book, work_olid=None, debug=False):
... isbn=u"3570028364", publish_date=u"1982"))
"""
id_name, id_value = self.get_primary_identifier(book)
author_name = None
for _author in book.authors:
if len(_author.name.split(" ")) > 1:
author_name = _author.name
continue

if not author_name:
raise ValueError("Unable to create_book without valid Author name")

author_olid = self.Author.get_olid_by_name(author_name)
author_key = ('/authors/' + author_olid) if author_olid else '__new__'
if len(book.authors) == 0 :
raise ValueError("Unable to create_book without valid Author name")
author_keys = {}
for _author in book.authors:
author_olid = self.Author.get_olid_by_name(_author.name)
author_keys[_author.name] = ('/authors/' + author_olid) if author_olid else '__new__'
return self._create_book(
title=book.title,
author_name=author_name,
author_key=author_key,
author_names=author_keys.keys(),
author_keys=author_keys,
publish_date=book.publish_date,
publisher=book.publisher,
id_name=id_name,
Expand All @@ -810,8 +807,8 @@ def create_book(self, book, work_olid=None, debug=False):
def _create_book(
self,
title,
author_name,
author_key,
author_names,
author_keys,
publish_date,
publisher,
id_name,
Expand All @@ -833,15 +830,17 @@ def _create_book(
if work_olid:
url += f'?work=/works/{work_olid}'
data = {
"title": title,
"author_name": author_name,
"author_key": author_key,
"book_title": title,
"publish_date": publish_date,
"publisher": publisher,
"id_name": id_name,
"id_value": id_value,
"_save": "",
}
for i,_name in enumerate(author_names):
data[f"author_names--{i}"] = _name
data[f"authors--{i}--author--key"] = author_keys[_name]

if debug:
return data

Expand Down