Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions ankihub/db/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions tests/addon/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"] += "<img src='_test4.jpg'>"
self.mid = ankihub_basic_note_type["id"]
note_info = NoteInfoFactory.create(
mid=self.mid,
Expand Down Expand Up @@ -1430,6 +1431,7 @@ def test_basic(
"test1.jpg",
"test2.jpg",
"test3.mp3",
"_test4.jpg",
}


Expand Down
Loading