示例#1
0
def _all_block_types(descriptors, aside_types):
    """
    Return a set of all block_types for the supplied `descriptors` and for
    the asides types in `aside_types` associated with those descriptors.
    """
    block_types = set()
    for descriptor in descriptors:
        block_types.add(BlockTypeKeyV1(descriptor.entry_point, descriptor.scope_ids.block_type))

    for aside_type in aside_types:
        block_types.add(BlockTypeKeyV1(XBlockAside.entry_point, aside_type))

    return block_types
示例#2
0
    def _all_block_types(self, descriptors):
        """
        Return a set of all block_types that are cached by this FieldDataCache.
        """
        block_types = set()
        for descriptor in descriptors:
            block_types.add(
                BlockTypeKeyV1(descriptor.entry_point,
                               descriptor.scope_ids.block_type))

        for aside_type in self.asides:
            block_types.add(BlockTypeKeyV1(XBlockAside.entry_point,
                                           aside_type))

        return block_types
示例#3
0
    def _cache_key_for_kvs_key(self, key):
        """
        Return the key used in this DjangoOrmFieldCache for the specified KeyValueStore key.

        Arguments:
            key (:class:`~DjangoKeyValueStore.Key`): The key representing the cached field
        """
        return BlockTypeKeyV1(key.block_family, key.block_scope_id), key.field_name
示例#4
0
 def _cache_key_from_kvs_key(self, key):
     """
     Return the key used in the FieldDataCache for the specified KeyValueStore key
     """
     if key.scope == Scope.user_state:
         return (key.scope, key.block_scope_id)
     elif key.scope == Scope.user_state_summary:
         return (key.scope, key.block_scope_id, key.field_name)
     elif key.scope == Scope.preferences:
         return (key.scope,
                 BlockTypeKeyV1(key.block_family,
                                key.block_scope_id), key.field_name)
     elif key.scope == Scope.user_info:
         return (key.scope, key.field_name)
示例#5
0
    def _create_object(self, kvs_key, value):
        """
        Create a new object to add to the cache (which should record
        the specified field ``value`` for the field identified by
        ``kvs_key``).

        Arguments:
            kvs_key (:class:`DjangoKeyValueStore.Key`): Which field to create an entry for
            value: The value to assign to the new field object
        """
        return XModuleStudentPrefsField(
            field_name=kvs_key.field_name,
            module_type=BlockTypeKeyV1(kvs_key.block_family, kvs_key.block_scope_id),
            student_id=kvs_key.user_id,
            value=value,
        )
示例#6
0
    def find_or_create(self, key):
        '''
        Find a model data object in this cache, or create it if it doesn't
        exist
        '''
        field_object = self.find(key)

        if field_object is not None:
            return field_object

        if key.scope == Scope.user_state:
            field_object, __ = StudentModule.objects.get_or_create(
                course_id=self.course_id,
                student_id=key.user_id,
                module_state_key=key.block_scope_id,
                defaults={
                    'state': json.dumps({}),
                    'module_type': key.block_scope_id.block_type,
                },
            )
        elif key.scope == Scope.user_state_summary:
            field_object, __ = XModuleUserStateSummaryField.objects.get_or_create(
                field_name=key.field_name, usage_id=key.block_scope_id)
        elif key.scope == Scope.preferences:
            field_object, __ = XModuleStudentPrefsField.objects.get_or_create(
                field_name=key.field_name,
                module_type=BlockTypeKeyV1(key.block_family,
                                           key.block_scope_id),
                student_id=key.user_id,
            )
        elif key.scope == Scope.user_info:
            field_object, __ = XModuleStudentInfoField.objects.get_or_create(
                field_name=key.field_name,
                student_id=key.user_id,
            )

        cache_key = self._cache_key_from_kvs_key(key)
        self.cache[cache_key] = field_object
        return field_object
示例#7
0
 def test_prevent_slash_in_family(self):
     with self.assertRaises(InvalidKeyError):
         BlockTypeKeyV1('foo:bar', 'baz')
示例#8
0
 def test_roundtrip_from_key(self, family, block_type):
     key = BlockTypeKeyV1(family, block_type)
     serialized = text_type(key)
     deserialized = BlockTypeKey.from_string(serialized)
     self.assertEqual(key, deserialized)
示例#9
0
 def test_deprecated_equality(self):
     self.assertEqual(BlockTypeKeyV1('xblock.v1', 'problem'),
                      BlockTypeKeyV1('xmodule.v1', 'problem'))
示例#10
0
 def test_deprecated_construction(self):
     block_type = BlockTypeKeyV1('xblock.v1', 'problem')
     serialized = text_type(block_type)
     self.assertEqual('problem', serialized)