Ejemplo n.º 1
0
    def test_batch_mark_as_duplicate_existing_markers(self):
        """
            Two videos are already marked as duplicates
        """
        self._sign_in(self.user)

        DuplicateVideoMarker.add_marker(self.video, self.video_2)
        DuplicateVideoMarker.add_marker(self.video, self.video_3)

        request = VideoBatchContainer.combined_message_class(
            project_id=self.project.pk,
            youtube_ids=[v.youtube_id for v in self.all_live_videos])

        with self.assertNumQueries(6):
            response = self.api.video_batch_mark_as_duplicate(request)

        self.assertEqual(len(self.all_live_videos), len(response.videos))

        for video in self.all_live_videos:
            item = next(i for i in response.items
                        if i.youtube_id == video.youtube_id)
            self.assertEqual(item.msg, 'ok')
            self.assertEqual(item.success, True)

            other_ids = set(v.pk for v in self.all_live_videos if v != video)
            self.assertEqual(other_ids,
                             set(video.get_duplicates(ids_only=True)))
Ejemplo n.º 2
0
    def video_mark_duplicate(self, request):
        """
            API Endpoint to mark a video as a duplicate of another video
            even if the 2 videos have unique YouTube ID's.
        """
        project = self.get_project(request.project_id, assigned_only=True)

        if request.youtube_id == request.duplicate_of_id:
            raise ForbiddenException(
                'You cannot mark a video as a duplicate of itself.')

        videos = {
            v.youtube_id: v
            for v in Video.objects.select_related("youtube_video").filter(
                project=project,
                youtube_id__in=(
                    request.youtube_id,
                    request.duplicate_of_id,
                ))
        }

        if not len(videos) == 2:
            raise NotFoundException

        video = videos[request.youtube_id]
        duplicate_of = videos[request.duplicate_of_id]

        DuplicateVideoMarker.add_marker(video, duplicate_of)

        return self.mapper.map(duplicate_of)
Ejemplo n.º 3
0
    def setUp(self):
        """
            Bootstrap test data
        """
        super(VideoAPITestDuplicatesList, self).setUp()

        self.project = milkman.deliver(Project)
        self.project.set_owner(self.admin)
        self.video = self.create_video(project=self.project)
        self.video_2 = self.create_video(project=self.project)

        DuplicateVideoMarker.add_marker(self.video, self.video_2)
Ejemplo n.º 4
0
    def video_batch_mark_video_as_duplicate(self, request):
        """
            Takes a list of video IDs and makes them all as a duplicate of the
            given video
        """
        project = self.get_project(request.project_id, assigned_only=True)
        video = get_obj_or_api_404(Video,
                                   project=project,
                                   youtube_id=request.youtube_id)

        videos = {
            v.pk: v
            for v in Video.objects.select_related("youtube_video").filter(
                project_id=project.pk, youtube_id__in=request.youtube_ids)
        }
        video_ids = videos.keys()
        youtube_ids = [v.youtube_id for v in videos.values()]
        combinations = [sorted((
            video.pk,
            vid,
        )) for vid in video_ids]

        existing_marker_query = Q()
        for id1, id2 in combinations:
            existing_marker_query = (existing_marker_query
                                     | Q(video_1=id1, video_2=id2))

        existing_markers = list(
            DuplicateVideoMarker.objects.filter(
                existing_marker_query).values_list("video_1", "video_2"))

        new_markers = []
        for vid in video_ids:
            if tuple(sorted((
                    video.pk,
                    vid,
            ))) not in existing_markers:
                new_markers.append(
                    DuplicateVideoMarker(video_1=video, video_2=videos[vid]))

        DuplicateVideoMarker.objects.bulk_create(new_markers)

        return VideoBatchListResponseMessage(items=[
            make_batch_response_message(yt_id, not_found=True)
            for yt_id in set(request.youtube_ids) - set(youtube_ids)
        ] + [
            make_batch_response_message(yt_id, success=True)
            for yt_id in youtube_ids
        ],
                                             videos=map(
                                                 self.mapper.map,
                                                 videos.values()),
                                             is_list=True)
