forked from Arthur-Milchior/anki-copy-note
-
Notifications
You must be signed in to change notification settings - Fork 0
/
copyNote2.0.py
114 lines (93 loc) · 3.7 KB
/
copyNote2.0.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# -*- coding: utf-8 -*-
# Copyright: Arthur Milchior [email protected]
# License: GNU GPL, version 3 or later; http://www.gnu.org/copyleft/gpl.html
# Select any number of cards in the card browser and create exact copies of each card in the deck
# Feel free to contribute to this code on https://github.com/Arthur-Milchior/anki-copy-note
# Anki's add-on number: 1566928056
# This add-ons is heavily based on Kealan Hobelmann's addon 396494452
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import anki.notes
from anki.hooks import addHook
from anki.utils import intTime, timestampID
from aqt import mw
from aqt.utils import showWarning, tooltip
#import profile
def copyCards(nids, review):
mw.checkpoint("Copy Notes")
mw.progress.start()
# Copy notes
for nid in nids:
print(("Found note: %s" % (nid)))
note = mw.col.getNote(nid)
model = note._model
# Create new note
note_copy = anki.notes.Note(mw.col, model=model)
# Copy tags and fields (all model fields) from original note
note_copy.tags = note.tags
note_copy.fields = note.fields
note_copy.id = timestampID(note.col.db, "notes", note.id)
# Refresh note and add to database
note_copy.flush()
mw.col.addNote(note_copy)
nid_copy = note_copy.id
cards_copy = note_copy.cards()
cards = note.cards()
ord_to_card = {card.ord: card for card in cards}
ord_to_card_copy = {card.ord: card for card in cards_copy}
if review:
for card in cards:
ord = card.ord
card_copy = ord_to_card_copy.get(ord)
if card_copy:
card.id = card_copy.id
card.nid = nid_copy
else:
tooltip("We copy a card which should not exists.")
card.id = timestampID(mw.col.db, "cards")
card.nid = nid_copy
card.flush()
else:
for card_copy in cards_copy:
ord = card_copy.ord
card = ord_to_card.get(ord)
if card:
card_copy.did = card.odid or card.did
card_copy.flush()
# Reset collection and main window
mw.col.reset()
mw.reset()
tooltip(_("""Cards copied."""))
def setupMenu(browser):
a = QAction("Note Copy", browser)
# Shortcut for convenience. Added by Didi
a.setShortcut(QKeySequence("Ctrl+C"))
browser.connect(a, SIGNAL("triggered()"), lambda: onCopyCards(browser))
browser.form.menuEdit.addSeparator()
browser.form.menuEdit.addAction(a)
a = QAction("Full Notes Copy", browser)
# Shortcut for convenience. Added by Didi
a.setShortcut(QKeySequence("Ctrl+Alt+C"))
browser.connect(a, SIGNAL("triggered()"),
lambda: onCopyCards(browser, review=True))
browser.form.menuEdit.addAction(a)
def onCopyCards(browser, review=False):
copyCards(browser.selectedNotes(), review)
# profile.run("copyCards(browser.selectedNotes(),review)")
addHook("browser.setupMenus", setupMenu)
# def timestampID(db, table, t=None):
# "Return a non-conflicting timestamp for table."
# # be careful not to create multiple objects without flushing them, or they
# # may share an ID.
# t = t or intTime(1000)
# while db.scalar("select id from %s where id = ?" % table, t):
# t += 1
# return t
def timestampID(db, table, t=None):
"Return a non-conflicting timestamp for table."
# be careful not to create multiple objects without flushing them, or they
# may share an ID.
t = t or intTime(1000)
while db.scalar("select id from %s where id = ?" % table, t):
t += 1
return t