Add API to get all data associated with a note including searchable_text

This commit is contained in:
Kovid Goyal 2023-09-09 18:18:51 +05:30
parent 50019962b1
commit 8e8f038896
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 10 additions and 0 deletions

View File

@ -972,6 +972,9 @@ class DB:
def notes_for(self, field_name, item_id): def notes_for(self, field_name, item_id):
return self.notes.get_note(self.conn, field_name, item_id) or '' return self.notes.get_note(self.conn, field_name, item_id) or ''
def notes_data_for(self, field_name, item_id):
return self.notes.get_note_data(self.conn, field_name, item_id)
def set_notes_for(self, field, item_id, doc: str, searchable_text: str, resource_hashes, remove_unused_resources) -> int: def set_notes_for(self, field, item_id, doc: str, searchable_text: str, resource_hashes, remove_unused_resources) -> int:
id_val = self.tables[field].id_map[item_id] id_val = self.tables[field].id_map[item_id]
note_id = self.notes.set_note(self.conn, field, item_id, id_val, doc, resource_hashes, searchable_text) note_id = self.notes.set_note(self.conn, field, item_id, id_val, doc, resource_hashes, searchable_text)

View File

@ -680,6 +680,11 @@ class Cache:
def notes_for(self, field, item_id) -> str: def notes_for(self, field, item_id) -> str:
return self.backend.notes_for(field, item_id) return self.backend.notes_for(field, item_id)
@read_api
def notes_data_for(self, field, item_id) -> str:
' Return all notes data as a dict or None if note does not exist '
return self.backend.notes_data_for(field, item_id)
@write_api @write_api
def set_notes_for(self, field, item_id, doc: str, searchable_text: str = copy_marked_up_text, resource_hashes=(), remove_unused_resources=False) -> int: def set_notes_for(self, field, item_id, doc: str, searchable_text: str = copy_marked_up_text, resource_hashes=(), remove_unused_resources=False) -> int:
return self.backend.set_notes_for(field, item_id, doc, searchable_text, resource_hashes, remove_unused_resources) return self.backend.set_notes_for(field, item_id, doc, searchable_text, resource_hashes, remove_unused_resources)

View File

@ -64,6 +64,8 @@ def test_cache_api(self: 'NotesTest'):
h1 = cache.add_notes_resource(b'resource1', 'r1.jpg') h1 = cache.add_notes_resource(b'resource1', 'r1.jpg')
h2 = cache.add_notes_resource(b'resource2', 'r1.jpg') h2 = cache.add_notes_resource(b'resource2', 'r1.jpg')
cache.set_notes_for('authors', author_id, doc, resource_hashes=(h1, h2)) cache.set_notes_for('authors', author_id, doc, resource_hashes=(h1, h2))
nd = cache.notes_data_for('authors', author_id)
self.ae(nd, {'id': 1, 'searchable_text': authors[0] + '\n' + doc, 'doc': doc, 'resource_hashes': frozenset({h1, h2})})
# test renaming to a new author preserves notes # test renaming to a new author preserves notes
cache.rename_items('authors', {author_id: 'renamed author'}) cache.rename_items('authors', {author_id: 'renamed author'})
raid = cache.get_item_id('authors', 'renamed author') raid = cache.get_item_id('authors', 'renamed author')