def test_build_query(add_languages): rec = { 'title': 'magic', 'languages': ['eng', 'fre'], 'authors': [{}], 'description': 'test', } q = build_query(rec) assert q['title'] == 'magic' assert q['description'] == {'type': '/type/text', 'value': 'test'} assert q['type'] == {'key': '/type/edition'} assert q['languages'] == [{'key': '/languages/eng'}, {'key': '/languages/fre'}] pytest.raises(InvalidLanguage, build_query, {'languages': ['wtf']})
def load_data(rec, account_key=None): """ Adds a new Edition to Open Library. Checks for existing Works. Creates a new Work, and Author, if required, otherwise associates the new Edition with the existing Work. :param dict rec: Edition record to add (no further checks at this point) :rtype: dict :return: { "success": False, "error": <error msg> } OR { "success": True, "work": {"key": <key>, "status": "created" | "modified" | "matched"}, "edition": {"key": <key>, "status": "created"}, "authors": [{"status": "matched", "name": "John Smith", "key": <key>}, ...] } """ cover_url = None if 'cover' in rec: cover_url = rec['cover'] del rec['cover'] try: # get an OL style edition dict edition = build_query(rec) except InvalidLanguage as e: return { 'success': False, 'error': str(e), } ekey = web.ctx.site.new_key('/type/edition') cover_id = None if cover_url: cover_id = add_cover(cover_url, ekey, account_key=account_key) if cover_id: edition['covers'] = [cover_id] edits = [] # Things (Edition, Work, Authors) to be saved reply = {} # TOFIX: edition.authors has already been processed by import_authors() in build_query(), following line is a NOP? author_in = [ import_author(a, eastern=east_in_by_statement(rec, a)) for a in edition.get('authors', []) ] # build_author_reply() adds authors to edits (authors, author_reply) = build_author_reply(author_in, edits, rec['source_records'][0]) if authors: edition['authors'] = authors reply['authors'] = author_reply wkey = None work_state = 'created' # Look for an existing work if 'authors' in edition: wkey = find_matching_work(edition) if wkey: w = web.ctx.site.get(wkey) work_state = 'matched' found_wkey_match = True need_update = False for k in subject_fields: if k not in rec: continue for s in rec[k]: if normalize(s) not in [ normalize(existing) for existing in w.get(k, []) ]: w.setdefault(k, []).append(s) need_update = True if cover_id: w.setdefault('covers', []).append(cover_id) need_update = True if need_update: work_state = 'modified' edits.append(w.dict()) else: # Create new work w = new_work(edition, rec, cover_id) wkey = w['key'] edits.append(w) assert wkey edition['works'] = [{'key': wkey}] edition['key'] = ekey edits.append(edition) web.ctx.site.save_many(edits, comment='import new book', action='add-book') # Writes back `openlibrary_edition` and `openlibrary_work` to # archive.org item after successful import: if 'ocaid' in rec: update_ia_metadata_for_ol_edition(ekey.split('/')[-1]) reply['success'] = True reply['edition'] = {'key': ekey, 'status': 'created'} reply['work'] = {'key': wkey, 'status': work_state} return reply