Пример #1
0
def get_prerequisites(request):
    if request.method == 'POST':
        data = json.loads(request.body)
        nodes = ContentNode.objects.prefetch_related('prerequisite').filter(
            pk__in=data['nodes'])

        prerequisite_mapping = {}
        postrequisite_mapping = {}
        prerequisite_tree_nodes = []

        for n in nodes:
            prereqs, prereqmapping = n.get_prerequisites()
            if data.get('get_postrequisites'):
                postreqs, postreqmapping = n.get_postrequisites()
                postrequisite_mapping.update(postreqmapping)
                prerequisite_mapping.update(prereqmapping)
                prerequisite_tree_nodes += prereqs + postreqs + [n]
            else:
                prerequisite_mapping.update({n.pk: prereqmapping})
                prerequisite_tree_nodes += prereqs + [n]

        return HttpResponse(
            json.dumps({
                "prerequisite_mapping":
                prerequisite_mapping,
                "postrequisite_mapping":
                postrequisite_mapping,
                "prerequisite_tree_nodes":
                JSONRenderer().render(
                    SimplifiedContentNodeSerializer(prerequisite_tree_nodes,
                                                    many=True).data),
            }))
Пример #2
0
def get_prerequisites(request, get_prerequisites, ids):
    nodes = ContentNode.objects.prefetch_related('prerequisite').filter(
        pk__in=ids.split(","))

    prerequisite_mapping = {}
    postrequisite_mapping = {}
    prerequisite_tree_nodes = []

    for n in nodes:
        prereqs, prereqmapping = n.get_prerequisites()
        if get_prerequisites == "true":
            postreqs, postreqmapping = n.get_postrequisites()
            postrequisite_mapping.update(postreqmapping)
            prerequisite_mapping.update(prereqmapping)
            prerequisite_tree_nodes += prereqs + postreqs + [n]
        else:
            prerequisite_mapping.update({n.pk: prereqmapping})
            prerequisite_tree_nodes += prereqs + [n]

    return HttpResponse(
        json.dumps({
            "prerequisite_mapping":
            prerequisite_mapping,
            "postrequisite_mapping":
            postrequisite_mapping,
            "prerequisite_tree_nodes":
            JSONRenderer().render(
                SimplifiedContentNodeSerializer(prerequisite_tree_nodes,
                                                many=True).data),
        }))
Пример #3
0
def get_nodes_by_ids_simplified(request):
    if request.method == 'POST':
        nodes = ContentNode.objects.prefetch_related('children').filter(
            pk__in=json.loads(request.body))
        return HttpResponse(JSONRenderer().render(
            SimplifiedContentNodeSerializer(nodes, many=True).data))
Пример #4
0
def get_node_diff(request):

    if request.method == 'POST':
        channel_id = json.loads(request.body)['channel_id']
        original = []  # Currently imported nodes
        changed = []  # Nodes from original node
        fields_to_check = [
            'title', 'description', 'license', 'license_description',
            'copyright_holder', 'author', 'extra_fields'
        ]
        assessment_fields_to_check = [
            'type', 'question', 'hints', 'answers', 'order', 'raw_data',
            'source_url', 'randomize'
        ]

        current_tree_id = Channel.objects.get(pk=channel_id).main_tree.tree_id
        nodes = ContentNode.objects.prefetch_related(
            'assessment_items').prefetch_related('files').prefetch_related(
                'tags')

        copied_nodes = nodes.filter(tree_id=current_tree_id).exclude(
            original_source_node_id=F('node_id'))
        channel_ids = copied_nodes.values_list(
            'original_channel_id',
            flat=True).exclude(original_channel_id=channel_id).distinct()
        tree_ids = Channel.objects.filter(pk__in=channel_ids).values_list(
            "main_tree__tree_id", flat=True)
        original_node_ids = copied_nodes.values_list('original_source_node_id',
                                                     flat=True).distinct()
        original_nodes = nodes.filter(tree_id__in=tree_ids,
                                      node_id__in=original_node_ids)

        # Use dictionary for faster lookup speed
        content_id_mapping = {n.content_id: n for n in original_nodes}

        for copied_node in copied_nodes:
            node = content_id_mapping.get(copied_node.content_id)

            if node:
                # Check lengths, metadata, tags, files, and assessment items
                node_changed = node.assessment_items.count() != copied_node.assessment_items.count() or \
                               node.files.count() != copied_node.files.count() or \
                               node.tags.count() != copied_node.tags.count() or \
                               any(filter(lambda f: getattr(node, f, None) != getattr(copied_node, f, None), fields_to_check)) or \
                               node.tags.exclude(tag_name__in=copied_node.tags.values_list('tag_name', flat=True)).exists() or \
                               node.files.exclude(checksum__in=copied_node.files.values_list('checksum', flat=True)).exists() or \
                               node.assessment_items.exclude(assessment_id__in=copied_node.assessment_items.values_list('assessment_id', flat=True)).exists()

                # Check individual assessment items
                if not node_changed and node.kind_id == content_kinds.EXERCISE:
                    for ai in node.assessment_items.all():
                        source_ai = copied_node.assessment_items.filter(
                            assessment_id=ai.assessment_id).first()
                        if source_ai:
                            node_changed = node_changed or any(
                                filter(
                                    lambda f: getattr(ai, f, None) != getattr(
                                        source_ai, f, None),
                                    assessment_fields_to_check))
                            if node_changed:
                                break

                if node_changed:
                    original.append(copied_node)
                    changed.append(node)

        serialized_original = JSONRenderer().render(
            SimplifiedContentNodeSerializer(original, many=True).data)
        serialized_changed = JSONRenderer().render(
            SimplifiedContentNodeSerializer(changed, many=True).data)

        return HttpResponse(
            json.dumps({
                "original": serialized_original,
                "changed": serialized_changed,
            }))
Пример #5
0
def get_nodes_by_ids_simplified(request, ids):
    nodes = ContentNode.objects.prefetch_related('children').filter(
        pk__in=ids.split(","))
    serializer = SimplifiedContentNodeSerializer(nodes, many=True)
    return Response(serializer.data)