make sure we update guid in note object too

This commit is contained in:
Damien Elmes 2012-10-27 21:11:16 +09:00
parent bd1c6d4395
commit 29e93f8c1d
4 changed files with 24 additions and 5 deletions

View file

@ -97,9 +97,10 @@ class Anki2Importer(Importer):
note = list(note) note = list(note)
guid, mid = note[1:3] guid, mid = note[1:3]
duplicate = False duplicate = False
guidChange = False first = True
# do we have the same guid? # do we have the same guid?
if guid in self._notes: if guid in self._notes:
first = False
# and do they share the same model id? # and do they share the same model id?
if self._notes[guid][2] == mid: if self._notes[guid][2] == mid:
# and do they share the same schema? # and do they share the same schema?
@ -109,9 +110,6 @@ class Anki2Importer(Importer):
self.src.models.scmhash(dstM)): self.src.models.scmhash(dstM)):
# then it's safe to treat as a duplicate # then it's safe to treat as a duplicate
duplicate = True duplicate = True
if not duplicate:
# not identical models, so we need to change guid
guidChange = True
# missing from local col or divergent model? # missing from local col or divergent model?
if not duplicate: if not duplicate:
# get corresponding local model # get corresponding local model
@ -129,8 +127,9 @@ class Anki2Importer(Importer):
dirty.append(note[0]) dirty.append(note[0])
# if it was originally the same as a note in this deck but the # if it was originally the same as a note in this deck but the
# models have diverged, we need to change the guid # models have diverged, we need to change the guid
if guidChange: if not first:
guid = guid64() guid = guid64()
note[1] = guid
# note we have the added note # note we have the added note
self._notes[guid] = (note[0], note[3], note[2]) self._notes[guid] = (note[0], note[3], note[2])
else: else:

Binary file not shown.

Binary file not shown.

View file

@ -158,6 +158,26 @@ def test_anki1_diffmodels():
# as the model schemas differ, should have been imported as new model # as the model schemas differ, should have been imported as new model
assert after == before + 1 assert after == before + 1
def test_anki2_diffmodels():
# create a new empty deck
dst = getEmptyDeck()
# import the 1 card version of the model
tmp = getUpgradeDeckPath("diffmodels2-1.apkg")
imp = AnkiPackageImporter(dst, tmp)
imp.run()
before = dst.noteCount()
# repeating the process should do nothing
imp = AnkiPackageImporter(dst, tmp)
imp.run()
assert before == dst.noteCount()
# then the 2 card version
tmp = getUpgradeDeckPath("diffmodels2-2.apkg")
imp = AnkiPackageImporter(dst, tmp)
imp.run()
after = dst.noteCount()
# as the model schemas differ, should have been imported as new model
assert after == before + 1
def test_csv(): def test_csv():
deck = getEmptyDeck() deck = getEmptyDeck()
file = unicode(os.path.join(testDir, "support/text-2fields.txt")) file = unicode(os.path.join(testDir, "support/text-2fields.txt"))