Skip to content

Commit

Permalink
remove duplicate limit
Browse files Browse the repository at this point in the history
  • Loading branch information
tatsumoto-ren committed Mar 17, 2024
1 parent 3214f8e commit c15acf7
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions duplicate_notes.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# Copyright: Ren Tatsumoto <tatsu at autistici.org>
# 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
Expand All @@ -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:
Expand Down

0 comments on commit c15acf7

Please sign in to comment.