From fb9760244262073384c90ec0de29e737455c5ce8 Mon Sep 17 00:00:00 2001 From: Will Daly Date: Mon, 1 Apr 2013 12:08:53 -0400 Subject: [PATCH] Save randomization seed if it is not already set, so that the same problem loads when the user checks/saves/resets --- common/lib/xmodule/xmodule/capa_module.py | 10 ++++++++++ .../lib/xmodule/xmodule/tests/test_capa_module.py | 13 +++++-------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/common/lib/xmodule/xmodule/capa_module.py b/common/lib/xmodule/xmodule/capa_module.py index da8b5b4f96..0869c3e484 100644 --- a/common/lib/xmodule/xmodule/capa_module.py +++ b/common/lib/xmodule/xmodule/capa_module.py @@ -150,6 +150,16 @@ class CapaModule(CapaFields, XModule): # TODO (vshnayder): move as much as possible of this work and error # checking to descriptor load time self.lcp = self.new_lcp(self.get_state_for_lcp()) + + # At this point, we need to persist the randomization seed + # so that when the problem is re-loaded (to check/view/save) + # it stays the same. + # However, we do not want to write to the database + # every time the module is loaded. + # So we set the seed ONLY when there is not one set already + if self.seed is None: + self.seed = self.lcp.seed + except Exception as err: msg = 'cannot create LoncapaProblem {loc}: {err}'.format( loc=self.location.url(), err=err) diff --git a/common/lib/xmodule/xmodule/tests/test_capa_module.py b/common/lib/xmodule/xmodule/tests/test_capa_module.py index 60e51fd725..c9053be2ad 100644 --- a/common/lib/xmodule/xmodule/tests/test_capa_module.py +++ b/common/lib/xmodule/xmodule/tests/test_capa_module.py @@ -868,10 +868,8 @@ class CapaModuleTest(unittest.TestCase): module = CapaFactory.create(rerandomize=rerandomize) # Get the seed - # This isn't stored in the XModule until - # we check/save/reset the problem, so we - # access the lcp seed directly - seed = module.lcp.seed + # By this point, the module should have persisted the seed + seed = module.seed self.assertTrue(seed is not None) # If we're not rerandomizing, the seed is always set @@ -931,10 +929,9 @@ class CapaModuleTest(unittest.TestCase): module = CapaFactory.create(rerandomize=rerandomize) # Get the seed - # This isn't stored in the XModule until - # we check/save/reset the problem, so we - # access the lcp seed directly - seed = module.lcp.seed + # By this point, the module should have persisted the seed + seed = module.seed + self.assertTrue(seed is not None) # We do NOT want the seed to reset if rerandomize # is set to 'never' -- it should still be 1