Exemplo n.º 1
0
def batch_add_tags(request):
    # check existing tag and subtract them from bulk_create
    insert_list = []
    tag_names = request.POST.getlist('tags[]')
    existing_tags = models.ContentTag.objects.filter(tag_name__in=tag_names)
    existing_tag_names = existing_tags.values_list('tag_name', flat=True)
    new_tag_names = set(tag_names) - set(existing_tag_names)
    for name in new_tag_names:
        insert_list.append(models.ContentTag(tag_name=name))
    new_tags = models.ContentTag.objects.bulk_create(insert_list)

    # bulk add all tags to selected nodes
    all_tags = set(existing_tags).union(set(new_tags))
    ThroughModel = models.Node.tags.through
    bulk_list = []
    node_pks = request.POST.getlist('nodes[]')
    for tag in all_tags:
        for pk in node_pks:
            bulk_list.append(ThroughModel(node_id=pk, contenttag_id=tag.pk))
    ThroughModel.objects.bulk_create(bulk_list)

    return HttpResponse("Tags are successfully saved.", status=200)
Exemplo n.º 2
0
def node(data, parent=None):
    new_node = None
    # Create topics
    if data['kind_id'] == "topic":
        new_node = cc.ContentNode(
            kind=topic(),
            parent=parent,
            title=data['title'],
            node_id=data['node_id'],
            content_id=data.get('content_id') or data['node_id'],
            sort_order=data.get('sort_order', 1),
        )
        new_node.save()

        for child in data['children']:
            node(child, parent=new_node)

    # Create videos
    elif data['kind_id'] == "video":
        new_node = cc.ContentNode(
            kind=video(),
            parent=parent,
            title=data['title'],
            node_id=data['node_id'],
            license=license_wtfpl(),
            content_id=data.get('content_id') or data['node_id'],
            sort_order=data.get('sort_order', 1),
        )
        new_node.save()
        video_file = fileobj_video(contents="Video File")
        video_file.contentnode = new_node
        video_file.preset_id = format_presets.VIDEO_HIGH_RES
        video_file.save()

    # Create exercises
    elif data['kind_id'] == "exercise":
        extra_fields = {
            'mastery_model': data['mastery_model'],
            'randomize': True,
            'm': data.get('m') or 0,
            'n': data.get('n') or 0
        }
        new_node = cc.ContentNode(
            kind=exercise(),
            parent=parent,
            title=data['title'],
            node_id=data['node_id'],
            license=license_wtfpl(),
            extra_fields=extra_fields,
            content_id=data.get('content_id') or data['node_id'],
            sort_order=data.get('sort_order', 1),
        )
        new_node.save()
        for assessment_item in data['assessment_items']:
            ai = cc.AssessmentItem(
                contentnode=new_node,
                assessment_id=assessment_item['assessment_id'],
                question=assessment_item['question'],
                type=assessment_item['type'],
                answers=json.dumps(assessment_item['answers']),
                hints=json.dumps(assessment_item.get('hints') or [])
            )
            ai.save()

    if data.get('tags'):
        for tag in data['tags']:
            t = cc.ContentTag(tag_name=tag['tag_name'])
            t.save()
            new_node.tags.add(t)
            new_node.save()

    return new_node