2011-08-28 18:01:32 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2011-10-27 15:22:26 +00:00
|
|
|
|
2011-08-28 18:01:32 +00:00
|
|
|
# Copyright (C) 2011 Alex Yatskov
|
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
|
|
|
|
|
2012-12-23 22:11:48 +00:00
|
|
|
import aqt
|
2012-12-25 16:25:10 +00:00
|
|
|
import anki
|
2011-08-28 18:01:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Anki:
|
2012-12-24 17:24:02 +00:00
|
|
|
def addNote(self, deckName, modelName, fields, tags=list()):
|
2012-12-25 02:53:41 +00:00
|
|
|
note = self.createNote(deckName, modelName, fields, tags)
|
2012-12-24 17:24:02 +00:00
|
|
|
if note is not None:
|
2012-12-25 02:53:41 +00:00
|
|
|
collection = self.collection()
|
|
|
|
collection.addNote(note)
|
|
|
|
collection.autosave()
|
|
|
|
self.startEditing()
|
2012-12-25 16:25:10 +00:00
|
|
|
return note.id
|
2011-08-28 18:01:32 +00:00
|
|
|
|
|
|
|
|
2012-12-24 17:24:02 +00:00
|
|
|
def canAddNote(self, deckName, modelName, fields):
|
|
|
|
return bool(self.createNote(deckName, modelName, fields))
|
2011-08-28 18:01:32 +00:00
|
|
|
|
|
|
|
|
2012-12-24 17:24:02 +00:00
|
|
|
def createNote(self, deckName, modelName, fields, tags=list()):
|
2012-12-25 02:53:41 +00:00
|
|
|
model = self.models().byName(modelName)
|
2012-12-24 03:13:45 +00:00
|
|
|
if model is None:
|
|
|
|
return None
|
2011-08-28 18:01:32 +00:00
|
|
|
|
2012-12-25 02:53:41 +00:00
|
|
|
deck = self.decks().byName(deckName)
|
2012-12-24 17:24:02 +00:00
|
|
|
if deck is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
note = anki.notes.Note(self.collection(), model)
|
|
|
|
note.model()['did'] = deck['id']
|
|
|
|
note.tags = tags
|
2012-12-24 03:13:45 +00:00
|
|
|
|
2012-12-24 02:54:05 +00:00
|
|
|
for name, value in fields.items():
|
|
|
|
note[name] = value
|
|
|
|
|
2012-12-25 16:25:10 +00:00
|
|
|
if not note.dupeOrEmpty():
|
|
|
|
return note
|
2011-08-28 18:01:32 +00:00
|
|
|
|
|
|
|
|
2012-12-25 02:58:39 +00:00
|
|
|
def browseNote(self, noteId):
|
2012-12-25 16:25:10 +00:00
|
|
|
browser = aqt.dialogs.open('Browser', self.window())
|
|
|
|
browser.form.searchEdit.lineEdit().setText('nid:{0}'.format(noteId))
|
|
|
|
browser.onSearch()
|
2011-08-28 18:01:32 +00:00
|
|
|
|
|
|
|
|
2012-12-25 02:53:41 +00:00
|
|
|
def startEditing(self):
|
|
|
|
self.window().requireReset()
|
2011-08-28 18:01:32 +00:00
|
|
|
|
|
|
|
|
2012-12-25 02:53:41 +00:00
|
|
|
def stopEditing(self):
|
2012-12-25 16:39:21 +00:00
|
|
|
if self.collection():
|
|
|
|
self.window().maybeReset()
|
2012-12-25 02:53:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
def window(self):
|
|
|
|
return aqt.mw
|
2011-08-28 18:01:32 +00:00
|
|
|
|
|
|
|
|
2012-12-25 17:11:51 +00:00
|
|
|
def addUiAction(self, action):
|
|
|
|
self.window().form.menuTools.addAction(action)
|
2011-08-28 18:01:32 +00:00
|
|
|
|
|
|
|
|
2012-12-23 22:45:48 +00:00
|
|
|
def collection(self):
|
|
|
|
return self.window().col
|
2011-08-28 18:01:32 +00:00
|
|
|
|
|
|
|
|
2012-12-23 22:45:48 +00:00
|
|
|
def models(self):
|
|
|
|
return self.collection().models
|
2011-08-28 18:01:32 +00:00
|
|
|
|
|
|
|
|
2012-12-24 00:02:00 +00:00
|
|
|
def modelNames(self):
|
|
|
|
return self.models().allNames()
|
|
|
|
|
|
|
|
|
2012-12-25 02:53:41 +00:00
|
|
|
def modelFieldNames(self, modelName):
|
|
|
|
model = self.models().byName(modelName)
|
2012-12-25 16:25:10 +00:00
|
|
|
if model is not None:
|
|
|
|
return [field['name'] for field in model['flds']]
|
2012-12-24 02:11:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
def decks(self):
|
|
|
|
return self.collection().decks
|
|
|
|
|
|
|
|
|
|
|
|
def deckNames(self):
|
|
|
|
return self.decks().allNames()
|