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
# 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):
needMapper = True
needDelimiter = False
update = True
importMode = 0
def __init__(self, col, file):
Importer.__init__(self, col, file)
@ -116,7 +121,7 @@ class NoteImporter(Importer):
" ".join(n.fields))
continue
# earlier in import?
if fld0 in firsts:
if fld0 in firsts and self.importMode != 2:
# duplicates in source file; log and ignore
self.log.append(_("Appeared twice in file: %s") %
fld0)
@ -133,12 +138,15 @@ class NoteImporter(Importer):
if fld0 == sflds[0]:
# duplicate
found = True
if self.update:
if self.importMode == 0:
data = self.updateData(n, id, sflds)
if data:
updates.append(data)
found = True
break
elif self.importMode == 2:
# allow duplicates in this case
found = False
# newly add
if not found:
data = self.newData(n)

View file

@ -159,10 +159,17 @@ def test_csv():
i.run()
n.load()
assert n.tags == ['test']
# if updating is disabled, count will be 0
i.update = False
# if add-only mode, count will be 0
i.importMode = 1
i.run()
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()
def test_csv2():