diff --git a/ankihub/db/db.py b/ankihub/db/db.py index a0cf14d28..9456512e4 100644 --- a/ankihub/db/db.py +++ b/ankihub/db/db.py @@ -39,7 +39,7 @@ from ..ankihub_client import Field, NoteInfo, suggestion_type_from_str from ..ankihub_client.models import DeckMedia as DeckMediaClientModel from ..ankihub_client.models import SuggestionType -from ..common_utils import get_media_names_from_note_field +from ..common_utils import get_media_names_from_note_field, get_media_names_from_note_type from ..settings import ANKIHUB_NOTE_TYPE_FIELD_NAME from .exceptions import IntegrityError, MissingValueError from .models import ( @@ -452,14 +452,25 @@ def media_names_for_ankihub_deck(self, ah_did: uuid.UUID) -> Set[str]: NOTE_NOT_DELETED_CONDITION, ankihub_deck_id=ah_did, ) - - return { + note_type_ids = set(note.anki_note_type_id for note in notes) + note_types: Dict[int, NotetypeDict] = { + nt.anki_note_type_id: nt.note_type_dict + for nt in AnkiHubNoteType.select(AnkiHubNoteType.anki_note_type_id, AnkiHubNoteType.note_type_dict).filter( + anki_note_type_id__in=note_type_ids + ) + } + note_type_refs = { + name for note_type in note_types.values() for name in get_media_names_from_note_type(note_type) + } + note_refs = { media_name for note in notes for field_value in (note.fields.values() if note.fields else []) - for media_name in get_media_names_from_note_field(field_value, self.note_type_dict(note.anki_note_type_id)) + for media_name in get_media_names_from_note_field(field_value, note_types[note.anki_note_type_id]) } + return {*note_type_refs, *note_refs} + def media_names_exist_for_ankihub_deck(self, ah_did: uuid.UUID, media_names: Set[str]) -> Dict[str, bool]: """Returns a dictionary where each key is a media name and the corresponding value is a boolean indicating whether the media file is referenced on a note in the given deck. diff --git a/tests/addon/test_unit.py b/tests/addon/test_unit.py index db0a032c7..6456f7428 100644 --- a/tests/addon/test_unit.py +++ b/tests/addon/test_unit.py @@ -1403,6 +1403,7 @@ def setup_method_fixture( ankihub_basic_note_type: NotetypeDict, next_deterministic_uuid: Callable[[], uuid.UUID], ): + ankihub_basic_note_type["tmpls"][0]["qfmt"] += "" self.mid = ankihub_basic_note_type["id"] note_info = NoteInfoFactory.create( mid=self.mid, @@ -1430,6 +1431,7 @@ def test_basic( "test1.jpg", "test2.jpg", "test3.mp3", + "_test4.jpg", }