Skip to content

Commit

Permalink
Implement failing cards when attempting to add duplicate
Browse files Browse the repository at this point in the history
  • Loading branch information
1over137 committed Mar 21, 2024
1 parent ac4446f commit 9282863
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
13 changes: 10 additions & 3 deletions vocabsieve/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from PyQt5.QtWidgets import QApplication, QMessageBox, QAction, QShortcut, QFileDialog

import qdarktheme

from .global_names import datapath, lock, app, settings # First local import
from .analyzer import BookAnalyzer
from .config import ConfigDialog
Expand All @@ -30,6 +31,7 @@
from .contentmanager import ContentManager
from .tools import (
compute_word_score,
failCards,
is_json,
make_audio_source_group,
modelFieldNames,
Expand Down Expand Up @@ -897,13 +899,14 @@ def createNote(self) -> None:
msgBox = QMessageBox()
msgBox.setIcon(QMessageBox.Warning)
msgBox.setText(
f'Note(s) with {self.note_type_first_field} "{self.word.text() if self.note_type_first_field == "word" else sentence}" already exists in Anki. <br>' +
f"Do you still want to add the note?<br>" +
"<br>".join(
f'Note(s) with {self.note_type_first_field} "{self.word.text() if self.note_type_first_field == "word" else sentence}" already exists in your Anki database.\n' +
f"Do you still want to add the note?\n" +
"\n".join(
f"Note id: {id}, created {unix_milliseconds_to_datetime_str(id)}" for id in note_ids))
msgBox.setWindowTitle("Note already exists")
msgBox.addButton("Add anyway", QMessageBox.AcceptRole)
msgBox.addButton("Cancel", QMessageBox.RejectRole)
msgBox.addButton("Fail existing", QMessageBox.DestructiveRole)
msgBox.addButton("View note(s)", QMessageBox.HelpRole)
msgBox.exec()
result = msgBox.buttonRole(msgBox.clickedButton())
Expand All @@ -917,6 +920,10 @@ def createNote(self) -> None:
elif result == QMessageBox.AcceptRole:
logger.info("User decided to add duplicate note")
allow_duplicates = True
elif result == QMessageBox.DestructiveRole:
logger.info("User decided to fail existing note")
failCards(settings.value("anki_api"), note_ids)
return

anki_settings = self.getAnkiSettings()

Expand Down
28 changes: 19 additions & 9 deletions vocabsieve/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,29 @@ def modelFieldNames(server, modelName):
return invoke('modelFieldNames', server, modelName=modelName)


def failCards(server, note_ids):
notes = notesInfo(server, note_ids)
card_ids = []
for note in notes:
card_ids.extend(note['cards'])
logger.info("Failing cards: " + str(card_ids))
answers = []
for card_id in card_ids:
answers.append({
"cardId": card_id,
"ease": 1
})
return invoke('answerCards', server, answers=answers)


def findNotes(server, query):
return invoke('findNotes', server, query=query)


def findCards(server, query):
return invoke('findCards', server, query=query)


def guiBrowse(server, query):
return invoke('guiBrowse', server, query=query)

Expand All @@ -189,15 +208,6 @@ def is_json(myjson) -> bool:
return False


def failed_lookup(word) -> str:
return str("<b>Definition for \"" + str(word) + "\" not found.</b><br>Check the following:<br>" +
"- Language setting (Current: " + settings.value("target_language", 'en') + ")<br>" +
"- Is the correct word being looked up?<br>" +
"- Are you connected to the Internet?<br>" +
"Otherwise, then " + settings.value("dict_source", "Wiktionary (English)") +
" probably just does not have this word listed.")


def is_oneword(s) -> bool:
return len(s.split()) == 1

Expand Down

0 comments on commit 9282863

Please sign in to comment.