View Single Post
Old 12-18-2024, 06:10 AM   #59
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,452
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by Comfy.n View Post
Hi @chaley,

Could you please create a similar template to search for each author that has a Link and return only one book per author?
stored template: item_has_links.
Click image for larger version

Name:	Clipboard03.jpg
Views:	60
Size:	176.2 KB
ID:	212534

The template:
Spoiler:
Code:
python:
def evaluate(book, context):
	field_name = context.arguments[0] if len(context.arguments) == 1 else None
	if field_name is None:
		raise NameError("The argument for the field name wasn't provided")

	db = context.db.new_api
	items_with_links = context.globals.get('items_with_links')
	if items_with_links is None:
		items_with_links = db.get_link_map(field_name)
		context.globals['items_with_links'] = items_with_links

	vals = book.get(field_name)
	if vals is None:
		return ''
	if isinstance(vals, str):
		vals = tuple((vals,))
	for val in vals:
		if val in items_with_links:
			return '1'
	return ''

The documentation:
Spoiler:
Code:
Return ``True`` if any ``item`` in the ``field`` named by ``field_name``, the argument to the template, has a link.

Saved search "author_has_links:
Spoiler:
Code:
template:"""program: item_has_links('authors')#@#:b:yes"""
Search to show one book per author that has links:
Spoiler:
Code:
search:"=author_has_links" and search:"=one_book_per_author"

This attachment is an importable copy. Import of stored templates is available in calibre source as of 18 Dec 2024, and release 7.23.
Attached Files
File Type: txt item_has_links.txt (835 Bytes, 47 views)

Last edited by chaley; 12-18-2024 at 08:30 AM.
chaley is offline   Reply With Quote