From c15acf7ca07173aef85325e3b6d2498bcabc675a Mon Sep 17 00:00:00 2001 From: Ren Tatsumoto Date: Sun, 17 Mar 2024 23:17:56 +0300 Subject: [PATCH] remove duplicate limit --- duplicate_notes.py | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/duplicate_notes.py b/duplicate_notes.py index d36b1b6..45a58e2 100644 --- a/duplicate_notes.py +++ b/duplicate_notes.py @@ -1,10 +1,9 @@ # Copyright: Ren Tatsumoto # License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html -from gettext import ngettext from collections.abc import Sequence -from anki.collection import Collection, OpChanges +from anki.collection import Collection, OpChanges, AddNoteRequest from anki.notes import Note from aqt import gui_hooks from aqt.browser import Browser @@ -14,34 +13,41 @@ from .config import config -LIMIT = 30 +def n_gettext_duplicate(n_notes: int, is_done: bool) -> str: + return f"Duplicate{'d' if is_done else ''} {n_notes} note{'s' if n_notes > 1 else ''}" -def duplicate_notes_op(col: Collection, notes: Sequence[Note]) -> OpChanges: - pos = col.add_custom_undo_entry(ngettext("Duplicate %d note", "Duplicate %d notes", len(notes)) % len(notes)) +def duplicate_notes_op(col: Collection, notes: Sequence[Note]) -> OpChanges: + pos = col.add_custom_undo_entry(n_gettext_duplicate(len(notes), is_done=False)) + requests: list[AddNoteRequest] = [] for ref_note in notes: new_note = Note(col, ref_note.note_type()) first_card = ref_note.cards()[0] for key in ref_note.keys(): new_note[key] = ref_note[key] new_note.tags = [tag for tag in ref_note.tags if tag != "leech" and tag != "marked"] - col.add_note(new_note, deck_id=(first_card.odid or first_card.did)) - + requests.append(AddNoteRequest(note=new_note, deck_id=(first_card.odid or first_card.did))) + col.add_notes(requests) return col.merge_undo_entries(pos) def duplicate_notes(browser: Browser) -> None: notes = [browser.col.get_note(note_id) for note_id in browser.selected_notes()] - if 1 <= len(notes) <= LIMIT: - CollectionOp(browser, lambda col: duplicate_notes_op(col, notes)).success( - lambda out: tooltip( - ngettext("%d note duplicated.", "%d notes duplicated.", len(notes)) % len(notes), parent=browser + if len(notes) > 0: + ( + CollectionOp( + parent=browser, + op=lambda col: duplicate_notes_op(col, notes), + ) + .success( + lambda out: tooltip(msg=n_gettext_duplicate(len(notes), is_done=True), parent=browser), ) - ).run_in_background() + .run_in_background() + ) else: - tooltip(f"Please select at most {LIMIT} notes.") + tooltip(f"Please select some notes.", parent=browser) def setup_context_menu(browser: Browser) -> None: