def test_resource_size__topic(self): topic_tree_node = tree() nested_topic = topic_tree_node.get_descendants().filter( kind=content_kinds.TOPIC).first() self.create_coach_node(nested_topic) query = Metadata(topic_tree_node) results = query.annotate(**{RESOURCE_SIZE: ResourceSize()}) serialized = ContentNodeSerializer(topic_tree_node).data self.assertEqual( serialized.get('metadata').get('resource_size'), results.get(serialized.get('id')).get(RESOURCE_SIZE)) self.assertEqual(0, results.get(serialized.get('id')).get(RESOURCE_SIZE))
class ContentNodeSerializer(SimplifiedContentNodeSerializer, ContentNodeFieldMixin): ancestors = serializers.SerializerMethodField('get_node_ancestors') valid = serializers.SerializerMethodField('check_valid') associated_presets = serializers.SerializerMethodField( 'retrieve_associated_presets') original_channel = serializers.SerializerMethodField( 'retrieve_original_channel') thumbnail_src = serializers.SerializerMethodField('retrieve_thumbail_src') tags = TagSerializer(many=True, read_only=False) metadata_query = Metadata( total_count=DescendantCount(), resource_count=ResourceCount(include_self=True), assessment_count=AssessmentCount(include_self=True), max_sort_order=SortOrderMax(include_self=True), resource_size=ResourceSize(include_self=True), has_changed_descendant=HasChanged(include_self=True), coach_count=CoachCount(include_self=True), ) def retrieve_associated_presets(self, node): return list(node.get_associated_presets()) def check_valid(self, node): isoriginal = node.node_id == node.original_source_node_id if node.kind_id == content_kinds.TOPIC: return True elif isoriginal and not node.license: return False elif isoriginal and node.license.copyright_holder_required and not node.copyright_holder: return False elif isoriginal and node.license.is_custom and not node.license_description: return False elif node.kind_id == content_kinds.EXERCISE: for aitem in node.assessment_items.exclude( type=exercises.PERSEUS_QUESTION): answers = json.loads(aitem.answers) correct_answers = filter(lambda a: a['correct'], answers) if aitem.question == "" or len(answers) == 0 or len(correct_answers) == 0 or \ any(filter(lambda a: a['answer'] == "", answers)) or \ (aitem.type == exercises.SINGLE_SELECTION and len(correct_answers) > 1) or \ any(filter(lambda h: h['hint'] == "", json.loads(aitem.hints))): return False return True else: return node.files.filter(preset__supplementary=False).exists() def retrieve_metadata(self, node): data = self.metadata_query.get(node.pk) if node.kind_id == content_kinds.EXERCISE: data.update(resource_count=data.get('assessment_count')) if node.kind_id != content_kinds.TOPIC: return data if not node.parent_id: # Add extra data to root node data.update( self.get_creators(node.get_descendants(include_self=True))) return data def retrieve_original_channel(self, node): channel_id = node.original_channel_id channel = channel_id and Channel.objects.get(pk=channel_id) return { "id": channel.pk, "name": channel.name, "thumbnail_url": channel.get_thumbnail(), } if (channel and not channel.deleted) else None class Meta: list_serializer_class = CustomListSerializer model = ContentNode fields = ('title', 'changed', 'id', 'description', 'sort_order', 'author', 'copyright_holder', 'license', 'language', 'license_description', 'assessment_items', 'slideshow_slides', 'files', 'ancestors', 'modified', 'original_channel', 'kind', 'parent', 'children', 'published', 'associated_presets', 'valid', 'metadata', 'original_source_node_id', 'tags', 'extra_fields', 'prerequisite', 'is_prerequisite_of', 'node_id', 'tree_id', 'publishing', 'freeze_authoring_data', 'role_visibility', 'provider', 'aggregator', 'thumbnail_src')
def test_multiple(self): topic_tree_node1 = tree() topic_tree_node2 = tree() topic_tree1_topics = topic_tree_node1.get_descendants().filter( kind=content_kinds.TOPIC) video_node = self.create_coach_node(topic_tree1_topics.first()) exercise_node = ContentNode.objects.get( tree_id=topic_tree_node2.tree_id, node_id='00000000000000000000000000000005') self.set_tree_changed(topic_tree_node1, False) self.set_tree_changed(topic_tree_node2, False) topic_tree1_topics.last().delete() nodes = ContentNode.objects.filter(pk__in=[ topic_tree_node1.pk, topic_tree_node2.pk, video_node.pk, exercise_node.pk, ]) query = Metadata( nodes, **{ DESCENDANT_COUNT: DescendantCount(), RESOURCE_COUNT: ResourceCount(), ASSESSMENT_COUNT: AssessmentCount(), RESOURCE_SIZE: ResourceSize(), COACH_COUNT: CoachCount(), HAS_CHANGED_DESCENDANT: HasChanged(include_self=True), MAX_SORT_ORDER: SortOrderMax(), }) topic_tree1_results = query.get(topic_tree_node1.pk) topic_tree2_results = query.get(topic_tree_node2.pk) video_node_results = query.get(video_node.pk) exercise_node_results = query.get(exercise_node.pk) self.assertIsNotNone(topic_tree1_results) self.assertEqual(6, topic_tree1_results.get(DESCENDANT_COUNT)) self.assertEqual(5, topic_tree1_results.get(RESOURCE_COUNT)) self.assertEqual(0, topic_tree1_results.get(ASSESSMENT_COUNT)) self.assertEqual(0, topic_tree1_results.get(RESOURCE_SIZE)) self.assertEqual(1, topic_tree1_results.get(COACH_COUNT)) self.assertTrue(topic_tree1_results.get(HAS_CHANGED_DESCENDANT)) self.assertEqual(1, topic_tree1_results.get(MAX_SORT_ORDER)) self.assertIsNotNone(topic_tree2_results) self.assertEqual(7, topic_tree2_results.get(DESCENDANT_COUNT)) self.assertEqual(5, topic_tree2_results.get(RESOURCE_COUNT)) self.assertEqual(0, topic_tree2_results.get(ASSESSMENT_COUNT)) self.assertEqual(0, topic_tree2_results.get(RESOURCE_SIZE)) self.assertEqual(0, topic_tree2_results.get(COACH_COUNT)) self.assertFalse(topic_tree2_results.get(HAS_CHANGED_DESCENDANT)) self.assertEqual(2, topic_tree2_results.get(MAX_SORT_ORDER)) self.assertIsNotNone(video_node_results) self.assertEqual(1, video_node_results.get(DESCENDANT_COUNT)) self.assertEqual(1, video_node_results.get(RESOURCE_COUNT)) self.assertEqual(0, video_node_results.get(ASSESSMENT_COUNT)) self.assertEqual( video_node.files.aggregate(size=Sum('file_size')).get('size'), video_node_results.get(RESOURCE_SIZE)) self.assertEqual(1, video_node_results.get(COACH_COUNT)) self.assertFalse(video_node_results.get(HAS_CHANGED_DESCENDANT)) self.assertEqual(video_node.sort_order, video_node_results.get(MAX_SORT_ORDER)) self.assertIsNotNone(exercise_node_results) self.assertEqual(1, exercise_node_results.get(DESCENDANT_COUNT)) self.assertEqual(1, exercise_node_results.get(RESOURCE_COUNT)) self.assertEqual(3, exercise_node_results.get(ASSESSMENT_COUNT)) self.assertEqual(0, exercise_node_results.get(RESOURCE_SIZE)) self.assertEqual(0, exercise_node_results.get(COACH_COUNT)) self.assertFalse(exercise_node_results.get(HAS_CHANGED_DESCENDANT)) self.assertEqual(exercise_node.sort_order, exercise_node_results.get(MAX_SORT_ORDER))