Example #1
0
def load_graph_data(request, path, graph_data_type):
    if not path.strip("/"):  # root node!
        nodes = [backend.get_root_node()]
        # related_nodes = []
    else:
        slot_path = path.rsplit(".", 1)[0]
        slot = assert_node_for_path(slot_path)
        if graph_data_type == "withSpam":
            # This means display ALL nodes
            nodes = backend.get_ordered_children_for(slot)
        else:  # if graph_data_type == 'full':
            nodes = (
                backend.Node.objects.filter(parents=slot)
                .annotate(spam_count=Count("spam_flags", distinct=True))
                .filter(spam_count__lt=2)
                .filter(votes__isnull=False)
            )

        current_node = backend.get_node_for_path(path)
        nodes = list(nodes)
        if current_node not in nodes:
            nodes.append(current_node)

    graph_data_children = map(create_graph_data_node_for_structure_node, nodes)
    data = {"graphDataChildren": graph_data_children, "graphDataRelated": []}
    return json_response({"loadGraphDataResponse": data})
Example #2
0
def fork_node_and_add_slot(path, user, wiki_text):
    source_node = assert_node_for_path(path)
    authors = list(source_node.text.authors.all()) + [user]
    title = source_node.title
    # create fork
    fork = create_structureNode(title,
                                source_node.text.text,
                                authors)
    parent_slot_path = path.rsplit('.', 1)[0]
    parent_slot = get_node_for_path(parent_slot_path)
    parent_slot.append_child(fork)
    fork_path = parent_slot_path + '.' + str(fork.get_index(parent_slot))
    short_titles = set()
    for slot in get_ordered_children_for(source_node):
        fork.append_child(slot)
        short_titles.add(slot.title)
    # create new slot plus node
    schema = parse(wiki_text, 'foo')
    short_title = turn_into_valid_short_title(schema['title'], short_titles)
    new_slot = create_slot(short_title)
    fork.append_child(new_slot)
    node = create_structure_from_structure_node_schema(schema, new_slot, user)
    arg_title = "Abschnitt über '{0}' fehlt.".format(schema['title'])
    source_node.add_derivate(fork, 'con', arg_title, authors=[user])
    # auto follow
    follow_node(fork, user.id)
    follow_node(node, user.id)
    return fork_path
Example #3
0
def load_text(request, path):
    prefix, path_type = parse_suffix(path)
    try:
        node = backend.get_node_for_path(prefix)
    except backend.IllegalPath:
        return json_error_response("IllegalPath", "Illegal Path: " + path)

    paragraphs = [
        {
            "wikiText": "=" + node.title + "=\n" + node.text.text,
            "path": path,
            "isFollowing": node.votes.filter(user=request.user.id).count() > 0,
            "authorGroup": [create_user_info(a) for a in node.text.authors.all()],
        }
    ]
    for slot in backend.get_ordered_children_for(node):
        favorite = backend.get_favorite_if_slot(slot)
        paragraphs.append(
            {
                "wikiText": build_text(favorite, depth=2),
                "path": path + "/" + slot.title + "." + str(favorite.get_index(slot)),
                "isFollowing": favorite.votes.filter(user=request.user.id).count() > 0,
                "authorGroup": [create_user_info(a) for a in favorite.text.authors.all()],
            }
        )
    return json_response(
        {
            "loadTextResponse": {
                "paragraphs": paragraphs,
                "isFollowing": node.votes.filter(user=request.user.id).count() > 0,
            }
        }
    )
Example #4
0
def load_index(request, path):
    try:
        node = backend.get_node_for_path(path)
    except backend.IllegalPath:
        return json_error_response("NonExistingNode", "Illegal Path: " + path)
    slot_list = backend.get_ordered_children_for(node)
    index_nodes = [create_index_node_for_slot(slot) for slot in slot_list]
    return json_response({"loadIndexResponse": index_nodes})
Example #5
0
def create_paragraph_list_for_node(node, path, depth=1):
    paragraphs = [create_paragraph_for_node(node, path, depth=depth)]
    for slot in backend.get_ordered_children_for(node):
        favorite = slot.favorite
        slot_path = path + "/" + slot.title
        fav_path = get_good_path_for_structure_node(favorite, slot, slot_path)
        paragraphs += create_paragraph_list_for_node(favorite,
                                                     fav_path,
                                                     depth=depth + 1)
    return paragraphs
