-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path__init__.py
122 lines (98 loc) · 3.84 KB
/
__init__.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
115
116
117
118
119
120
121
122
# -*- coding: utf-8 -*-
# Ivan Schurawel
# https://github.com/is343
# October 21, 2018
# January 22, 2019
# August 9, 2020
# August 16, 2020
# January 30, 2024
"""
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
from aqt import mw
# import all of the Qt GUI library
from aqt.qt import *
from anki.cards import Card
from aqt.utils import askUser, getOnlyText, showWarning, tooltip
from anki.hooks import addHook
from anki.utils import pointVersion
import time
import datetime
DUE = 2
SUSPENDED = -1
IS_AT_LEAST_VERSION_23 = pointVersion() >= 231000
def onDeckBrowserDelayCards(did):
deckManager = mw.col.decks
deck_name = deckManager.name(did)
cids = deckManager.cids(did, children=True)
if not cids:
tooltip("Deck contains no cards.")
return
cards = [Card(mw.col, cid) for cid in cids]
days_to_delay = calculate_delay(did, cards)
days_text = "days"
if days_to_delay == 1:
days_text = "day"
confirm_response = askUser("The oldest card is {0} {1} overdue."
" Delay all cards by {0} {1}?"
"<br><br>If you press 'No' you will be able to manually enter"
" how many days to delay by.".format(days_to_delay, days_text),
defaultno=True, title="Confirm Delay")
if not confirm_response:
days_to_delay = getOnlyText("How many days would you like to delay? (Negative numbers will bring days forward)")
if not days_to_delay:
return
try:
days_to_delay = int(days_to_delay)
if type(days_to_delay) != int:
raise ValueError('Not a valid int')
return
except:
showWarning("Please only enter whole numbers")
return
delay_cards(did, deckManager, cards, days_to_delay)
def calculate_delay(did, cards):
today_date = datetime.date.today()
collection_creation = mw.col.crt
collection_creation_date = datetime.date.fromtimestamp(collection_creation)
difference = (today_date - collection_creation_date).days
max_days_due = 0
for card in cards:
if card.type == DUE and card.queue != SUSPENDED and card.due <= difference:
days_due = difference - card.due
if days_due > max_days_due:
max_days_due = days_due
return max_days_due
def delay_cards(did, deckManager, cards, days_to_delay):
for card in cards:
if card.type == DUE:
adjusted_due_date = card.due + days_to_delay
card.due = adjusted_due_date
if IS_AT_LEAST_VERSION_23:
mw.col.update_card(card)
else: # older versions
card.flush()
deck = deckManager.get(did)
mw.col.decks.save(deck)
mw.deckBrowser.refresh()
main_text = "Delayed deck by:"
days_text = "days"
if days_to_delay == 1 | days_to_delay == -1:
days_text = "day"
if days_to_delay < 0:
main_text = "Deck brought forward by:"
days_to_delay *= -1
tooltip("{0} {1} {2}".format(main_text, days_to_delay, days_text))
def onDeckBrowserShowOptions(menu, did):
a = menu.addAction("Delay Overdue")
a.triggered.connect(lambda _, did=did: onDeckBrowserDelayCards(did))
addHook('showDeckOptions', onDeckBrowserShowOptions)