diff --git a/anki/importing/noteimp.py b/anki/importing/noteimp.py index 5d51baceb..ccb4fedf2 100644 --- a/anki/importing/noteimp.py +++ b/anki/importing/noteimp.py @@ -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) diff --git a/tests/test_importing.py b/tests/test_importing.py index b525a9b13..a40e68bb2 100644 --- a/tests/test_importing.py +++ b/tests/test_importing.py @@ -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():