Compare commits

..

No commits in common. "ba67c52d9b75eb13a02d491d2292ac2702c99817" and "0514569621f428835c3da6894727ba5e2c5af864" have entirely different histories.

View File

@ -15,11 +15,10 @@
import aqt import aqt
required_anki_version = (26, 6, 3)
anki_version = tuple(int(segment) for segment in aqt.appVersion.split(".")) anki_version = tuple(int(segment) for segment in aqt.appVersion.split("."))
if anki_version < required_anki_version: if anki_version < (2, 1, 45):
raise Exception("Minimum Anki version supported: {required_anki_version[0]}.{required_anki_version[1]}.{required_anki_version[2]}") raise Exception("Minimum Anki version supported: 2.1.45")
import base64 import base64
import glob import glob
@ -311,7 +310,7 @@ class AnkiConnect:
val = note.fields[0] val = note.fields[0]
if not val.strip(): if not val.strip():
return 1 return 1
csum = anki.utils.field_checksum(val) csum = anki.utils.fieldChecksum(val)
# Create dictionary of deck ids # Create dictionary of deck ids
dids = None dids = None
@ -359,13 +358,13 @@ class AnkiConnect:
def getCard(self, card_id: int) -> Card: def getCard(self, card_id: int) -> Card:
try: try:
return self.collection().get_card(card_id) return self.collection().getCard(card_id)
except NotFoundError: except NotFoundError:
self.raiseNotFoundError('Card was not found: {}'.format(card_id)) self.raiseNotFoundError('Card was not found: {}'.format(card_id))
def getNote(self, note_id: int) -> Note: def getNote(self, note_id: int) -> Note:
try: try:
return self.collection().get_note(note_id) return self.collection().getNote(note_id)
except NotFoundError: except NotFoundError:
self.raiseNotFoundError('Note was not found: {}'.format(note_id)) self.raiseNotFoundError('Note was not found: {}'.format(note_id))
@ -568,7 +567,7 @@ class AnkiConnect:
self.startEditing() self.startEditing()
did = self.collection().decks.id(deck) did = self.collection().decks.id(deck)
mod = anki.utils.int_time() mod = anki.utils.intTime()
usn = self.collection().usn() usn = self.collection().usn()
# normal cards # normal cards
@ -613,7 +612,7 @@ class AnkiConnect:
collection = self.collection() collection = self.collection()
config['id'] = str(config['id']) config['id'] = str(config['id'])
config['mod'] = anki.utils.int_time() config['mod'] = anki.utils.intTime()
config['usn'] = collection.usn() config['usn'] = collection.usn()
if int(config['id']) not in [c['id'] for c in collection.decks.all_config()]: if int(config['id']) not in [c['id'] for c in collection.decks.all_config()]:
return False return False
@ -742,6 +741,7 @@ class AnkiConnect:
nCardsAdded = collection.addNote(ankiNote) nCardsAdded = collection.addNote(ankiNote)
if nCardsAdded < 1: if nCardsAdded < 1:
raise Exception('The field values you have provided would make an empty question on all cards.') raise Exception('The field values you have provided would make an empty question on all cards.')
collection.autosave()
return ankiNote.id return ankiNote.id
@ -829,7 +829,9 @@ class AnkiConnect:
pictureObjectOrList = note.get('picture') pictureObjectOrList = note.get('picture')
self.addMedia(ankiNote, pictureObjectOrList, util.MediaType.Picture) self.addMedia(ankiNote, pictureObjectOrList, util.MediaType.Picture)
self.collection().update_note(ankiNote, skip_undo_entry=True); ankiNote.flush()
self.collection().autosave()
@util.api() @util.api()
@ -893,8 +895,11 @@ class AnkiConnect:
# Update the tags # Update the tags
anki_note.tags = new_tags anki_note.tags = new_tags
# Update note to ensure changes are saved # Flush changes to ensure they are saved
collection.update_note(anki_note, skip_undo_entry=True); anki_note.flush()
# Save changes to the collection
collection.autosave()
@util.api() @util.api()
def updateNoteTags(self, note, tags): def updateNoteTags(self, note, tags):
@ -948,7 +953,7 @@ class AnkiConnect:
if note.has_tag(tag_to_replace): if note.has_tag(tag_to_replace):
note.remove_tag(tag_to_replace) note.remove_tag(tag_to_replace)
note.add_tag(replace_with_tag) note.add_tag(replace_with_tag)
self.collection().update_note(note, skip_undo_entry=True); note.flush()
self.window().requireReset() self.window().requireReset()
self.window().progress.finish() self.window().progress.finish()
@ -965,7 +970,7 @@ class AnkiConnect:
if note.has_tag(tag_to_replace): if note.has_tag(tag_to_replace):
note.remove_tag(tag_to_replace) note.remove_tag(tag_to_replace)
note.add_tag(replace_with_tag) note.add_tag(replace_with_tag)
self.collection().update_note(note, skip_undo_entry=True); note.flush()
self.window().requireReset() self.window().requireReset()
self.window().progress.finish() self.window().progress.finish()
@ -984,7 +989,7 @@ class AnkiConnect:
couldSetEaseFactors.append(True) couldSetEaseFactors.append(True)
ankiCard.factor = easeFactors[i] ankiCard.factor = easeFactors[i]
self.collection().update_card(ankiCard, skip_undo_entry=True) ankiCard.flush()
return couldSetEaseFactors return couldSetEaseFactors
@ -1014,7 +1019,7 @@ class AnkiConnect:
ankiCard = self.getCard(card) ankiCard = self.getCard(card)
for i, key in enumerate(keys): for i, key in enumerate(keys):
setattr(ankiCard, key, newValues[i]) setattr(ankiCard, key, newValues[i])
self.collection().update_card(ankiCard, skip_undo_entry=True) ankiCard.flush()
result.append(True) result.append(True)
except Exception as e: except Exception as e:
result.append([False, str(e)]) result.append([False, str(e)])