def alter_list_data_to_serialize(self, request, to_be_serialized): """ Defines a hook to process list view data before being serialized. We pluck out the "user" and replace with the fields we're interested in (username, facility name, is_teacher), and pluck out the content_id and replace with the content_title (if found). This is to make the csv output more human friendly. :param request: HTTP request object :param to_be_serialized: the unprocessed list of objects that will be serialized :return: the _processed_ list of objects to serialize """ from kalite.topic_tools import get_content_data, get_exercise_data filtered_bundles = [ bundle for bundle in to_be_serialized["objects"] if (bundle.data["difficulty"], bundle.data["quality"]) != (0, 0) ] serializable_objects = [] for bundle in filtered_bundles: user_id = bundle.data["user"].data["id"] user = self._facility_users.get(user_id) bundle.data["username"] = user.username bundle.data["facility_name"] = user.facility.name bundle.data["is_teacher"] = user.is_teacher bundle.data.pop("user") content_id = bundle.data.pop("content_id", None) content = get_content_data( request, content_id) or get_exercise_data(request, content_id) bundle.data["content_title"] = content.get( "title", "Missing title") if content else "Unknown content" serializable_objects.append(bundle) to_be_serialized["objects"] = serializable_objects return to_be_serialized
def obj_get(self, bundle, **kwargs): id = kwargs.get("id", None) exercise = get_exercise_data(bundle.request, id) if exercise: return Exercise(**exercise) else: raise NotFound('Exercise with id %s not found' % id)
def alter_list_data_to_serialize(self, request, to_be_serialized): """ Defines a hook to process list view data before being serialized. We pluck out the "user" and replace with the fields we're interested in (username, facility name, is_teacher), and pluck out the content_id and replace with the content_title (if found). This is to make the csv output more human friendly. :param request: HTTP request object :param to_be_serialized: the unprocessed list of objects that will be serialized :return: the _processed_ list of objects to serialize """ from kalite.topic_tools import get_content_data, get_exercise_data filtered_bundles = [bundle for bundle in to_be_serialized["objects"] if (bundle.data["difficulty"], bundle.data["quality"]) != (0, 0)] serializable_objects = [] for bundle in filtered_bundles: user_id = bundle.data["user"].data["id"] user = self._facility_users.get(user_id) bundle.data["username"] = user.username bundle.data["facility_name"] = user.facility.name bundle.data["is_teacher"] = user.is_teacher bundle.data.pop("user") content_id = bundle.data.pop("content_id", None) content = get_content_data(request, content_id) or get_exercise_data(request, content_id) bundle.data["content_title"] = content.get("title", "Missing title") if content else "Unknown content" serializable_objects.append(bundle) to_be_serialized["objects"] = serializable_objects return to_be_serialized
def get_next_recommendations(user, request): """Get the recommendations for the Next section, and return them as a list. Logic: Next recommendations are currently comprised of 3 main subgroups: group recommendations, struggling exercises, and topic tree based data. Group recommendations consist of finding the most common item tackled immediately after the most recent item, struggling is determined by the "struggling" model attribute, and topic tree data is based off the graphical distance between neighboring exercise/topic nodes. Args: user -- The current user as a facility user model object. """ exercise_parents_table = get_exercise_parents_lookup_table() most_recent = get_most_recent_exercises(user) complete_exercises = set(get_completed_exercises(user)) def filter_complete(ex): return ex not in complete_exercises if len(most_recent) > 0 and most_recent[0] in exercise_parents_table: current_subtopic = exercise_parents_table[most_recent[0]]['subtopic_id'] else: current_subtopic = None #logic for recommendations based off of the topic tree structure if current_subtopic: topic_tree_based_data = generate_recommendation_data()[current_subtopic]['related_subtopics'][:settings.TOPIC_RECOMMENDATION_DEPTH] topic_tree_based_data = get_exercises_from_topics(topic_tree_based_data) else: topic_tree_based_data = [] #for checking that only exercises that have not been accessed are returned topic_tree_based_data = [ex for ex in topic_tree_based_data if ex not in most_recent or filter_complete(ex)] #logic to generate recommendations based on exercises student is struggling with struggling = filter(filter_complete, get_exercise_prereqs(get_struggling_exercises(user))) #logic to get recommendations based on group patterns, if applicable group = filter(filter_complete, get_group_recommendations(user)) #now append titles and other metadata to each exercise id final = [] # final data to return for exercise_id in (group[:2] + struggling[:2] + topic_tree_based_data[:1]): #notice the concatenation if exercise_id in exercise_parents_table: subtopic_id = exercise_parents_table[exercise_id]['subtopic_id'] exercise = get_exercise_data(request, exercise_id) exercise["topic"] = get_topic_data(request, subtopic_id) final.append(exercise) #final recommendations are a combination of struggling, group filtering, and topic_tree filtering return final
def get_resume_recommendations(user, request): """Get the recommendation for the Resume section. Logic: Find the most recent incomplete item (video or exercise) and return that as the recommendation. Args: user -- The current user as a facility user model object. """ final = get_most_recent_incomplete_item(user) if final: if final.get("kind") == "Content": return [get_content_data(request, final.get("id"))] if final.get("kind") == "Exercise": return [get_exercise_data(request, final.get("id"))] else: return []