Ignore option closeAfterAdding of guiAddCards

The functionality was broken, creating a dialog that was not,
in fact, closing after adding a card.

See the deleted comment in `test_graphical.py`
This commit is contained in:
oakkitten 2022-04-07 09:04:46 +01:00
parent 8d507908c7
commit c53aa86a0d
3 changed files with 2 additions and 126 deletions

View File

@ -1015,9 +1015,6 @@ corresponding to when the API was available for use.
Audio, video, and picture files can be embedded into the fields via the `audio`, `video`, and `picture` keys, respectively. Audio, video, and picture files can be embedded into the fields via the `audio`, `video`, and `picture` keys, respectively.
Refer to the documentation of `addNote` and `storeMediaFile` for an explanation of these fields. Refer to the documentation of `addNote` and `storeMediaFile` for an explanation of these fields.
The `closeAfterAdding` member inside `options` group can be set to true to create a dialog that closes upon adding the note.
Invoking the action mutliple times with this option will create _multiple windows_.
The result is the ID of the note which would be added, if the user chose to confirm the *Add Cards* dialogue. The result is the ID of the note which would be added, if the user chose to confirm the *Add Cards* dialogue.
*Sample request*: *Sample request*:
@ -1033,9 +1030,6 @@ corresponding to when the API was available for use.
"Text": "The capital of Romania is {{c1::Bucharest}}", "Text": "The capital of Romania is {{c1::Bucharest}}",
"Extra": "Romania is a country in Europe" "Extra": "Romania is a country in Europe"
}, },
"options": {
"closeAfterAdding": true
},
"tags": [ "tags": [
"countries" "countries"
], ],

View File

@ -1400,91 +1400,6 @@ class AnkiConnect:
collection.models.setCurrent(model) collection.models.setCurrent(model)
collection.models.update(model) collection.models.update(model)
closeAfterAdding = False
if note is not None and 'options' in note:
if 'closeAfterAdding' in note['options']:
closeAfterAdding = note['options']['closeAfterAdding']
if type(closeAfterAdding) is not bool:
raise Exception('option parameter \'closeAfterAdding\' must be boolean')
addCards = None
if closeAfterAdding:
randomString = ''.join(random.choice(string.ascii_letters) for _ in range(10))
windowName = 'AddCardsAndClose' + randomString
class AddCardsAndClose(aqt.addcards.AddCards):
def __init__(self, mw):
# the window must only reset if
# * function `onModelChange` has been called prior
# * window was newly opened
self.modelHasChanged = True
super().__init__(mw)
self.addButton.setText('Add and Close')
self.addButton.setShortcut(aqt.qt.QKeySequence('Ctrl+Return'))
def _addCards(self):
super()._addCards()
# if adding was successful it must mean it was added to the history of the window
if len(self.history):
self.reject()
def onModelChange(self):
if self.isActiveWindow():
super().onModelChange()
self.modelHasChanged = True
def onReset(self, model=None, keep=False):
if self.isActiveWindow() or self.modelHasChanged:
super().onReset(model, keep)
self.modelHasChanged = False
else:
# modelchoosers text is changed by a reset hook
# therefore we need to change it back manually
self.modelChooser.models.setText(self.editor.note.model()['name'])
self.modelHasChanged = False
def _reject(self):
savedMarkClosed = aqt.dialogs.markClosed
aqt.dialogs.markClosed = lambda _: savedMarkClosed(windowName)
super()._reject()
aqt.dialogs.markClosed = savedMarkClosed
aqt.dialogs._dialogs[windowName] = [AddCardsAndClose, None]
addCards = aqt.dialogs.open(windowName, self.window())
if savedMid:
deck['mid'] = savedMid
editor = addCards.editor
ankiNote = editor.note
if 'fields' in note:
for name, value in note['fields'].items():
if name in ankiNote:
ankiNote[name] = value
self.addMediaFromNote(ankiNote, note)
editor.loadNote()
if 'tags' in note:
ankiNote.tags = note['tags']
editor.updateTags()
# if Anki does not Focus, the window will not notice that the
# fields are actually filled
aqt.dialogs.open(windowName, self.window())
addCards.setAndFocusNote(editor.note)
return ankiNote.id
elif note is not None:
collection = self.collection()
ankiNote = anki.notes.Note(collection, model) ankiNote = anki.notes.Note(collection, model)
# fill out card beforehand, so we can be sure of the note id # fill out card beforehand, so we can be sure of the note id

View File

@ -36,12 +36,6 @@ class TestAddCards:
"tags": ["tag1"] "tags": ["tag1"]
} }
options_closeAfterAdding = {
"options": {
"closeAfterAdding": True
}
}
# an actual small image, you can see it if you run the test with GUI # an actual small image, you can see it if you run the test with GUI
# noinspection SpellCheckingInspection # noinspection SpellCheckingInspection
base64_gif = "R0lGODlhBQAEAHAAACwAAAAABQAEAIH///8AAAAAAAAAAAACB0QMqZcXDwoAOw==" base64_gif = "R0lGODlhBQAEAHAAACwAAAAABQAEAIH///8AAAAAAAAAAAACB0QMqZcXDwoAOw=="
@ -57,8 +51,8 @@ class TestAddCards:
} }
@staticmethod @staticmethod
def click_on_add_card_dialog_save_button(dialog_name="AddCards"): def click_on_add_card_dialog_save_button():
dialog = aqt.dialogs._dialogs[dialog_name][1] dialog = aqt.dialogs._dialogs["AddCards"][1]
dialog.addButton.click() dialog.addButton.click()
# todo previously, these tests were verifying # todo previously, these tests were verifying
@ -83,33 +77,6 @@ class TestAddCards:
assert len(ac.findCards(query="new")) == 1 assert len(ac.findCards(query="new")) == 1
assert ac.retrieveMediaFile(filename="smiley.gif") == self.base64_gif assert ac.retrieveMediaFile(filename="smiley.gif") == self.base64_gif
# todo the tested method, when called with option `closeAfterAdding=True`,
# is broken for the following reasons:
# * it uses the note that comes with dialog's Editor.
# this note might be of a different model than the proposed note,
# and field values from the proposed note can't be put into it.
# * most crucially, `AddCardsAndClose` is trying to override the method
# `_addCards` that is no longer present or called by the superclass.
# also, it creates and registers a new class each time it is called.
# todo fix the method, or ignore/disallow the option `closeAfterAdding`?
@pytest.mark.skip("API method `guiAddCards` is broken "
"when called with note option `closeAfterAdding=True`")
def test_with_note_and_closeAfterAdding(self, setup):
def find_AddCardsAndClose_dialog_registered_name():
for name in aqt.dialogs._dialogs.keys():
if name.startswith("AddCardsAndClose"):
return name
def dialog_is_open(name):
return aqt.dialogs._dialogs[name][1] is not None
ac.guiAddCards(note={**self.note, **self.options_closeAfterAdding})
dialog_name = find_AddCardsAndClose_dialog_registered_name()
assert dialog_is_open(dialog_name)
self.click_on_add_card_dialog_save_button(dialog_name)
wait_until(aqt.dialogs.allClosed)
class TestReviewActions: class TestReviewActions:
@pytest.fixture @pytest.fixture