def test_update_item_with_duplicated_item(self):
     """
     Tests that update_items updates *all* items with the same id.
     Content item ids are not unique (though paths are) because the db is not normalized, so items with the same
       id should be updated together.
     """
     item_id = "addition_1"  # This item is known to be duplicated.
     items = get_content_items(ids=[item_id])
     self.assertGreater(len(items), 1)
     item = items[0]
     available = item.get("available")
     inverse_available = not available
     update_item({"available": inverse_available}, item.get("path"))
     items = get_content_items(ids=[item.get("id")])
     self.assertTrue(all([item.get("available") == inverse_available for item in items]))
示例#2
0
def update_all_distributed_callback(request):
    """
    """

    if request.method != "POST":
        raise PermissionDenied("Only POST allowed to this URL endpoint.")

    videos = json.loads(request.POST["video_logs"])
    exercises = json.loads(request.POST["exercise_logs"])
    user = FacilityUser.objects.get(id=request.POST["user_id"])
    node_ids = [node.get("id") for node in get_content_items()]
    # Save videos
    n_videos_uploaded = 0
    for video in videos:
        video_id = video['video_id']
        youtube_id = video['youtube_id']

        # Only save video logs for videos that we recognize.
        if video_id not in node_ids:
            logging.warn("Skipping unknown video %s" % video_id)
            continue

        try:
            (vl, _) = VideoLog.get_or_initialize(user=user, video_id=video_id)  # has to be that video_id, could be any youtube_id
            for key,val in video.iteritems():
                setattr(vl, key, val)
            logging.debug("Saving video log for %s: %s" % (video_id, vl))
            vl.save()
            n_videos_uploaded += 1
        except KeyError:  #
            logging.error("Could not save video log for data with missing values: %s" % video)
        except Exception as e:
            error_message = _("Unexpected error importing videos: %(err_msg)s") % {"err_msg": e}
            return JsonResponseMessageError(error_message, status=500)

    # Save exercises
    n_exercises_uploaded = 0
    for exercise in exercises:
        # Only save video logs for videos that we recognize.
        if exercise['exercise_id'] not in node_ids:
            logging.warn("Skipping unknown video %s" % exercise['exercise_id'])
            continue

        try:
            (el, _) = ExerciseLog.get_or_initialize(user=user, exercise_id=exercise["exercise_id"])
            for key,val in exercise.iteritems():
                setattr(el, key, val)
            logging.debug("Saving exercise log for %s: %s" % (exercise['exercise_id'], el))
            el.save()
            n_exercises_uploaded += 1
        except KeyError:
            logging.error("Could not save exercise log for data with missing values: %s" % exercise)
        except Exception as e:
            error_message = _("Unexpected error importing exercises: %(err_msg)s") % {"err_msg": e}
            return JsonResponseMessageError(error_message, status=500)

    return JsonResponseMessageSuccess(_("Uploaded %(num_exercises)d exercises and %(num_videos)d videos") % {
        "num_exercises": n_exercises_uploaded,
        "num_videos": n_videos_uploaded,
    })
示例#3
0
 def test_topic_availability(self):
     nodes = get_content_items()
     for topic in nodes:
         if topic.get("kind") == "Topic":
             any_available = any([
                 item.get("available", False)
                 for item in get_topic_nodes(parent=topic.get("id"))
             ])
             self.assertEqual(
                 topic["available"], any_available,
                 "Topic availability for {topic} did not match child availability when any children are available."
                 .format(topic=topic.get("title")))
示例#4
0
 def test_topic_availability(self):
     nodes = get_content_items()
     for topic in nodes:
         if topic.get("kind") == "Topic":
             any_available = any([item.get("available", False) for item in get_topic_nodes(parent=topic.get("id"))])
             self.assertEqual(
                 topic["available"],
                 any_available,
                 "Topic availability for {topic} did not match child availability when any children are available.".format(
                     topic=topic.get("title")
                 ),
             )
示例#5
0
def get_exercise_prereqs(exercises):
    """Return a list of prequisites (if applicable) for each specified exercise.

    :param exercise_ids: A list of exercise ids.
    :return: A list of prerequisite exercises (as dicts), if any are known.
    """
    if exercises:
        exercises = get_content_items(ids=exercises)
    prereqs = []
    for exercise in exercises:
        prereqs += exercise.get('prerequisites', [])

    return list(set(prereqs))
def get_exercise_prereqs(exercises):
    """Return a list of prequisites (if applicable) for each specified exercise.

    :param exercise_ids: A list of exercise ids.
    :return: A list of prerequisite exercises (as dicts), if any are known.
    """
    if exercises:
        exercises = get_content_items(ids=exercises)
    prereqs = []
    for exercise in exercises:
        prereqs += exercise.get('prerequisites', [])

    return list(set(prereqs))
示例#7
0
    def _setup(self, num_logs=50, **kwargs):
        super(OneHundredRandomLogUpdates, self)._setup(**kwargs)
        nodes = dict([(node.get("id"), node) for node in get_content_items()])

        try:
            self.user = FacilityUser.objects.get(username=self.username)
        except:
            #take username from ExerciseLog
            all_exercises = ExerciseLog.objects.all()
            self.user = FacilityUser.objects.get(id=all_exercises[0].user_id)
            print self.username, " not in FacilityUsers, using ", self.user
        self.num_logs = num_logs
        #give the platform a chance to cache the logs
        ExerciseLog.objects.filter(user=self.user).delete()
        for x in range(num_logs):
            while True:
                ex_idx = int(self.random.random() * len(nodes.keys()))
                ex_id = nodes.keys()[ex_idx]
                if not ExerciseLog.objects.filter(user=self.user, exercise_id=ex_id):
                    break
            ex = ExerciseLog(user=self.user, exercise_id=ex_id)
            ex.save()
        self.exercise_list = ExerciseLog.objects.filter(user=self.user)
        self.exercise_count = self.exercise_list.count()

        VideoLog.objects.filter(user=self.user).delete()
        for x in range(num_logs):
            while True:
                vid_idx = int(self.random.random() * len(nodes.keys()))
                vid_id = nodes.keys()[vid_idx]
                if not VideoLog.objects.filter(user=self.user, video_id=vid_id):
                    break
            vid = VideoLog(user=self.user, video_id=vid_id)
            vid.save()
        self.video_list = VideoLog.objects.filter(user=self.user)
        self.video_count = self.video_list.count()