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-24 04:29:48 +00:00
|
|
|
import anki.hooks
|
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-24 03:13:45 +00:00
|
|
|
note = self.createNote(fields, deckName, modelName, tags)
|
2012-12-24 17:24:02 +00:00
|
|
|
if note is not None:
|
|
|
|
self.collection().addNote(note)
|
|
|
|
self.decks().save(self.currentDeck())
|
|
|
|
self.window().requireReset()
|
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-24 03:13:45 +00:00
|
|
|
model = self.findModel(modelName)
|
|
|
|
if model is None:
|
|
|
|
return None
|
2011-08-28 18:01:32 +00:00
|
|
|
|
2012-12-24 17:24:02 +00:00
|
|
|
deck = self.findDeck(deckName)
|
|
|
|
if deck is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
#~ conf = self.collection().conf
|
|
|
|
#~ deck = self.currentDeck()
|
|
|
|
#~ if conf['curModel'] != model['id'] or deck['mid'] != model['id']:
|
|
|
|
#~ conf['curModel'] = deck['mid'] = model['id']
|
|
|
|
#~ self.collection().decks.save(deck)
|
|
|
|
#~ anki.hooks.runHook('currentModelChanged')
|
|
|
|
#~ self.window().reset()
|
2012-12-24 04:29:48 +00:00
|
|
|
|
2012-12-24 17:24:02 +00:00
|
|
|
#~ self.collection().conf['curModel'] = model['id']
|
|
|
|
#~ deck['mid'] = model['id']
|
2012-12-24 04:29:48 +00:00
|
|
|
|
2012-12-24 17:24:02 +00:00
|
|
|
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-24 04:29:48 +00:00
|
|
|
return None if note.dupeOrEmpty() else note
|
2011-08-28 18:01:32 +00:00
|
|
|
|
|
|
|
|
2012-12-24 17:24:02 +00:00
|
|
|
#~ def browseNote(self, noteId):
|
|
|
|
#~ browser = ui.dialogs.get('CardList', self.window())
|
|
|
|
#~ browser.dialog.filterEdit.setText('fid:' + str(noteId))
|
|
|
|
#~ browser.updateSearch()
|
|
|
|
#~ browser.onnote()
|
2011-08-28 18:01:32 +00:00
|
|
|
|
|
|
|
|
2012-12-23 22:45:48 +00:00
|
|
|
def window(self):
|
|
|
|
return aqt.mw
|
2011-08-28 18:01:32 +00:00
|
|
|
|
|
|
|
|
2012-12-23 22:45:48 +00:00
|
|
|
def form(self):
|
|
|
|
return self.window().form
|
2011-08-28 18:01:32 +00:00
|
|
|
|
|
|
|
|
2012-12-23 22:45:48 +00:00
|
|
|
def toolsMenu(self):
|
|
|
|
return self.form().menuTools
|
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-24 01:50:49 +00:00
|
|
|
def modelFieldNames(self, model):
|
|
|
|
return [field['name'] for field in model['flds']]
|
2012-12-24 00:02:00 +00:00
|
|
|
|
|
|
|
|
2012-12-24 01:50:49 +00:00
|
|
|
def findModel(self, name):
|
2012-12-24 03:13:45 +00:00
|
|
|
return self.models().byName(name)
|
|
|
|
|
|
|
|
|
|
|
|
def currentModel(self):
|
|
|
|
return self.models().current()
|
2012-12-24 02:11:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
def decks(self):
|
|
|
|
return self.collection().decks
|
|
|
|
|
|
|
|
|
|
|
|
def deckNames(self):
|
|
|
|
return self.decks().allNames()
|
|
|
|
|
|
|
|
|
|
|
|
def findDeck(self, name):
|
2012-12-24 03:13:45 +00:00
|
|
|
return self.decks().byName(name)
|
|
|
|
|
|
|
|
|
|
|
|
def currentDeck(self):
|
|
|
|
return self.decks().current()
|
2012-12-24 02:11:14 +00:00
|
|
|
|