Example #6
0
def create_paragraph_list_for_node(node, path, depth=1):
    paragraphs = [create_paragraph_for_node(node, path, depth=depth)]
    for slot in backend.get_ordered_children_for(node):
        favorite = slot.favorite
        slot_path = path + "/" + slot.title
        fav_path = get_good_path_for_structure_node(favorite, slot, slot_path)
        paragraphs += create_paragraph_list_for_node(favorite,
                                                     fav_path,
                                                     depth=depth + 1)
    return paragraphs
Example #7
0
def get_index_nodes_for_path(path):
    path = path.strip().strip('/')
    try:  # to get from cache
        index_cache = backend.IndexCache.objects.get(path=path)
        index_nodes = json.loads(index_cache.index_nodes)
    except backend.IndexCache.DoesNotExist:
        node = assert_node_for_path(path)
        slot_list = backend.get_ordered_children_for(node)
        index_nodes = [create_index_node_for_slot(slot) for slot in slot_list]
        # write to cache
        index_cache = json.dumps(index_nodes)
        backend.IndexCache.objects.create(path=path, index_nodes=index_cache)
    return index_nodes
Example #8
0
def get_index_nodes_for_path(path):
    path = path.strip().strip('/')
    try:  # to get from cache
        index_cache = backend.IndexCache.objects.get(path=path)
        index_nodes = json.loads(index_cache.index_nodes)
    except backend.IndexCache.DoesNotExist:
        node = assert_node_for_path(path)
        slot_list = backend.get_ordered_children_for(node)
        index_nodes = [create_index_node_for_slot(slot) for slot in slot_list]
        # write to cache
        index_cache = json.dumps(index_nodes)
        backend.IndexCache.objects.create(path=path, index_nodes=index_cache)
    return index_nodes
Example #9
0
def load_graph_data(request, path, graph_data_type):
    if not path.strip('/'):  # root node!
        nodes = [backend.get_root_node()]
        related_nodes = []
    else:
        slot_path = path.rsplit('.', 1)[0]
        slot = assert_node_for_path(slot_path)
        nodes = backend.get_ordered_children_for(slot)
        sources = Q(derivates__in=nodes)
        derivates = Q(sources__in=nodes)
        related_nodes = backend.Node.objects.filter(sources | derivates). \
            exclude(id__in=[n.id for n in nodes]).distinct().all()
    graph_data_children = map(create_graph_data_node_for_structure_node, nodes)
    graph_data_related = map(create_graph_data_node_for_structure_node,
                             related_nodes)
    data = {'graphDataChildren': graph_data_children,
            'graphDataRelated': graph_data_related}
    return json_response({'loadGraphDataResponse': data})
Example #10
0
def load_graph_data(request, path, graph_data_type):
    if not path.strip("/"):  # root node!
        nodes = [backend.get_root_node()]
        related_nodes = []
    else:
        slot_path = path.rsplit(".", 1)[0]
        try:
            slot = backend.get_node_for_path(slot_path)
        except backend.IllegalPath:
            return json_error_response("NonExistingNode", "Could not find slot: " + slot_path + " for node " + path)
        nodes = backend.get_ordered_children_for(slot)
        sources = Q(derivates__in=nodes)
        derivates = Q(sources__in=nodes)
        related_nodes = (
            backend.Node.objects.filter(sources | derivates).exclude(id__in=[n.id for n in nodes]).distinct().all()
        )

    graph_data_children = [create_graph_data_node_for_structure_node(n) for n in nodes]
    graph_data_related = [create_graph_data_node_for_structure_node(n) for n in related_nodes]
    data = {"graphDataChildren": graph_data_children, "graphDataRelated": graph_data_related}
    return json_response({"loadGraphDataResponse": data})
Example #11
0
def load_graph_data(request, path, graph_data_type):
    if not path.strip('/'):  # root node!
        nodes = [backend.get_root_node()]
        # related_nodes = []
    else:
        slot_path = path.rsplit('.', 1)[0]
        slot = assert_node_for_path(slot_path)
        if graph_data_type == "withSpam":
            # This means display ALL nodes
            nodes = backend.get_ordered_children_for(slot)
        else:  # if graph_data_type == 'full':
            nodes = backend.Node.objects.filter(parents=slot)\
                .annotate(spam_count=Count('spam_flags', distinct=True))\
                .filter(spam_count__lt=2)\
                .filter(votes__isnull=False)

        current_node = backend.get_node_for_path(path)
        nodes = list(nodes)
        if current_node not in nodes:
            nodes.append(current_node)

    graph_data_children = map(create_graph_data_node_for_structure_node, nodes)
    data = {'graphDataChildren': graph_data_children, 'graphDataRelated': []}
    return json_response({'loadGraphDataResponse': data})