fix updating

This commit is contained in:
Damien Elmes 2012-02-29 12:49:42 +09:00
parent 7189e57e80
commit 099ba71bb0
3 changed files with 18 additions and 13 deletions

View file

@ -17,6 +17,7 @@ class TextImporter(NoteImporter):
self.lines = None
self.fileobj = None
self.delimiter = None
self.tagsToAdd = []
def foreignNotes(self):
self.sniff()
@ -131,5 +132,5 @@ class TextImporter(NoteImporter):
def noteFromFields(self, fields):
note = ForeignNote()
note.fields.extend([x.strip() for x in fields])
print "fixme - add tagsToAdd to note tags"
note.tags.extend(self.tagsToAdd)
return note

View file

@ -5,10 +5,9 @@
import time
from anki.lang import _
from anki.utils import fieldChecksum, ids2str, guid64, timestampID, \
joinFields, intTime
joinFields, intTime, splitFields
from anki.errors import *
from anki.importing.base import Importer
#from anki.deck import NEW_CARDS_RANDOM
# Stores a list of fields, tags and deck
######################################################################
@ -20,12 +19,9 @@ class ForeignNote(object):
self.tags = []
self.deck = None
# Base class for csv/supermemo/etc importers
# Base class for CSV and similar text-based imports
######################################################################
# - instead of specifying an update key, do it by default using first field
# The mapping is list of input fields, like:
# ['Expression', 'Reading', '_tags', None]
# - None means that the input should be discarded
@ -47,7 +43,6 @@ class NoteImporter(Importer):
def run(self):
"Import."
print "fixme: randomize"
assert self.mapping
c = self.foreignNotes()
self.importNotes(c)
@ -103,9 +98,12 @@ class NoteImporter(Importer):
"select flds from notes where id = ?", id)
if fld0 == splitFields(flds)[0]:
# duplicate
data = self.updateData(n, id)
if data:
updates.append(data)
if self.update:
data = self.updateData(n, id)
if data:
updates.append(data)
# note that we've seen this note once already
csums[fieldChecksum(n.fields[0])] = -1
break
# newly add
else:

View file

@ -82,12 +82,18 @@ def test_csv():
i = TextImporter(deck, file)
i.mapping = ['Front', 'Back']
i.run()
print i.log
# four problems - too many & too few fields, a missing front, and a
# duplicate entry
assert len(i.log) == 4
assert i.total == 5
print deck.db.all("select * from notes")
# if we run the import again, it should update instead
i.run()
assert len(i.log) == 4
assert i.total == 5
# if updating is disabled, count will be 0
i.update = False
i.run()
assert i.total == 0
deck.close()
def test_csv_tags():