From 37fc450b8b380d3b15f2d9ca0abfa3cda9da4f29 Mon Sep 17 00:00:00 2001 From: "J. Cliff Dyer" Date: Thu, 22 Sep 2016 11:07:35 -0400 Subject: [PATCH] Add version to VisibleBlocks model. TNL-5600 --- lms/djangoapps/grades/models.py | 16 ++++++++++------ lms/djangoapps/grades/tests/test_models.py | 13 ++++++++----- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/lms/djangoapps/grades/models.py b/lms/djangoapps/grades/models.py index dfb415e412..eabf9037ed 100644 --- a/lms/djangoapps/grades/models.py +++ b/lms/djangoapps/grades/models.py @@ -23,6 +23,8 @@ from xmodule_django.models import CourseKeyField, UsageKeyField log = logging.getLogger(__name__) +BLOCK_RECORD_LIST_VERSION = 1 + # Used to serialize information about a block at the time it was used in # grade calculation. BlockRecord = namedtuple('BlockRecord', ['locator', 'weight', 'max_score']) @@ -33,12 +35,13 @@ class BlockRecordList(tuple): An immutable ordered list of BlockRecord objects. """ - def __new__(cls, blocks, course_key): # pylint: disable=unused-argument + def __new__(cls, blocks, course_key, version=None): # pylint: disable=unused-argument return super(BlockRecordList, cls).__new__(cls, blocks) - def __init__(self, blocks, course_key): + def __init__(self, blocks, course_key, version=None): super(BlockRecordList, self).__init__(blocks) self.course_key = course_key + self.version = version or BLOCK_RECORD_LIST_VERSION def __eq__(self, other): assert isinstance(other, BlockRecordList) @@ -73,8 +76,9 @@ class BlockRecordList(tuple): for block_dict in list_of_block_dicts: block_dict['locator'] = unicode(block_dict['locator']) # BlockUsageLocator is not json-serializable data = { - 'course_key': unicode(self.course_key), - 'blocks': list_of_block_dicts, + u'blocks': list_of_block_dicts, + u'course_key': unicode(self.course_key), + u'version': self.version, } return json.dumps( data, @@ -98,7 +102,7 @@ class BlockRecordList(tuple): ) for block in block_dicts ) - return cls(record_generator, course_key) + return cls(record_generator, course_key, version=data['version']) @classmethod def from_list(cls, blocks, course_key): @@ -121,7 +125,7 @@ class VisibleBlocksQuerySet(models.QuerySet): """ model, _ = self.get_or_create( hashed=blocks.hash_value, - defaults={'blocks_json': blocks.json_value, 'course_id': blocks.course_key}, + defaults={u'blocks_json': blocks.json_value, u'course_id': blocks.course_key}, ) return model diff --git a/lms/djangoapps/grades/tests/test_models.py b/lms/djangoapps/grades/tests/test_models.py index b4dc2dc77e..e3dd0a24a7 100644 --- a/lms/djangoapps/grades/tests/test_models.py +++ b/lms/djangoapps/grades/tests/test_models.py @@ -14,6 +14,7 @@ from opaque_keys.edx.locator import CourseLocator, BlockUsageLocator from lms.djangoapps.grades.models import ( BlockRecord, BlockRecordList, + BLOCK_RECORD_LIST_VERSION, PersistentSubsectionGrade, VisibleBlocks ) @@ -32,7 +33,10 @@ class BlockRecordListTestCase(TestCase): ) def test_empty_block_record_set(self): - empty_json = '{0}"blocks":[],"course_key":"{1}"{2}'.format('{', unicode(self.course_key), '}') + empty_json = u'{"blocks":[],"course_key":"%s","version":%s}' % ( + unicode(self.course_key), + BLOCK_RECORD_LIST_VERSION, + ) brs = BlockRecordList((), self.course_key) self.assertFalse(brs) @@ -100,7 +104,7 @@ class BlockRecordTest(GradesModelTestCase): @ddt.unpack def test_serialization(self, weight, max_score, block_key): """ - Tests serialization of a BlockRecord using the to_dict() method. + Tests serialization of a BlockRecord using the _asdict() method. """ record = BlockRecord(block_key, weight, max_score) expected = OrderedDict([ @@ -130,10 +134,9 @@ class VisibleBlocksTest(GradesModelTestCase): for block_dict in list_of_block_dicts: block_dict['locator'] = unicode(block_dict['locator']) # BlockUsageLocator is not json-serializable expected_data = { + 'blocks': [{'locator': unicode(self.record_a.locator), 'max_score': 10, 'weight': 1}], 'course_key': unicode(self.record_a.locator.course_key), - 'blocks': [ - {'locator': unicode(self.record_a.locator), 'max_score': 10, 'weight': 1}, - ], + 'version': BLOCK_RECORD_LIST_VERSION, } expected_json = json.dumps(expected_data, separators=(',', ':'), sort_keys=True) expected_hash = b64encode(sha1(expected_json).digest())