def update_video_public_field(team_id): from apps.teams.models import Team with Timer("update-video-public-field-time"): team = Team.objects.get(pk=team_id) for team_video in team.teamvideo_set.all(): video = team_video.video video.is_public = team.is_visible video.save() video_changed_tasks(video.id)
def test_incomplete_language(self): user = User.objects.all()[0] video = Video.objects.filter(teamvideo__isnull=False)[0] video.user = user video.save() sv_en = add_subtitles(video, 'en', make_subtitle_lines(4), complete=False) video_changed_tasks(video.pk, sv_en.pk) self.assertEquals(0, BillingRecord.objects.all().count())
def test_original_language(self): user = User.objects.all()[0] video = Video.objects.filter(teamvideo__isnull=False)[0] video.user = user video.primary_audio_language_code = '' video.save() sv_en = add_subtitles(video, 'en', make_subtitle_lines(4), complete=True) video_changed_tasks(video.pk, sv_en.pk) self.assertEquals(1, BillingRecord.objects.all().count()) br = BillingRecord.objects.all()[0] self.assertFalse(br.is_original)
def test_record_insertion(self): user = User.objects.all()[0] video = Video.objects.filter(teamvideo__isnull=False)[0] video.primary_audio_language_code = 'en' video.user = user video.save() now = datetime.now() sv = add_subtitles(video, 'en', make_subtitle_lines(4), complete=True, author=user, created=now) sl = sv.subtitle_language video_changed_tasks(video.pk, sv.pk) self.assertEquals(1, BillingRecord.objects.all().count()) br = BillingRecord.objects.all()[0] self.assertEquals(br.video.pk, video.pk) self.assertEquals(br.team.pk, video.get_team_video().team.pk) self.assertEquals(br.created, now) self.assertEquals(br.is_original, sl.is_primary_audio_language()) self.assertEquals(br.user.pk, user.pk) self.assertEquals(br.new_subtitle_language.pk, sl.pk) team = video.get_team_video().team start = datetime(2013, 1, 1, 0, 0) end = datetime.now() + timedelta(days=1) csv_data = BillingRecord.objects.csv_report_for_team(team, start, end) self.assertEquals(2, len(csv_data)) self.assertEquals(10, len(csv_data[1])) # 2 sv = add_subtitles(video, 'en', make_subtitle_lines(4), author=user, created=now) sl = sv.subtitle_language video_changed_tasks(video.pk, sv.pk) # A new one shouldn't be created for the same language self.assertEquals(1, BillingRecord.objects.all().count())
def test_update_language_complete(self): """ https://unisubs.sifterapp.com/issues/2225 Create a version not synced. Then later """ user = User.objects.all()[0] video = Video.objects.filter(teamvideo__isnull=False)[0] video.user = user video.save() first_version = add_subtitles(video, 'en', make_subtitle_lines(4, is_synced=False), complete=False, author=user) # create a transla video_changed_tasks(video.pk, first_version.pk) self.assertEquals(0, BillingRecord.objects.all().count()) second_version = add_subtitles(video, 'en', make_subtitle_lines(4), complete=True, author=user) video_changed_tasks(video.pk, second_version.pk) self.assertEquals(1, BillingRecord.objects.all().count())
def test_non_ascii_text(self): non_ascii_text = u'abcd\xe9' user = test_factories.create_user(username=non_ascii_text) test_factories.create_team_member(self.team, user) self.video.title = non_ascii_text self.video.save() sv = add_subtitles(self.video, 'en', make_subtitle_lines(4), title=non_ascii_text, author=user, description=non_ascii_text, complete=True) video_changed_tasks(self.video.pk, sv.pk) report = BillingReport.objects.create( start_date=sv.created - timedelta(days=1), end_date=sv.created + timedelta(days=1), type=BillingReport.TYPE_NEW, ) report.teams.add(self.team) self.process_report(report)
def test_update_source_language(self): """ https://unisubs.sifterapp.com/issues/2225 Create a version not synced. Create a translation. Then later finish the original one """ user = User.objects.all()[0] video = Video.objects.filter(teamvideo__isnull=False)[0] video.user = user video.save() original_version = add_subtitles( video, 'en', make_subtitle_lines(4, is_synced=False), complete=False, author=user ) original_lang = original_version.subtitle_language video_changed_tasks(video.pk, original_version.pk) self.assertEquals(0, BillingRecord.objects.all().count()) translation_version = add_subtitles( video, 'pt', make_subtitle_lines(4, is_synced=False), author=user, parents=[original_version]) translation_language = translation_version.subtitle_language # no billing for this one, because it isn't synced! self.assertEquals(0, BillingRecord.objects.all().count()) # now sync them original_version = add_subtitles( video, 'en', make_subtitle_lines(4, is_synced=True), complete=True, author=user) original_lang = original_version.subtitle_language video_changed_tasks(video.pk, original_version.pk) bl_original = BillingRecord.objects.filter(new_subtitle_language=original_lang) self.assertEquals(1, bl_original.count()) translation_version = add_subtitles( video, 'pt', make_subtitle_lines(5), author=user, parents=[original_version], complete=True) video_changed_tasks(video.pk, translation_version.pk) bl_translation = BillingRecord.objects.filter(new_subtitle_language=translation_language) self.assertEquals(1, bl_translation.count())
def test_notification_sending(self): """ Make the system send updates only on the object being followed (language vs. video). The following is taken directly from the ticket ----------------------------------------------- 1. Followers of a video (submitter + anyone who chose to follow the video) should: * Be listed as followers for each language of this video * Get notifications about any changes made to the video or any of the related languages. * Get notifications about any comments left on the video or any of the related videos. 2. Followers of a language (followers of language + transcriber(s)/translator(s) + editor(s) + anyone who chose to follow the language) should: * Get notifications about any changes made to the subtitles in this language, but not in any other language for the same video. * Get notifications about comments made on the subtitles in this language, but not in any other language for the video, nor on the video as a whole entity. """ # Video is submitted by self.user (pk 2, [email protected]) # The submitter is automatically added to followers via the # ``Video.get_or_create_for_url`` method. Here we do that by hand. self.assertEquals(0, Message.objects.count()) self.assertEquals(0, Comment.objects.count()) self.video.user = self.user self.video.user.notify_by_email = True self.video.user.notify_by_message = False self.video.user.save() self.video.followers.add(self.user) self.video.save() # Create a user that only follows the language user_language_only = User.objects.create(username='******', email='*****@*****.**', notify_by_email=True, notify_by_message=True) user_language2_only = User.objects.create(username='******', email='*****@*****.**', notify_by_email=True, notify_by_message=True) # Create a user that will make the edits user_edit_maker = User.objects.create(username='******', email='*****@*****.**', notify_by_email=True, notify_by_message=True) self.language.followers.clear() self.language.followers.add(user_language_only) latest_version = self.language.get_tip() latest_version.title = 'Old title' latest_version.description = 'Old description' latest_version.save() # Create another language lan2 = SubtitleLanguage.objects.create(video=self.video, language_code='ru') lan2.followers.add(user_language2_only) self.assertEquals(4, SubtitleLanguage.objects.count()) subtitles = self.language.get_tip().get_subtitles() subtitles.append_subtitle(1500, 3000, 'new text') version = pipeline.add_subtitles(self.video, self.language.language_code, subtitles, author=user_edit_maker, title="New title", description="New description") # Clear the box because the above generates some emails mail.outbox = [] # Kick it off video_changed_tasks(version.video.id, version.id) # -------------------------------------------------------------------- # How many emails should we have? # * The submitter # * All video followers who want emails # * All followers of the language being changed # * Minus the change author # # In our case that is: languageonly, adam, admin people = set(self.video.followers.filter(notify_by_email=True)) people.update(self.language.followers.filter(notify_by_email=True)) number = len(list(people)) - 1 # for the editor self.assertEqual(len(mail.outbox), number) email = mail.outbox[0] tos = [item for sublist in mail.outbox for item in sublist.to] self.assertTrue('New description' in email.body) self.assertTrue('Old description' in email.body) self.assertTrue('New title' in email.body) self.assertTrue('Old title' in email.body) # Make sure that all followers of the video got notified # Excluding the author of the new version excludes = list( User.objects.filter(email__in=[version.author.email]).all()) self.assertEquals(1, len(excludes)) followers = self.video.notification_list(excludes) self.assertTrue(excludes[0].notify_by_email and excludes[0].notify_by_message) self.assertTrue(followers.filter(pk=self.video.user.pk).exists()) for follower in followers: self.assertTrue(follower.email in tos) self.assertTrue(self.user.notify_by_email) self.assertTrue(self.user.email in tos) # Refresh objects self.user = User.objects.get(pk=self.user.pk) self.video = Video.objects.get(pk=self.video.pk) # Messages sent? self.assertFalse(self.video.user.notify_by_message) self.assertFalse( User.objects.get(pk=self.video.user.pk).notify_by_message) followers = self.video.followers.filter( notify_by_message=True).exclude(pk__in=[e.pk for e in excludes]) self.assertEquals(followers.count(), 1) self.assertNotEquals(followers[0].pk, self.user.pk) self.assertEquals(followers.count(), Message.objects.count()) for follower in followers: self.assertTrue(Message.objects.filter(user=follower).exists()) language_follower_email = None for email in mail.outbox: if user_language_only.email in email.to: language_follower_email = email break self.assertFalse(language_follower_email is None) # -------------------------------------------------------------------- # Now test comment notifications Message.objects.all().delete() mail.outbox = [] # Video comment first form = CommentForm( self.video, { 'content': 'Text', 'object_pk': self.video.pk, 'content_type': ContentType.objects.get_for_model( self.video).pk }) form.save(self.user, commit=True) self.assertEquals(1, Comment.objects.count()) self.assertEqual(len(mail.outbox), 1) emails = [] for e in mail.outbox: for a in e.to: emails.append(a) followers = self.video.followers.filter(notify_by_email=True) self.assertEquals(emails.sort(), [f.email for f in followers].sort()) followers = self.video.followers.filter(notify_by_email=False) for follower in followers: self.assertFalse(follower.email in emails) followers = self.video.followers.filter(notify_by_message=True) self.assertEquals(followers.count(), Message.objects.count()) for message in Message.objects.all(): self.assertTrue(isinstance(message.object, Video)) self.assertTrue(message.user in list(followers)) # And now test comments on languages Message.objects.all().delete() mail.outbox = [] form = CommentForm( self.language, { 'content': 'Text', 'object_pk': self.language.pk, 'content_type': ContentType.objects.get_for_model( self.language).pk }) form.save(self.user, commit=True) self.assertEquals( Message.objects.count(), self.language.followers.filter(notify_by_message=True).count()) followers = self.language.followers.filter(notify_by_message=True) # The author of the comment shouldn't get a message self.assertFalse(Message.objects.filter(user=self.user).exists()) lan2 = SubtitleLanguage.objects.get(pk=lan2.pk) lan2_followers = lan2.followers.all() for message in Message.objects.all(): self.assertTrue(isinstance(message.object, SubtitleLanguage)) self.assertTrue(message.user in list(followers)) self.assertFalse(message.user in list(lan2_followers))