View Single Post
Old 09-08-2024, 08:45 AM   #2
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 12,447
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
I would do something like this. I didn't try to run it, but see below.
Code:
	# I assume you have an old API db reference. Get a new_api reference
	ndb = db.new_api
	book_to_id_map = {}
	for i, title, last_modified, isbn in extracted_ids:
		# Get the existing identifiers for the book
		identifiers_for_book = ndb.field_for('identifiers', i)
		# Add/replace the ISBN identifer
		identifiers_for_book['isbn'] = isbn
		# Save the (probably) updated list of identifiers
		book_to_id_map[i] = identifiers_for_book
	# Set all the books' identifier values
	ndb.set_field('identifiers', book_to_id_map)
As a test I ran this in the template tester. It worked. The first 4 books with more than one identifier ended up including "isbn:grumblebunny"
Code:
python:
def evaluate(book, context):
	db = context.db.new_api
	ids = list(db.search("identifiers:#>2"))
	book_to_id_map = {}
	for id_ in ids[0:4]:
		identifiers_for_book = db.field_for('identifiers', id_)
		identifiers_for_book['isbn'] = 'grumblebunny'
		book_to_id_map[id_] = identifiers_for_book
	db.set_field('identifiers', book_to_id_map)
	return 'foo'
chaley is offline   Reply With Quote