dupe fact on add to avoid session issues

This commit is contained in:
Damien Elmes 2008-12-07 11:38:35 +09:00
parent 02a124b66d
commit 8c9f883e68
3 changed files with 17 additions and 11 deletions

View file

@ -783,6 +783,7 @@ and due < :now""", now=time.time())
"Add a fact to the deck. Return list of new cards."
if not fact.model:
fact.model = self.currentModel
fact = self.cloneFact(fact)
# validate
fact.assertValid()
fact.assertUnique(self.s)
@ -805,7 +806,7 @@ and due < :now""", now=time.time())
# keep track of last used tags for convenience
self.lastTags = fact.tags
self.flushMod()
return cards
return fact
def availableCardModels(self, fact, checkActive=True):
"List of active card models that aren't empty for FACT."
@ -899,11 +900,7 @@ where facts.id not in (select factId from cards)""")
cms = self.availableCardModels(oldFact)
if not cms:
return []
# dupe fact
fact = self.newFact(oldFact.model)
for field in fact.fields:
fact[field.name] = oldFact[field.name]
fact.tags = oldFact.tags
fact = self.cloneFact(oldFact)
# proceed
cards = []
for cardModel in cms:
@ -911,6 +908,15 @@ where facts.id not in (select factId from cards)""")
cards.append(card)
return cards
def cloneFact(self, oldFact):
"Copy fact into new session."
model = self.s.query(Model).get(oldFact.model.id)
fact = self.newFact(model)
for field in fact.fields:
fact[field.name] = oldFact[field.name]
fact.tags = oldFact.tags
return fact
# Cards
##########################################################################

View file

@ -85,14 +85,14 @@ def test_factAddDelete():
assert e.data['type'] == 'fieldEmpty'
# add a fact
f['Front'] = u"one"; f['Back'] = u"two"
deck.addFact(f)
f = deck.addFact(f)
assert len(f.cards) == 1
deck.rollback()
# try with two cards
f = deck.newFact()
f['Front'] = u"one"; f['Back'] = u"two"
f.model.cardModels[1].active = True
deck.addFact(f)
f = deck.addFact(f)
assert len(f.cards) == 2
# ensure correct order
c0 = [c for c in f.cards if c.ordinal == 0][0]
@ -101,7 +101,7 @@ def test_factAddDelete():
f2 = deck.newFact()
f2['Front'] = u"one"; f2['Back'] = u"three"
try:
deck.addFact(f2)
f2 = deck.addFact(f2)
except Exception, e:
pass
assert e.data['type'] == 'fieldNotUnique'

View file

@ -196,8 +196,8 @@ def test_localsync_threeway():
# add a new question
f = deck1.newFact()
f['Front'] = u"a"; f['Back'] = u"b"
cards = deck1.addFact(f)
card = cards[0]
f = deck1.addFact(f)
card = f.cards[0]
client.sync()
assert deck1.cardCount == 6
assert deck2.cardCount == 6