allow duplicates in import

This commit is contained in:
Damien Elmes 2012-10-23 17:52:55 +09:00
parent 0c1e98d0b2
commit eb7240d220
2 changed files with 20 additions and 5 deletions

View file

@ -38,11 +38,16 @@ class ForeignCard(object):
# - _tags maps to note tags # - _tags maps to note tags
# If the first field of the model is not in the map, the map is invalid. # If the first field of the model is not in the map, the map is invalid.
# The import mode is one of:
# 0: update if first field matches existing note
# 1: ignore if first field matches existing note
# 2: import even if first field matches existing note
class NoteImporter(Importer): class NoteImporter(Importer):
needMapper = True needMapper = True
needDelimiter = False needDelimiter = False
update = True importMode = 0
def __init__(self, col, file): def __init__(self, col, file):
Importer.__init__(self, col, file) Importer.__init__(self, col, file)
@ -116,7 +121,7 @@ class NoteImporter(Importer):
" ".join(n.fields)) " ".join(n.fields))
continue continue
# earlier in import? # earlier in import?
if fld0 in firsts: if fld0 in firsts and self.importMode != 2:
# duplicates in source file; log and ignore # duplicates in source file; log and ignore
self.log.append(_("Appeared twice in file: %s") % self.log.append(_("Appeared twice in file: %s") %
fld0) fld0)
@ -133,12 +138,15 @@ class NoteImporter(Importer):
if fld0 == sflds[0]: if fld0 == sflds[0]:
# duplicate # duplicate
found = True found = True
if self.update: if self.importMode == 0:
data = self.updateData(n, id, sflds) data = self.updateData(n, id, sflds)
if data: if data:
updates.append(data) updates.append(data)
found = True found = True
break break
elif self.importMode == 2:
# allow duplicates in this case
found = False
# newly add # newly add
if not found: if not found:
data = self.newData(n) data = self.newData(n)

View file

@ -159,10 +159,17 @@ def test_csv():
i.run() i.run()
n.load() n.load()
assert n.tags == ['test'] assert n.tags == ['test']
# if updating is disabled, count will be 0 # if add-only mode, count will be 0
i.update = False i.importMode = 1
i.run() i.run()
assert i.total == 0 assert i.total == 0
# and if dupes mode, will reimport everything
assert deck.cardCount() == 5
i.importMode = 2
i.run()
# includes repeated field
assert i.total == 6
assert deck.cardCount() == 11
deck.close() deck.close()
def test_csv2(): def test_csv2():