def import_videos(self): if not self.should_import_videos(): return video_ids = google.get_uploaded_video_ids(self.channel_id) if not video_ids: return for video_id in video_ids: if video_id == self.last_import_video_id: break video_url = 'http://youtube.com/watch?v={}'.format(video_id) if self.type == ExternalAccount.TYPE_USER: try: Video.add(video_url, self.user) except Video.UrlAlreadyAdded: continue elif self.import_team: def add_to_team(video, video_url): TeamVideo.objects.create(video=video, team=self.import_team) try: Video.add(video_url, None, add_to_team) except Video.UrlAlreadyAdded: continue self.last_import_video_id = video_ids[0] self.save()
def test_duplicate_video_url_public_video(self): # test calling Video.add() with a URL already in the system on a # public video. url = 'http://example.com/video.mp4' # This should be an exception if the video is being added without a # team video = VideoFactory(video_url__url=url) check_duplicate_url_error(url, video) # Adding with to a team should be okay though Video.add(url, UserFactory(), team=TeamFactory())
def test_speakername_admin_edit(self): v, _ = Video.add('http://unisubs.example.com/video1801.mp4', self.user) self.admin_video_pg.log_in(self.superuser.username, 'password') self.admin_video_pg.open_edit_video_page(v.id) self.admin_video_pg.add_speaker_name('Jerry Garcia') v.clear_language_cache() v, _ = Video.add('http://unisubs.example.com/video1801.mp4', self.user) self.assertEquals({u'speaker-name': u'Jerry Garcia'}, v.get_metadata())
def test_add_youtube_video(self): vt = YoutubeVideoType('http://www.youtube.com/watch?v=_ShmidkrcY0') vt_2 = YoutubeVideoType('http://www.youtube.com/watch?v=_ShmidkrcY0we') vt_3 = YoutubeVideoType('http://www.youtube.com/watch?v=_ShmidkrcY0ewgwe') user = UserFactory() video, video_url = Video.add(vt, user) with assert_raises(Video.UrlAlreadyAdded): video_2, video_url_2 = Video.add(vt_2, user) with assert_raises(Video.UrlAlreadyAdded): video_3, video_url_3 = Video.add(vt_3, user)
def clean(self): if self._errors: return self.cleaned_data # See if any error happen when we create our video try: Video.add(self.cleaned_data['video_url'], self.user, self.setup_video, self.team) except Video.DuplicateUrlError, e: msg = ugettext('This video was already added to your team') self._errors['video_url'] = self.error_class([msg])
def test_exception_in_setup_callback(self): # If setup_callback throws an exception, we shouldn't create any # video/video_url objects num_videos_before_call = Video.objects.count() num_video_urls_before_call = VideoUrl.objects.count() url = 'http://example.com/video.mp4' with assert_raises(ValueError): Video.add(MockVideoType(url), UserFactory(), mock.Mock(side_effect=ValueError())) assert_equal(Video.objects.count(), num_videos_before_call) assert_equal(VideoUrl.objects.count(), num_video_urls_before_call)
def test_title_from_url(self): # As a fallback, we should use the video URL to set the title video, video_url = Video.add(MockVideoType(self.url), self.user) assert_equal(video.title, 'example.com/.../video.mp4') # but not if title is set manually video.delete() def setup_callback(video, video_url): video.title = 'test title' video, video_url = Video.add(MockVideoType(self.url), self.user, setup_callback) assert_equal(video.title, 'test title')
def test_duplicate_video_url_but_other_team(self): # test calling Video.add() with a URL already in the system on a # team video url = 'http://example.com/video.mp4' team = TeamFactory() other_team = TeamFactory() video = VideoFactory(video_url__url=url, team=team) # Adding a video to the same team should be an exception check_duplicate_url_error(url, video, team=team) # Adding to a different team should be okay though Video.add(url, UserFactory(), team=other_team) # Adding a public video should also be okay Video.add(url, UserFactory(), team=None)
def setUpClass(cls): super(TestCaseEditing, cls).setUpClass() cls.editor_pg = editor_page.EditorPage(cls) cls.data_utils = data_helpers.DataHelpers() cls.video_pg = video_page.VideoPage(cls) cls.user = UserFactory.create() cls.video_pg.open_page('auth/login/') cls.video_pg.log_in(cls.user.username, 'password') data = { 'video_url': 'http://www.youtube.com/watch?v=5CKwCfLUwj4', 'title': 'Open Source Philosophy' } url_part = 'videos/' r = cls.data_utils.make_request(cls.user, 'post', url_part, **data) cls.video, _ = Video.add( 'http://www.youtube.com/watch?v=5CKwCfLUwj4', self.user) cls.data_utils.add_subs(video=cls.video) langs = ['en', 'da', 'ar', 'tr', 'zh-cn', 'nl'] for lc in langs: defaults = { 'video': cls.video, 'language_code': lc, 'complete': True, 'visibility': 'public', 'committer': cls.user, 'subtitles': ('apps/webdriver_testing/subtitle_data/' 'Open Source Philosophy.%s.dfxp' % lc) } cls.data_utils.add_subs(**defaults)
def test_move_into_project(self): # Test moving a video from the public area and also into a project video1, video_url1 = Video.add(self.url, self.user) video2, video_url2 = self.team.add_video(self.url, self.user, project=self.project) assert_equal(video2.get_team_video().project, self.project)
def test_add_video(self): # test the simple case of creating a new video video, video_url = Video.add(MockVideoType(self.url), self.user) assert_equal(video.get_video_url(), self.url) assert_equal(video_url.primary, True) assert_equal(video_url.added_by, self.user) assert_equal(video_url.type, MockVideoType.abbreviation) assert_equal(video.user, self.user)
def test_attributes_from_video_url(self): # Video.add() should set attributes on the video from the VideoType mock_video_type = MockVideoType(self.url, title='vurl title', duration=100) mock_video_type.owner_username.return_value = 'test-user' video, video_url = Video.add(mock_video_type, self.user) assert_equal(video.title, 'vurl title') assert_equal(video.duration, 100) assert_equal(video_url.videoid, mock_video_type.video_id)
def test_add_to_team_moves_public_video(self): # Test adding a video URL to a team, when it's already on a video in # the public area. We should move the existing video rather than # create a new one video1, video_url1 = Video.add(self.url, self.user) video2, video_url2 = self.team.add_video(self.url, self.user) assert_equal(video1.id, video2.id) assert_equal(video2.get_team_video().team, self.team) video_url = VideoUrl.objects.get(url=self.url) assert_equal(video_url.team_id, self.team.id)
def test_add_new(self): """Submit a new video for the team. """ test_url = 'http://www.youtube.com/watch?v=i_0DXxNeaQ0' self.videos_tab.log_in(self.admin.username, 'password') self.videos_tab.open_videos_tab(self.team.slug) self.videos_tab.add_video(url=test_url) self.videos_tab.open_videos_tab(self.team.slug) video, _ = Video.add(test_url, self.user) self.assertTrue(self.videos_tab.video_present(video.title))
def test_setup_callback(self): # Test the setup_callback param def setup_callback(video, video_url): video.title = 'setup_callback title' video, video_url = Video.add(MockVideoType(self.url), self.user, setup_callback) assert_equal(video.title, 'setup_callback title') # check that we saved the data to the DB assert_equal( test_utils.reload_obj(video).title, 'setup_callback title')
def test_video_already_added(self): url = 'http://example.com/video.mp4' # test calling Video.add() with a URL already in the system video = VideoFactory(video_url__url=url) num_videos_before_call = Video.objects.count() with assert_raises(Video.UrlAlreadyAdded) as cm: v, vurl = Video.add(url, UserFactory()) assert_equal(cm.exception.video, video) assert_equal(cm.exception.video_url, video.get_primary_videourl_obj()) # test that we didn't create any extra videos as a result of the call assert_equal(Video.objects.count(), num_videos_before_call)
def test_add_video_url_to_team_video(self): # Test trying to add a video URL to a team video, when that video URL # is already in the public area. There's no clear way to handle this, # for now just let it happen video1, video_url1 = Video.add(self.url, self.user) video2, video_url2 = self.team.add_video( 'http://otherurl.com/video.mp4', self.user) with assert_raises(Video.DuplicateUrlError) as cm: video2.add_url(self.url, self.user) assert_equal(cm.exception.video, video1) assert_equal(cm.exception.video_url, video_url1) assert_equal(cm.exception.from_prevent_duplicate_public_videos, True)
def test_add_video_url_to_public(self): # Test trying to add a video URL to a public video, when that video URL # is already on a team video. This should result in a # DuplicateUrlError. video1, video_url1 = self.team.add_video(self.url, self.user) video2, video_url2 = Video.add('http://otherurl.com/video.mp4', self.user) with assert_raises(Video.DuplicateUrlError) as cm: video2.add_url(self.url, self.user) assert_equal(cm.exception.video, video1) assert_equal(cm.exception.video_url, video_url1) assert_equal(cm.exception.from_prevent_duplicate_public_videos, True)
def clean(self): if self._errors: return self.cleaned_data # Try to create the video and see if any errors happen video_url = self.cleaned_data['video_url'] try: self.video, video_url = Video.add(self.cleaned_data['video_url'], self.user) self.created = True except Video.DuplicateUrlError, e: self.video = e.video self.created = False
def test_move_to_team(self): # Test moving a video from a team without # prevent_duplicate_public_videos to a team with it set. It should # result in a DuplicateUrlError if there is a public video with the # same URL public_video, public_video_url = Video.add(self.url, self.user) other_team = TeamFactory(admin=self.user) video, video_url = other_team.add_video(self.url, self.user) with assert_raises(Video.DuplicateUrlError) as cm: video.get_team_video().move_to(self.team) assert_equal(cm.exception.video, public_video) assert_equal(cm.exception.video_url, public_video_url) assert_equal(cm.exception.from_prevent_duplicate_public_videos, True)
def check_duplicate_url_error(url, existing_video, team=None, from_prevent_duplicate_public_videos=False): num_videos_before_call = Video.objects.count() with assert_raises(Video.DuplicateUrlError) as cm: v, vurl = Video.add(url, UserFactory(), team=team) assert_equal(cm.exception.video, existing_video) assert_equal(cm.exception.video_url, existing_video.get_primary_videourl_obj()) assert_equal(cm.exception.from_prevent_duplicate_public_videos, from_prevent_duplicate_public_videos) # test that we didn't create any extra videos as a result of the call assert_equal(Video.objects.count(), num_videos_before_call)
def _create_videos(video_data, users): videos = [] for x in video_data: shuffle(users) def setup_video(video, video_url): video.title = x.get('title') video.is_subtitled = x['langs'] > 0 video, video_url = Video.add(x['url'], user[0] if users else None, setup_video) _add_langs_to_video(video, x['langs']) videos.append(video) return videos
def create(self, validated_data): def setup_video(video, video_url): for key in ('title', 'description', 'duration', 'thumbnail', 'primary_audio_language_code'): if validated_data.get(key): setattr(video, key, validated_data[key]) if validated_data.get('metadata'): video.update_metadata(validated_data['metadata'], commit=False) self._update_team(video, validated_data) try: return Video.add(validated_data['video_url'], self.context['user'], setup_video)[0] except VideoTypeError: self.fail('invalid-url', url=validated_data['video_url']) except Video.UrlAlreadyAdded: self.fail('video-exists', url=validated_data['video_url'])
def _create_video(self, video_type, info, entry): from videos.models import Video from teams.models import TeamVideo def setup_video(video, video_url): for name, value in info.items(): setattr(video, name, value) if self.team: tv = TeamVideo.objects.create( video=video, team=self.team, added_by=self.user, description=video.description) try: video, video_url = Video.add(video_type, self.user, setup_video) self._created_videos.append(video) except Video.UrlAlreadyAdded, e: # This shouldn't happen since we filtered out existing items # already, but catch the exception anyways pass
def setUp(self): self.user_1 = User.objects.create(username='******', notify_by_email=False) self.user_2 = User.objects.create(username='******', notify_by_email=False) def setup_video(video, video_url): video.primary_audio_language_code = 'en' self.video = video = Video.add("http://www.example.com/video.mp4", self.user_1)[0] mail.outbox = [] self.original_language = SubtitleLanguage.objects.create( video=video, language_code='en') subs = SubtitleSet.from_list('en', [ (1000, 2000, "1"), (2000, 3000, "2"), (3000, 4000, "3"), ]) self.original_language.add_version(subtitles=subs)
def test_signals(self, on_video_url_added, on_video_added): def setup_callback(video, video_url): assert_equal(on_video_added.call_count, 0) assert_equal(on_video_url_added.call_count, 0) video, video_url = Video.add(MockVideoType(self.url), self.user, setup_callback) assert_equal(on_video_added.call_count, 1) assert_equal( on_video_added.call_args, mock.call(signal=signals.video_added, sender=video, video_url=video_url)) assert_equal(on_video_url_added.call_count, 1) assert_equal( on_video_url_added.call_args, mock.call(signal=signals.video_url_added, sender=video_url, video=video, new_video=True))
def get_video_id(video_url, public_only=False, referer=None): """ Returns the cache video_id for this video If public only is """ cache_key = _video_id_key(video_url) value = cache.get(cache_key) if bool(value): return value else: from videos.models import Video try: video, _ = Video.add(video_url, None) except Video.DuplicateUrlError, e: video = e.video video_id = video.video_id cache.set(cache_key, video_id, TIMEOUT) return video_id
def create(self, validated_data): def setup_video(video, video_url): for key in ('title', 'description', 'duration', 'thumbnail', 'primary_audio_language_code'): if validated_data.get(key): setattr(video, key, validated_data[key]) if validated_data.get('metadata'): video.update_metadata(validated_data['metadata'], commit=False) try: team = validated_data.get('team') if team: video, video_url = team.add_video( validated_data['video_url'], self.context['user'], self.calc_project(validated_data), setup_video) else: video, video_url = Video.add(validated_data['video_url'], self.context['user'], setup_video) return video except VideoTypeError: self.fail('invalid-url', url=validated_data['video_url']) except Video.DuplicateUrlError: self.fail('video-exists', url=validated_data['video_url'])
def test_add_credit_on_new_video(self): video, video_url = Video.add('http://youtube.com/watch?v=abcdef', self.user) self.mock_add_amara_credit.delay.assert_called_with(video_url.id)
def test_notify_by_message_false(self): self.user.notify_by_message = False video, video_url = Video.add(MockVideoType(self.url), self.user) assert_false(video.followers.filter(id=self.user.id).exists())