From e3da64f00bf44a3da64d130eaf815d98b7d329fa Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Fri, 21 Sep 2012 15:51:45 +0900 Subject: [PATCH] don't clobber tags if they're not mapped --- anki/importing/noteimp.py | 24 +++++++++++++++++++----- tests/test_importing.py | 7 +++++++ 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/anki/importing/noteimp.py b/anki/importing/noteimp.py index 497e54e07..5d51baceb 100644 --- a/anki/importing/noteimp.py +++ b/anki/importing/noteimp.py @@ -85,6 +85,11 @@ class NoteImporter(Importer): def importNotes(self, notes): "Convert each card into a note, apply attributes and add to col." assert self.mappingOk() + # note whether tags are mapped + self._tagsMapped = False + for f in self.mapping: + if f == "_tags": + self._tagsMapped = True # gather checks for duplicate comparison csums = {} for csum, id in self.col.db.execute( @@ -182,16 +187,25 @@ class NoteImporter(Importer): if not self.processFields(n, sflds): print "no cards generated" return - self.col.tags.register(n.tags) - tags = self.col.tags.join(n.tags) - return [intTime(), self.col.usn(), n.fieldsStr, tags, - id, n.fieldsStr, tags] + if self._tagsMapped: + self.col.tags.register(n.tags) + tags = self.col.tags.join(n.tags) + return [intTime(), self.col.usn(), n.fieldsStr, tags, + id, n.fieldsStr, tags] + else: + return [intTime(), self.col.usn(), n.fieldsStr, + id, n.fieldsStr] def addUpdates(self, rows): old = self.col.db.totalChanges() - self.col.db.executemany(""" + if self._tagsMapped: + self.col.db.executemany(""" update notes set mod = ?, usn = ?, flds = ?, tags = ? where id = ? and (flds != ? or tags != ?)""", rows) + else: + self.col.db.executemany(""" +update notes set mod = ?, usn = ?, flds = ? +where id = ? and flds != ?""", rows) self.updateCount = self.col.db.totalChanges() - old def processFields(self, note, fields=None): diff --git a/tests/test_importing.py b/tests/test_importing.py index 17e78bc06..b525a9b13 100644 --- a/tests/test_importing.py +++ b/tests/test_importing.py @@ -152,6 +152,13 @@ def test_csv(): i.run() assert len(i.log) == 5 assert i.total == 5 + # but importing should not clobber tags if they're unmapped + n = deck.getNote(deck.db.scalar("select id from notes")) + n.addTag("test") + n.flush() + i.run() + n.load() + assert n.tags == ['test'] # if updating is disabled, count will be 0 i.update = False i.run()