def validate_data(topictree, node_cache, slug2id_map, knowledge_map): # Validate related videos for exercise_nodes in node_cache['Exercise'].values(): exercise = exercise_nodes[0] exercise_path = os.path.join(settings.PROJECT_PATH, "static", "js", "khan-exercises", "exercises", "%s.html" % 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=topictree).get("children"): sys.stderr.write("Could not find any children for topic %s\n" % (topic["path"])) # Validate all topics in knowledge map are in the node cache for slug in knowledge_map["topics"]: if slug not in node_cache["Topic"]: sys.stderr.write("Unknown topic in knowledge map: %s\n" % slug) topicdata_path = os.path.join(settings.PROJECT_PATH + "/static/data/", "topicdata", "%s.json" % slug) if not os.path.exists(topicdata_path): sys.stderr.write("Could not find topic data in topicdata directory: '%s'\n" % slug) # Validate all topics in node-cache are in (or out) of knowledge map, as requested. for topic_nodes in node_cache["Topic"].values(): topic = topic_nodes[0] if topic["in_knowledge_map"] and not topic["slug"] in knowledge_map["topics"]: sys.stderr.write("Topic '%-40s' not in knowledge map, but node_cache says it should be.\n" % topic["slug"]) elif not topic["in_knowledge_map"] and topic["slug"] in knowledge_map["topics"]: sys.stderr.write("Topic '%-40s' in knowledge map, but node_cache says it shouldn't be.\n" % topic["slug"]) elif topic["in_knowledge_map"] and not topic_tools.get_topic_by_path(topic["path"], root_node=topictree).get("children"): sys.stderr.write("Topic '%-40s' in knowledge map, but has no children.\n" % topic["slug"]) elif topic["in_knowledge_map"] and not topic_tools.get_all_leaves(topic_tools.get_topic_by_path(topic["path"], root_node=topictree), leaf_type="Exercise"): sys.stderr.write("Topic '%40s' in knowledge map, but has no exercises.\n" % topic["slug"])
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) topictree_node = topic_tools.get_topic_by_path( node_cache["Topic"][slug]["path"], root_node=topictree) 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] nodecache_node["in_knowledge_map"] = False topictree_node["in_knowledge_map"] = False
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=topictree) 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
def validate_data(topictree, node_cache, knowledge_map): # Validate related videos for exercise in node_cache['Exercise'].values(): exercise_path = os.path.join(settings.PROJECT_PATH, "static", "js", "khan-exercises", "exercises", "%s.html" % exercise["slug"]) if not os.path.exists(exercise_path): sys.stderr.write("Could not find exercise HTML file: %s\n" % exercise_path) for vid in exercise.get("related_video_readable_ids", []): if not vid in node_cache["Video"]: sys.stderr.write( "Could not find related video %s in node_cache (from exercise %s)\n" % (vid, exercise["slug"])) # Validate related exercises for video in node_cache["Video"].values(): ex = video["related_exercise"] if ex and not ex["slug"] 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 in node_cache["Topic"].values(): if not topic_tools.get_topic_by_path( topic["path"], root_node=topictree).get("children"): sys.stderr.write("Could not find any children for topic %s\n" % (topic["path"])) # Validate all topics in knowledge map are in the node cache for slug in knowledge_map["topics"]: if slug not in node_cache["Topic"]: sys.stderr.write("Unknown topic in knowledge map: %s\n" % slug) topicdata_path = os.path.join(settings.PROJECT_PATH + "/static/data/", "topicdata", "%s.json" % slug) if not os.path.exists(topicdata_path): sys.stderr.write( "Could not find topic data in topicdata directory: '%s'\n" % slug) # Validate all topics in node-cache are in (or out) of knowledge map, as requested. for topic in node_cache["Topic"].values(): if topic["in_knowledge_map"] and not topic["slug"] in knowledge_map[ "topics"]: sys.stderr.write( "Topic '%-40s' not in knowledge map, but node_cache says it should be.\n" % topic["slug"]) elif not topic["in_knowledge_map"] and topic["slug"] in knowledge_map[ "topics"]: sys.stderr.write( "Topic '%-40s' in knowledge map, but node_cache says it shouldn't be.\n" % topic["slug"]) elif topic["in_knowledge_map"] and not topic_tools.get_topic_by_path( topic["path"], root_node=topictree).get("children"): sys.stderr.write( "Topic '%-40s' in knowledge map, but has no children.\n" % topic["slug"]) elif topic["in_knowledge_map"] and not topic_tools.get_all_leaves( topic_tools.get_topic_by_path(topic["path"], root_node=topictree), leaf_type="Exercise"): sys.stderr.write( "Topic '%40s' in knowledge map, but has no exercises.\n" % topic["slug"])
def get_exercise_topic_tree(request, topic_path): return JsonResponse(convert_topic_tree_for_dynatree(get_topic_by_path(topic_path)));
def get_topic_tree(request, topic_path): return JsonResponse( convert_topic_tree_for_dynatree(get_topic_by_path(topic_path)))