def add_all_isbns(isbns, work, language=None, title=None): first_edition = None for isbn in isbns: edition = bookloader.add_by_isbn(isbn, work, language=language, title=title) if edition: first_edition = first_edition if first_edition else edition if work and (edition.work_id != work.id): if work.doab and edition.work.doab and work.doab != edition.work.doab: if work.created < edition.work.created: work = merge_works(work, edition.work) else: work = merge_works(edition.work, work) else: work = edition.work return work, first_edition
def load_from_books(books): ''' books is an iterator of book dicts. each book must have attributes (umich dialect) eISBN, ClothISBN, PaperISBN, Publisher, FullTitle, Title, Subtitle, AuthorsList, Author1Last, Author1First, Author1Role, Author2Last, Author2First, Author2Role, Author3Last, Author3First, Author3Role, AuthorBio, TableOfContents, Excerpt, DescriptionLong, DescriptionBrief, BISACCode1, BISACCode2, BISACCode3, CopyrightYear, ePublicationDate, eListPrice, ListPriceCurrencyType, List Price in USD (paper ISBN), eTerritoryRights, SubjectListMARC, , Book-level DOI, URL, License ''' # Goal: get or create an Edition and Work for each given book results = [] for (i, book) in enumerate(books): # try first to get an Edition already in DB with by one of the ISBNs in book (isbns, edition) = get_isbns(book) if len(isbns) == 0: continue title = get_title(book) authors = get_authors(book) # if matching by ISBN doesn't work, then create a Work and Edition # with a title and the first ISBN if not edition: work = Work(title=title) work.save() edition = Edition(title=title, work=work) edition.save() Identifier.set(type='isbn', value=isbns[0], edition=edition, work=work) work = edition.work # at this point, work and edition exist url = get_url(book) if url: Identifier.set(type='http', value=url, edition=edition, work=work) # make sure each isbn is represented by an Edition # also associate authors, publication date, cover, publisher for isbn in isbns: edition = add_by_isbn_from_google(isbn, work=work) if edition and edition.work != work: work = merge_works(work, edition.work) if not edition: edition = Edition(title=title, work=work) edition.save() Identifier.set(type='isbn', value=isbn, edition=edition, work=work) edition.authors.clear() for (author, role) in authors: edition.add_author(author, inv_relator_contrib.get(role, 'aut')) edition.publication_date = get_pubdate(book) edition.cover_image = get_cover(book) edition.save() edition.set_publisher(get_publisher(book)) # possibly replace work.description description = get_description(book) if len(description) > len(work.description): work.description = description work.save() # set language lang = get_language(book) if lang: work.language = lang work.save() # add a bisac subject (and ancestors) to work for bisacsh in get_subjects(book): while bisacsh: add_subject(bisacsh.full_label, work, authority="bisacsh") bisacsh = bisacsh.parent logging.info(u'loaded work {}'.format(work.title)) loading_ok = loaded_book_ok(book, work, edition) results.append((book, work, edition)) try: logger.info(u"{} {} {}\n".format(i, title, loading_ok)) except Exception as e: logger.info(u"{} {}\n".format(i, title, str(e))) return results