Ejemplo n.º 5
0
    def setUp(self):
        """
            Bootstrap test data
        """
        super(VideoAPIBatchDeleteDuplicateMarkTests, self).setUp()
        self.project = milkman.deliver(Project)
        self.project.set_owner(self.admin)
        self.project.add_assigned(self.user, pending=False)

        self.video = self.create_video(project=self.project)
        self.video_2 = self.create_video(project=self.project)
        self.video_3 = self.create_video(project=self.project)

        self.all_live_videos = (
            self.video,
            self.video_2,
            self.video_3,
        )

        for vid1, vid2 in itertools.combinations(self.all_live_videos, 2):
            DuplicateVideoMarker.add_marker(vid1, vid2)
Ejemplo n.º 6
0
    def test_batch_mark_as_duplicate_existing_markers(self):
        """
            Video already marked as duplicate with one of the videos
        """
        self._sign_in(self.user)

        DuplicateVideoMarker.add_marker(self.video, self.video_2)

        request = VideoIDBatchContainer.combined_message_class(
            project_id=self.project.pk,
            youtube_id=self.video.youtube_id,
            youtube_ids=(
                self.video_2.youtube_id,
                self.video_3.youtube_id,
            ))

        with self.assertNumQueries(7):
            response = self.api.video_batch_mark_video_as_duplicate(request)

        self.assertEqual(2, len(response.videos))

        item_2 = next(i for i in response.items
                      if i.youtube_id == self.video_2.youtube_id)
        self.assertEqual(item_2.msg, 'ok')
        self.assertTrue(item_2.success)

        self.assertEqual(1, len(self.video_2.get_duplicates()))
        self.assertEqual(self.video.pk,
                         self.video_2.get_duplicates(ids_only=True)[0])

        item_3 = next(i for i in response.items
                      if i.youtube_id == self.video_3.youtube_id)
        self.assertEqual(item_3.msg, 'ok')
        self.assertTrue(item_3.success)

        self.assertEqual(1, len(self.video_2.get_duplicates()))
        self.assertEqual(self.video.pk,
                         self.video_3.get_duplicates(ids_only=True)[0])
Ejemplo n.º 7
0
    def video_batch_mark_as_duplicate(self, request):
        """
            Takes a list of video IDs are marks them all as duplicates of
            one another
        """
        project = self.get_project(request.project_id, assigned_only=True)

        videos = {
            v.pk: v
            for v in Video.objects.select_related("youtube_video").filter(
                project_id=project.pk, youtube_id__in=request.youtube_ids)
        }
        video_ids = videos.keys()
        youtube_ids = [v.youtube_id for v in videos.values()]
        combinations = [
            sorted(x) for x in itertools.combinations(video_ids, 2)
        ]

        existing_marker_query = Q()
        for id1, id2 in combinations:
            existing_marker_query = (existing_marker_query
                                     | Q(video_1=id1, video_2=id2))

        existing_markers = list(
            DuplicateVideoMarker.objects.filter(
                existing_marker_query).values_list("video_1", "video_2"))

        new_markers = []
        for id1, id2 in combinations:
            if (
                    id1,
                    id2,
            ) not in existing_markers:
                new_markers.append(
                    DuplicateVideoMarker(video_1=videos[id1],
                                         video_2=videos[id2]))

        DuplicateVideoMarker.objects.bulk_create(new_markers)

        return VideoBatchListResponseMessage(items=[
            make_batch_response_message(yt_id, not_found=True)
            for yt_id in set(request.youtube_ids) - set(youtube_ids)
        ] + [
            make_batch_response_message(yt_id, success=True)
            for yt_id in youtube_ids
        ],
                                             videos=map(
                                                 self.mapper.map,
                                                 videos.values()),
                                             is_list=True)