Added guiCheckDatabase action and fix #202 in saveDeckConfig (#271)

* Added guiCheckDatabase action

This action initiates a check database action in Anki but doesn’t wait for the action to complete before returning.

* Fixed #202 no dconf attribute in saveDeckConfig

Anki manages access to deck configuration differently now. This adapts saveDeckConfig to new style of access.
This commit is contained in:
NSBum 2021-07-13 20:55:22 -04:00 committed by GitHub
parent 2150940c7e
commit 8404bd782d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 9 deletions

View File

@ -1230,6 +1230,26 @@ corresponding to when the API was available for use.
} }
``` ```
* **guiCheckDatabase**
Requests a database check, but returns immediately without waiting for the check to complete. Therefore, the action will always return `true` even if errors are detected during the database check.
*Sample request*:
```json
{
"action": "guiCheckDatabase",
"version": 6
}
```
*Sample result*:
```json
{
"result": true,
"error": null
}
```
#### Media Actions #### Media Actions
* **storeMediaFile** * **storeMediaFile**

View File

@ -363,7 +363,7 @@ class AnkiConnect:
return { return {
"permission": "denied", "permission": "denied",
} }
msg = QMessageBox(None) msg = QMessageBox(None)
msg.setWindowTitle("A website want to access to Anki") msg.setWindowTitle("A website want to access to Anki")
msg.setText(origin + " request permission to use Anki through AnkiConnect.\nDo you want to give it access ?") msg.setText(origin + " request permission to use Anki through AnkiConnect.\nDo you want to give it access ?")
@ -550,12 +550,13 @@ class AnkiConnect:
config['id'] = str(config['id']) config['id'] = str(config['id'])
config['mod'] = anki.utils.intTime() 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 config['id'] not in collection.decks.dconf: return False
try:
collection.decks.save(config)
collection.decks.updateConf(config)
except:
return False return False
collection.decks.dconf[config['id']] = config
collection.decks.changed = True
return True return True
@ -1088,14 +1089,14 @@ class AnkiConnect:
checkForText = False checkForText = False
if css and findText in model['css']: if css and findText in model['css']:
checkForText = True checkForText = True
model['css'] = model['css'].replace(findText, replaceText) model['css'] = model['css'].replace(findText, replaceText)
for tmpls in model.get('tmpls'): for tmpls in model.get('tmpls'):
if front and findText in tmpls['qfmt']: if front and findText in tmpls['qfmt']:
checkForText = True checkForText = True
tmpls['qfmt'] = tmpls['qfmt'].replace(findText, replaceText) tmpls['qfmt'] = tmpls['qfmt'].replace(findText, replaceText)
if back and findText in tmpls['afmt']: if back and findText in tmpls['afmt']:
checkForText = True checkForText = True
tmpls['afmt'] = tmpls['afmt'].replace(findText, replaceText) tmpls['afmt'] = tmpls['afmt'].replace(findText, replaceText)
self.collection().models.save(model, True) self.collection().models.save(model, True)
self.collection().models.flush() self.collection().models.flush()
if checkForText: if checkForText:
@ -1188,7 +1189,7 @@ class AnkiConnect:
scids = anki.utils.ids2str(cards) scids = anki.utils.ids2str(cards)
self.collection().db.execute('update cards set type=3, queue=1 where id in ' + scids) self.collection().db.execute('update cards set type=3, queue=1 where id in ' + scids)
self.stopEditing() self.stopEditing()
@util.api() @util.api()
def cardReviews(self, deck, startID): def cardReviews(self, deck, startID):
@ -1595,6 +1596,12 @@ class AnkiConnect:
timer.start(1000) # 1s should be enough to allow the response to be sent. timer.start(1000) # 1s should be enough to allow the response to be sent.
@util.api()
def guiCheckDatabase(self):
self.window().onCheckDB()
return True
@util.api() @util.api()
def addNotes(self, notes): def addNotes(self, notes):
results = [] results = []

View File

@ -62,6 +62,9 @@ class TestGui(unittest.TestCase):
# guiDeckBrowser # guiDeckBrowser
util.invoke('guiDeckBrowser') util.invoke('guiDeckBrowser')
# guiDatabaseCheck
util.invoke('guiDatabaseCheck')
# guiExitAnki # guiExitAnki
# util.invoke('guiExitAnki') # util.invoke('guiExitAnki')