Ejemplo n.º 1
0
    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
Ejemplo n.º 2
0
    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
Ejemplo n.º 3
0
    def obj_get(self, bundle, **kwargs):
        content_id = kwargs.get("id", None)
        content = get_content_data(bundle.request, content_id)

        if content:
            return Content(**content)
        else:
            raise NotFound('Content with id %s not found' % content_id)
Ejemplo n.º 4
0
 def obj_get(self, bundle, **kwargs):
     content_id = kwargs.get("id", None)
     content = get_content_data(bundle.request, content_id)
     # MUST: Include messages in the api call.
     if content:
         content['messages'] = get_messages_for_api_calls(bundle.request)
         return Content(**content)
     else:
         raise NotFound('Content with id %s not found' % content_id)
Ejemplo n.º 5
0
 def obj_get(self, bundle, **kwargs):
     content_id = kwargs.get("id", None)
     content = get_content_data(bundle.request, content_id)
     # MUST: Include messages in the api call.
     if content:
         content['messages'] = get_messages_for_api_calls(bundle.request)
         return Content(**content)
     else:
         raise NotFound('Content with id %s not found' % content_id)
Ejemplo n.º 6
0
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 []
Ejemplo n.º 7
0
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 []