예제 #1
0
def validate_data(topic_tree, node_cache, slug2id_map):

    # Validate related videos
    for exercise_nodes in node_cache['Exercise'].values():
        exercise = exercise_nodes[0]
        exercise_path = EXERCISE_FILEPATH_TEMPLATE % exercise["slug"]
        if not os.path.exists(exercise_path):
            sys.stderr.write("Could not find exercise HTML file: %s\n" %
                             exercise_path)
        for vid_slug in exercise.get("related_video_slugs", []):
            if vid_slug not in slug2id_map or slug2id_map[
                    vid_slug] not in node_cache["Video"]:
                sys.stderr.write(
                    "Could not find related video %s in node_cache (from exercise %s)\n"
                    % (vid_slug, exercise["slug"]))

    # Validate related exercises
    for video_nodes in node_cache["Video"].values():
        video = video_nodes[0]
        ex = video["related_exercise"]
        if ex and ex["slug"] not in node_cache["Exercise"]:
            sys.stderr.write(
                "Could not find related exercise %s in node_cache (from video %s)\n"
                % (ex["slug"], video["slug"]))

    # Validate all topics have leaves
    for topic_nodes in node_cache["Topic"].values():
        topic = topic_nodes[0]
        if not topic_tools.get_topic_by_path(
                topic["path"], root_node=topic_tree).get("children"):
            sys.stderr.write("Could not find any children for topic %s\n" %
                             (topic["path"]))
예제 #2
0
    def scrub_knowledge_map(knowledge_map, node_cache):
        """
        Some topics in the knowledge map, we don't keep in our topic tree / node cache.
        Eliminate them from the knowledge map here.
        """
        for slug in knowledge_map["topics"].keys():
            nodecache_node = node_cache["Topic"].get(slug, [{}])[0]
            topictree_node = topic_tools.get_topic_by_path(
                nodecache_node.get("path"), root_node=topic_tree)

            if not nodecache_node or not topictree_node:
                logging.warn("Removing unrecognized knowledge_map topic '%s'" %
                             slug)
            elif not topictree_node.get("children"):
                logging.warn(
                    "Removing knowledge_map topic '%s' with no children." %
                    slug)
            elif not "Exercise" in topictree_node.get("contains"):
                logging.warn(
                    "Removing knowledge_map topic '%s' with no exercises." %
                    slug)
            else:
                continue

            del knowledge_map["topics"][slug]
            topictree_node["in_knowledge_map"] = False
예제 #3
0
def get_topic_tree_by_kinds(request, topic_path, kinds_to_query=None):
    """Given a root path, returns all topic nodes that contain the requested kind(s).
    Topic nodes without those kinds are removed.
    """
    def convert_topic_tree_for_dynatree(node, kinds_to_query):
        """Converts topic tree from standard dictionary nodes
        to dictionary nodes usable by the dynatree app"""

        if node["kind"] != "Topic":
            # Should never happen, but only run this function for topic nodes.
            return None

        elif not set(kinds_to_query).intersection(set(node["contains"])):
            # Eliminate topics that don't contain the requested kinds
            return None

        topic_children = []
        for child_node in node["children"]:
            child_dict = convert_topic_tree_for_dynatree(
                child_node, kinds_to_query)
            if child_dict:
                # Only keep children that themselves have the requsted kind
                topic_children.append(child_dict)

        return {
            "title": _(node["title"]),
            "tooltip": re.sub(r'<[^>]*?>', '',
                              _(node.get("description")) or ""),
            "isFolder": True,
            "key": node["path"],
            "children": topic_children,
            "expand": False,  # top level
        }

    kinds_to_query = kinds_to_query or request.GET.get("kinds",
                                                       "Exercise").split(",")
    topic_node = get_topic_by_path(topic_path)
    if not topic_node:
        raise Http404

    return JsonResponse(
        convert_topic_tree_for_dynatree(topic_node, kinds_to_query))
예제 #4
0
    def scrub_knowledge_map(knowledge_map, node_cache):
        """
        Some topics in the knowledge map, we don't keep in our topic tree / node cache.
        Eliminate them from the knowledge map here.
        """
        for slug in knowledge_map["topics"].keys():
            nodecache_node = node_cache["Topic"].get(slug, [{}])[0]
            topictree_node = topic_tools.get_topic_by_path(nodecache_node.get("path"), root_node=topic_tree)

            if not nodecache_node or not topictree_node:
                logging.warn("Removing unrecognized knowledge_map topic '%s'" % slug)
            elif not topictree_node.get("children"):
                logging.warn("Removing knowledge_map topic '%s' with no children." % slug)
            elif not "Exercise" in topictree_node.get("contains"):
                logging.warn("Removing knowledge_map topic '%s' with no exercises." % slug)
            else:
                continue

            del knowledge_map["topics"][slug]
            topictree_node["in_knowledge_map"] = False
예제 #5
0
def get_topic_tree_by_kinds(request, topic_path, kinds_to_query=None):
    """Given a root path, returns all topic nodes that contain the requested kind(s).
    Topic nodes without those kinds are removed.
    """

    def convert_topic_tree_for_dynatree(node, kinds_to_query):
        """Converts topic tree from standard dictionary nodes
        to dictionary nodes usable by the dynatree app"""

        if node["kind"] != "Topic":
            # Should never happen, but only run this function for topic nodes.
            return None

        elif not set(kinds_to_query).intersection(set(node["contains"])):
            # Eliminate topics that don't contain the requested kinds
            return None

        topic_children = []
        for child_node in node["children"]:
            child_dict = convert_topic_tree_for_dynatree(child_node, kinds_to_query)
            if child_dict:
                # Only keep children that themselves have the requsted kind
                topic_children.append(child_dict)

        return {
            "title": _(node["title"]),
            "tooltip": re.sub(r'<[^>]*?>', '', _(node.get("description")) or ""),
            "isFolder": True,
            "key": node["path"],
            "children": topic_children,
            "expand": False,  # top level
        }

    kinds_to_query = kinds_to_query or request.GET.get("kinds", "Exercise").split(",")
    topic_node = get_topic_by_path(topic_path)
    if not topic_node:
        raise Http404

    return JsonResponse(convert_topic_tree_for_dynatree(topic_node, kinds_to_query));
예제 #6
0
def validate_data(topic_tree, node_cache, slug2id_map):

    # Validate related videos
    for exercise_nodes in node_cache['Exercise'].values():
        exercise = exercise_nodes[0]
        exercise_path = EXERCISE_FILEPATH_TEMPLATE % exercise["slug"]
        if not os.path.exists(exercise_path):
            sys.stderr.write("Could not find exercise HTML file: %s\n" % exercise_path)
        for vid_slug in exercise.get("related_video_slugs", []):
            if vid_slug not in slug2id_map or slug2id_map[vid_slug] not in node_cache["Video"]:
                sys.stderr.write("Could not find related video %s in node_cache (from exercise %s)\n" % (vid_slug, exercise["slug"]))

    # Validate related exercises
    for video_nodes in node_cache["Video"].values():
        video = video_nodes[0]
        ex = video["related_exercise"]
        if ex and ex["slug"] not in node_cache["Exercise"]:
            sys.stderr.write("Could not find related exercise %s in node_cache (from video %s)\n" % (ex["slug"], video["slug"]))

    # Validate all topics have leaves
    for topic_nodes in node_cache["Topic"].values():
        topic = topic_nodes[0]
        if not topic_tools.get_topic_by_path(topic["path"], root_node=topic_tree).get("children"):
            sys.stderr.write("Could not find any children for topic %s\n" % (topic["path"]))