def new_user(username, provider): "Creates a new user with a temporary password taking care of any same usernames" same_name_count = User.objects.filter(username__startswith = username).count() if same_name_count: username = '******' % (username, same_name_count + 1) if provider == 'Twitter': username = '******'+username #for Twitter name_count = AuthUser.objects.filter(username__startswith = username).count() if name_count: username = '******'%(username, name_count + 1) user = User(username = username) temp_password = User.objects.make_random_password(length=12) user.set_password(temp_password) return user
def test_author(self): def _get_tip_author(): sl = SubtitleLanguage.objects.get(video=self.video, language_code='en') return sl.get_tip(full=True).author def _add(*args, **kwargs): pipeline.add_subtitles(self.video, 'en', None, *args, **kwargs) # Not passing at all. _add() self.assertEqual(_get_tip_author(), self.anon) # Passing nil. _add(author=None) self.assertEqual(_get_tip_author(), self.anon) # Passing anonymous. _add(author=User.get_anonymous()) self.assertEqual(_get_tip_author(), self.anon) # Passing u1. _add(author=self.u1) self.assertEqual(_get_tip_author().id, self.u1.id) # Passing u2. _add(author=self.u2) self.assertEqual(_get_tip_author().id, self.u2.id) # Passing nonsense self.assertRaises(ValueError, lambda: _add(author='dogs')) self.assertRaises(ValueError, lambda: _add(author=-1234)) self.assertRaises(ValueError, lambda: _add(author=[self.u1]))
def setUp(self): self.video = make_video() self.u1 = UserFactory() self.u2 = UserFactory() self.anon = User.get_anonymous() v = self.video self.en1 = pipeline.add_subtitles(v, 'en', title="title 1", description="desc 1", subtitles=[(100, 200, "sub 1")], author=self.u1) self.en2 = pipeline.add_subtitles(v, 'en', title="title 2", description="desc 2", subtitles=[(100, 200, "sub 2")], author=self.u2) self.en3 = pipeline.add_subtitles(v, 'en', title="title 3", description="desc 3", subtitles=[(100, 200, "sub 3")], author=self.u1)
def _get_subtitles_from_youtube(self, video_obj): from videos.models import SubtitleLanguage, SubtitleVersion, Subtitle url = 'http://www.youtube.com/watch_ajax?action_get_caption_track_all&v=%s' % video_obj.youtube_videoid h = httplib2.Http() resp, content = h.request(url, "GET") if resp.status < 200 or resp.status >= 400: logger.warning("Youtube subtitles error", extra={ 'data': { "url": url, "video_id": video_obj.youtube_videoid, "status_code": resp.status, "response": content } }) return parser = YoutubeSubtitleParser(content) if not parser: return language, create = SubtitleLanguage.objects.get_or_create( video=video_obj, language=parser.language) language.is_original = False language.is_forked = True language.save() try: version_no = language.subtitleversion_set.order_by('-version_no')[:1] \ .get().version_no + 1 except SubtitleVersion.DoesNotExist: version_no = 0 version = SubtitleVersion(language=language) version.version_no = version_no version.datetime_started = datetime.now() version.user = User.get_youtube_anonymous() version.note = u'From youtube' version.is_forked = True version.save() for i, item in enumerate(parser): subtitle = Subtitle(**item) subtitle.version = version subtitle.subtitle_id = int(random.random() * 10e12) subtitle.subtitle_order = i + 1 subtitle.save() version.finished = True version.save() language.had_version = True language.is_complete = True language.save()
def test_contribuitors_do_bypass_moderation(self): lang = self.video.subtitle_language() member, created = TeamMember.objects.get_or_create(user=self.user, team=self.team) member.role=TeamMember.ROLE_MANAGER member.save() add_moderation(self.video, self.team, self.user) joe_doe = User(username="******", password="******", email="*****@*****.**") joe_doe.save() joe_member, c = TeamMember.objects.get_or_create(user=joe_doe, team=self.team) joe_member.save() v0 = self._create_versions(lang, 1, user=joe_doe)[0] self.assertEquals(v0.moderation_status,WAITING_MODERATION) joe_member.promote_to_contributor() joe_doe = refresh_obj(joe_doe) self.assertTrue(self.team.is_contributor(joe_doe, authenticated=False)) v1 = self._create_versions(lang, 1, user=joe_doe)[0] metadata_manager.update_metadata(self.video.pk) v1 = refresh_obj(v1) self.assertEquals(v1.moderation_status, APPROVED)
def _create_user(self, access_token, data): username = self._find_available_username(data) twitter_username = data.screen_name first_name, last_name = self._get_first_last_name(data) avatar = data.profile_image_url email = '%s@twitteruser.%s.com' % (twitter_username, settings.SITE_NAME) user = User(username=username, email=email, first_name=first_name, last_name=last_name) temp_password = User.objects.make_random_password(length=24) user.set_password(temp_password) user.save() TwitterAccount.objects.create(user=user, username=twitter_username, access_token=access_token.key, avatar=avatar) return user
def _get_subtitles_from_youtube(self, video_obj): from videos.models import SubtitleLanguage, SubtitleVersion, Subtitle url = 'http://www.youtube.com/watch_ajax?action_get_caption_track_all&v=%s' % video_obj.youtube_videoid h = httplib2.Http() resp, content = h.request(url, "GET") if resp.status < 200 or resp.status >= 400: logger.warning("Youtube subtitles error", extra={ 'data': { "url": url, "video_id": video_obj.youtube_videoid, "status_code": resp.status, "response": content } }) return parser = YoutubeSubtitleParser(content) if not parser: return language, create = SubtitleLanguage.objects.get_or_create(video=video_obj, language = parser.language) language.is_original = False language.is_forked = True language.save() try: version_no = language.subtitleversion_set.order_by('-version_no')[:1] \ .get().version_no + 1 except SubtitleVersion.DoesNotExist: version_no = 0 version = SubtitleVersion(language=language) version.version_no = version_no version.datetime_started = datetime.now() version.user = User.get_youtube_anonymous() version.note = u'From youtube' version.is_forked = True version.save() for i, item in enumerate(parser): subtitle = Subtitle(**item) subtitle.version = version subtitle.subtitle_id = int(random.random()*10e12) subtitle.subtitle_order = i+1 subtitle.save() version.finished = True version.save() language.had_version = True language.is_complete = True language.save()
def user_videos_activity(context, user=None): user = user or context['user'] if user.is_authenticated(): context['users_actions'] = Action.objects.select_related('video', 'language', 'language__video', 'user') \ .filter(video__customuser=user) \ .exclude(user=user) \ .exclude(user=User.get_anonymous())[:ACTIONS_ON_PAGE] else: context['users_actions'] = Action.objects.none() return context
def user_videos_activity(context, user=None): user = user or context['user'] if user.is_authenticated(): context['users_actions'] = Action.objects.select_related('video', 'language', 'language__video', 'user') \ .filter(video__customuser=user) \ .exclude(user=user) \ .exclude(user=User.get_youtube_anonymous())[:ACTIONS_ON_PAGE] else: context['users_actions'] = Action.objects.none() return context
def user_videos_activity(context, user=None): user = user or context['user'] if user.is_authenticated(): videos_ids = Video.objects.filter(subtitlelanguage__subtitleversion__user=user).distinct() \ .values_list('id', flat=True) context['users_actions'] = Action.objects.filter(Q(video__pk__in=videos_ids)| \ Q(action_type=Action.ADD_VIDEO, user=user)) \ .exclude(user=user) \ .exclude(user=User.get_youtube_anonymous())[:ACTIONS_ON_PAGE] else: context['users_actions'] = Action.objects.none() return context
def authenticate(self, access_token): '''authenticates the token by requesting user information from twitter ''' twitter = oauthtwitter.OAuthApi(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET, access_token) try: userinfo = twitter.GetUserInfo() except: # If we cannot get the user information, user cannot be authenticated raise screen_name = userinfo.screen_name img_url = userinfo.profile_image_url try: user_profile = TwitterUserProfile.objects.get(screen_name = screen_name) if user_profile.user.is_active: return user_profile.user else: return except TwitterUserProfile.DoesNotExist: #Create new user same_name_count = User.objects.filter(username__startswith = screen_name).count() if same_name_count: username = '******' % (screen_name, same_name_count + 1) else: username = screen_name username = '******'+username name_count = AuthUser.objects.filter(username__startswith = username).count() if name_count: username = '******'%(username, name_count + 1) user = User(username = username) temp_password = User.objects.make_random_password(length=12) user.set_password(temp_password) name_data = userinfo.name.split() try: first_name, last_name = name_data[0], ' '.join(name_data[1:]) except: first_name, last_name = screen_name, '' user.first_name, user.last_name = first_name, last_name if img_url: img = ContentFile(urlopen(img_url).read()) name = img_url.split('/')[-1] user.picture.save(name, img, False) #user.email = '%s@twitteruser.%s.com'%(userinfo.screen_name, settings.SITE_NAME) user.save() userprofile = TwitterUserProfile(user = user, screen_name = screen_name) userprofile.access_token = access_token.key userprofile.url = userinfo.url userprofile.location = userinfo.location userprofile.description = userinfo.description userprofile.profile_image_url = userinfo.profile_image_url userprofile.save() AuthMeta(user=user, provider='Twitter').save() return user
def user_videos_activity(context, user=None): user = user or context['user'] if user.is_authenticated(): videos_ids = Video.objects.filter(subtitlelanguage__subtitleversion__user=user).distinct() \ .values_list('id', flat=True) context['users_actions'] = Action.objects.select_related('video', 'language', 'language__video', 'user').filter(Q(video__pk__in=videos_ids)| \ Q(action_type=Action.ADD_VIDEO, user=user)) \ .exclude(user=user) \ .exclude(user=User.get_youtube_anonymous())[:ACTIONS_ON_PAGE] else: context['users_actions'] = Action.objects.none() return context
def _create_user(self, access_token, data, email): username = self._find_available_username(data) twitter_username = data.screen_name first_name, last_name = self._get_first_last_name(data) avatar = data.profile_image_url if email is None: email = TwitterAuthBackend._generate_email(twitter_username) user = User(username=username, email=email, first_name=first_name, last_name=last_name) temp_password = User.objects.make_random_password(length=24) user.set_password(temp_password) user.save() TwitterAccount.objects.create(user=user, username=twitter_username, access_token=access_token.key, avatar=avatar) return user
def _import_srt(self, video, videosrt_url): h = httplib2.Http() resp, content = h.request(videosrt_url, "GET") if resp.status == 200: data = {'video': video.id, 'language': 'en', 'video_language': 'en', 'is_complete': True } file_data = {'subtitles': SimpleUploadedFile('subs.srt', content)} form = SubtitlesUploadForm( CustomUser.get_youtube_anonymous(), data, file_data) if form.is_valid(): form.save() else: print('url {0} did not return valid srt data'.format(videosrt_url))
def _import_srt(self, video, videosrt_url): h = httplib2.Http() resp, content = h.request(videosrt_url, "GET") if resp.status == 200: data = { 'video': video.id, 'language': 'en', 'video_language': 'en', 'is_complete': True } file_data = {'subtitles': SimpleUploadedFile('subs.srt', content)} form = SubtitlesUploadForm(CustomUser.get_youtube_anonymous(), data, file_data) if form.is_valid(): form.save() else: print('url {0} did not return valid srt data'.format( videosrt_url))
def _get_subtitles_from_youtube(self, video_obj): from videos.models import SubtitleLanguage, SubtitleVersion, Subtitle url = 'http://www.youtube.com/watch_ajax?action_get_caption_track_all&v=%s' % video_obj.youtube_videoid d = urllib.urlopen(url) parser = YoutubeSubtitleParser(d.read()) if not parser: return language, create = SubtitleLanguage.objects.get_or_create( video=video_obj, language=parser.language) language.is_original = False language.is_forked = True language.save() try: version_no = language.subtitleversion_set.order_by('-version_no')[:1] \ .get().version_no + 1 except SubtitleVersion.DoesNotExist: version_no = 0 version = SubtitleVersion(language=language) version.version_no = version_no version.datetime_started = datetime.now() version.user = User.get_youtube_anonymous() version.note = u'From youtube' version.is_forked = True version.save() for i, item in enumerate(parser): subtitle = Subtitle(**item) subtitle.version = version subtitle.subtitle_id = int(random.random() * 10e12) subtitle.subtitle_order = i + 1 subtitle.save() version.finished = True version.save() language.had_version = True language.is_complete = True language.save()
def _get_subtitles_from_youtube(self, video_obj): from videos.models import SubtitleLanguage, SubtitleVersion, Subtitle url = 'http://www.youtube.com/watch_ajax?action_get_caption_track_all&v=%s' % video_obj.youtube_videoid d = urllib.urlopen(url) parser = YoutubeSubtitleParser(d.read()) if not parser: return language, create = SubtitleLanguage.objects.get_or_create(video=video_obj, language = parser.language) language.is_original = False language.is_forked = True language.save() try: version_no = language.subtitleversion_set.order_by('-version_no')[:1] \ .get().version_no + 1 except SubtitleVersion.DoesNotExist: version_no = 0 version = SubtitleVersion(language=language) version.version_no = version_no version.datetime_started = datetime.now() version.user = User.get_youtube_anonymous() version.note = u'From youtube' version.is_forked = True version.save() for i, item in enumerate(parser): subtitle = Subtitle(**item) subtitle.version = version subtitle.subtitle_id = int(random.random()*10e12) subtitle.subtitle_order = i+1 subtitle.save() version.finished = True version.save() language.was_complete = True language.is_complete = True language.save()
def _get_subtitles_from_youtube(self): if not self.youtube_videoid: return url = 'http://www.youtube.com/watch_ajax?action_get_caption_track_all&v=%s' % self.youtube_videoid d = urllib.urlopen(url) parser = YoutubeSubtitleParser(d.read()) if not parser: return language = SubtitleLanguage(video=self) language.is_original = False language.is_forked = True language.language = parser.language language.save() version = SubtitleVersion(language=language) version.datetime_started = datetime.now() version.user = User.get_youtube_anonymous() version.note = u'From youtube' version.save() for i, item in enumerate(parser): subtitle = Subtitle(**item) subtitle.version = version subtitle.subtitle_id = int(random.random()*10e12) subtitle.sub_order = i+1 subtitle.save() version.finished = True version.save() language.was_complete = True language.is_complete = True language.save()
def save_subtitles_for_lang(lang, video_pk, youtube_id): from videos.models import Video yt_lc = lang.get('lang_code') lc = LanguageCode(yt_lc, "youtube").encode("unisubs") if not lc in SUPPORTED_LANGUAGE_CODES: logger.warn( "Youtube import did not find language code", extra={"data": { "language_code": lc, "youtube_id": youtube_id, }}) return try: video = Video.objects.get(pk=video_pk) except Video.DoesNotExist: return from videos.models import SubtitleLanguage, SubtitleVersion, Subtitle url = u'http://www.youtube.com/api/timedtext?v=%s&lang=%s&name=%s' url = url % (youtube_id, yt_lc, urlquote(lang.get('name', u''))) xml = YoutubeVideoType._get_response_from_youtube(url) if xml is None: return parser = YoutubeXMLParser(xml) if not parser: return language, create = SubtitleLanguage.objects.get_or_create( video=video, language=lc, defaults={ 'created': datetime.now(), }) language.is_original = False language.is_forked = True language.save() try: version_no = language.subtitleversion_set.order_by('-version_no')[:1] \ .get().version_no + 1 except SubtitleVersion.DoesNotExist: version_no = 0 version = SubtitleVersion(language=language) version.title = video.title version.description = video.description version.version_no = version_no version.datetime_started = datetime.now() version.user = User.get_anonymous() version.note = u'From youtube' version.is_forked = True version.save() for i, item in enumerate(parser): subtitle = Subtitle() subtitle.subtitle_text = item['subtitle_text'] subtitle.start_time = item['start_time'] subtitle.end_time = item['end_time'] subtitle.version = version subtitle.subtitle_id = int(random.random() * 10e12) subtitle.subtitle_order = i + 1 subtitle.save() assert subtitle.start_time or subtitle.end_time, item['subtitle_text'] version.finished = True version.save() language.has_version = True language.had_version = True language.is_complete = True language.save() from videos.tasks import video_changed_tasks video_changed_tasks.delay(video.pk)
def tutorial_shown(request): User.tutorial_was_shown(request.user.id) return HttpResponse(json.dumps({'success': True}))
def secure_id_lookup(secure_id): return User.lookup_by_secure_id(secure_id)
def save_subtitles_for_lang(lang, video_pk, youtube_id): from videos.models import Video lc = lang.get('lang_code') if not lc in SUPPORTED_LANGUAGES_DICT: return try: video = Video.objects.get(pk=video_pk) except Video.DoesNotExist: return from videos.models import SubtitleLanguage, SubtitleVersion, Subtitle url = u'http://www.youtube.com/api/timedtext?v=%s&lang=%s&name=%s' url = url % (youtube_id, lc, urlquote(lang.get('name', u''))) xml = YoutubeVideoType._get_response_from_youtube(url) if xml is None: return parser = YoutubeXMLParser(xml) if not parser: return language, create = SubtitleLanguage.objects.get_or_create(video=video, language=lc) language.is_original = False language.is_forked = True language.save() try: version_no = language.subtitleversion_set.order_by('-version_no')[:1] \ .get().version_no + 1 except SubtitleVersion.DoesNotExist: version_no = 0 version = SubtitleVersion(language=language) version.version_no = version_no version.datetime_started = datetime.now() version.user = User.get_anonymous() version.note = u'From youtube' version.is_forked = True version.save() for i, item in enumerate(parser): subtitle = Subtitle() subtitle.subtitle_text = item['subtitle_text'] subtitle.start_time = item['start_time'] subtitle.end_time = item['end_time'] subtitle.version = version subtitle.subtitle_id = int(random.random()*10e12) subtitle.subtitle_order = i+1 subtitle.save() assert subtitle.start_time or subtitle.end_time, item['subtitle_text'] version.finished = True version.save() language.has_version = True language.had_version = True language.is_complete = True language.save() from videos.tasks import video_changed_tasks video_changed_tasks.delay(video.pk)
def compute_statistics(team, stats_type): """computes a bunch of statistics for the team, either at the video or member levels. """ from views import TableCell summary = '' graph = '' graph_recent = '' summary_recent = '' graph_additional = '' graph_additional_recent = '' summary_additional = '' summary_additional_recent = '' summary_table = '' if stats_type == 'videosstats': (complete_languages, incomplete_languages) = team.get_team_languages() languages = complete_languages + incomplete_languages unique_languages = set(languages) total = 0 numbers = [] y_title = "Number of edited subtitles" for l in unique_languages: count_complete = complete_languages.count(l) count_incomplete = incomplete_languages.count(l) numbers.append( (get_language_label(l), count_complete + count_incomplete, "Published: %s, total edits:" % count_complete)) total += count_complete + count_incomplete summary = 'Top languages (all time)' title = "" graph = plot(numbers, title=title, graph_type='HorizontalBar', labels=True, max_entries=20, y_title=y_title) (complete_languages_recent, incomplete_languages_recent, new_languages) = team.get_team_languages(since=30) languages_recent = complete_languages_recent + incomplete_languages_recent unique_languages_recent = set(languages_recent) summary_recent = "Top languages (past 30 days)" numbers_recent = [] total_recent = 0 for l in unique_languages_recent: count_complete_recent = complete_languages_recent.count(l) count_incomplete_recent = incomplete_languages_recent.count(l) numbers_recent.append( (get_language_label(l), count_complete_recent + count_incomplete_recent, "Published: %s, total edits:" % count_complete_recent)) total_recent += count_complete_recent + count_incomplete_recent title_recent = "" graph_recent = plot(numbers_recent, title=title_recent, graph_type='HorizontalBar', labels=True, max_entries=20, y_title=y_title) summary_table = [] summary_table.append([ TableCell("", header=True), TableCell("all time", header=True), TableCell("past 30 days", header=True) ]) summary_table.append([ TableCell("videos added", header=True), TableCell(str(team.videos_count)), TableCell(str(team.videos_count_since(30))) ]) summary_table.append([ TableCell("languages edited", header=True), TableCell(str(len(unique_languages))), TableCell(str(len(unique_languages_recent))) ]) summary_table.append([ TableCell("subtitles edited", header=True), TableCell(str(total)), TableCell(str(total_recent)) ]) elif stats_type == 'teamstats': languages = list(team.languages()) unique_languages = set(languages) summary = u'Members by language (all time)' numbers = [] for l in unique_languages: numbers.append((get_language_label(l), languages.count(l), get_language_label(l))) title = '' graph = plot(numbers, graph_type='HorizontalBar', title=title, max_entries=25, labels=True, total_label="Members: ") languages_recent = list(team.languages(members_joined_since=30)) unique_languages_recent = set(languages_recent) summary_recent = u'New members by language (past 30 days)' numbers_recent = [] for l in unique_languages_recent: numbers_recent.append( (get_language_label(l), languages_recent.count(l), get_language_label(l), "%s://%s%s" % (DEFAULT_PROTOCOL, Site.objects.get_current().domain, reverse('teams:members', args=[], kwargs={'slug': team.slug}) + "?sort=-joined&lang=%s" % l))) title_recent = '' graph_recent = plot(numbers_recent, graph_type='HorizontalBar', title=title_recent, max_entries=25, labels=True, xlinks=True, total_label="Members: ") summary_table = [] summary_table.append([ TableCell("", header=True), TableCell("all time", header=True), TableCell("past 30 days", header=True) ]) summary_table.append([ TableCell("members joined", header=True), TableCell(str(team.members_count)), TableCell(str(team.members_count_since(30))) ]) summary_table.append([ TableCell("member languages", header=True), TableCell(str(len(unique_languages))), TableCell(str(len(unique_languages_recent))) ]) active_users = {} for sv in team.active_users(): if sv[0] in active_users: active_users[sv[0]].add(sv[1]) else: active_users[sv[0]] = set([sv[1]]) most_active_users = active_users.items() most_active_users.sort(reverse=True, key=lambda x: len(x[1])) if len(most_active_users) > 20: most_active_users = most_active_users[:20] active_users_recent = {} for sv in team.active_users(since=30): if sv[0] in active_users_recent: active_users_recent[sv[0]].add(sv[1]) else: active_users_recent[sv[0]] = set([sv[1]]) most_active_users_recent = active_users_recent.items() most_active_users_recent.sort(reverse=True, key=lambda x: len(x[1])) if len(most_active_users_recent) > 20: most_active_users_recent = most_active_users_recent[:20] def displayable_user(user, users_details): user_details = users_details[user[0]] return ("%s %s (%s)" % (user_details[1], user_details[2], user_details[3]), len(user[1]), "%s %s (%s)" % (user_details[1], user_details[2], user_details[3]), "%s://%s%s" % (DEFAULT_PROTOCOL, Site.objects.get_current().domain, reverse("profiles:profile", kwargs={'user_id': str(user[0])}))) user_details = User.displayable_users( map(lambda x: int(x[0]), most_active_users)) user_details_dict = {} for user in user_details: user_details_dict[user[0]] = user most_active_users = map( lambda x: displayable_user(x, user_details_dict), most_active_users) summary_additional = u'Top contributors (all time)' graph_additional = plot(most_active_users, graph_type='HorizontalBar', title='', labels=True, xlinks=True, total_label="Contributions: ") user_details_recent = User.displayable_users( map(lambda x: int(x[0]), most_active_users_recent)) user_details_dict_recent = {} for user in user_details_recent: user_details_dict_recent[user[0]] = user most_active_users_recent = map( lambda x: displayable_user(x, user_details_dict_recent), most_active_users_recent) summary_additional_recent = u'Top contributors (past 30 days)' graph_additional_recent = plot(most_active_users_recent, graph_type='HorizontalBar', title='', labels=True, xlinks=True, total_label="Contributions: ") statistics = { 'computed_on': datetime.utcnow().replace( tzinfo=utc).strftime("%A %d. %B %Y %H:%M:%S UTC"), 'summary': summary, 'summary_recent': summary_recent, 'activity_tab': stats_type, 'graph': graph, 'graph_recent': graph_recent, 'graph_additional': graph_additional, 'graph_additional_recent': graph_additional_recent, 'summary_additional': summary_additional, 'summary_additional_recent': summary_additional_recent, 'summary_table': summary_table, } return statistics
def setup_amara_db(self, db): from auth.models import CustomUser CustomUser.get_amara_anonymous()
def facebook_login(request, next=None, confirmed=False, email=None, form_data=None): data = parse_signed_request( request.COOKIES['fbsr_' + settings.FACEBOOK_APP_ID], settings.FACEBOOK_SECRET_KEY) if data is None or \ datetime.datetime.now() - datetime.datetime.fromtimestamp(data['issued_at']) > datetime.timedelta(minutes=2): return redirect('auth:login') next = request.GET.get('next', settings.LOGIN_REDIRECT_URL) try: account = FacebookAccount.objects.get(uid=data['user_id']) user = account.user if user.is_active: user = authenticate(facebook=True, user=user) auth_login(request, user) return redirect(next) else: account.delete() raise FacebookAccount.DoesNotExist except FacebookAccount.DoesNotExist: if confirmed: if form_data is not None and \ 'avatar' in form_data and \ 'first_name' in form_data and \ 'last_name' in form_data and \ len(form_data['first_name'] + form_data['last_name']) > 0: user_created = False first_name = form_data['first_name'] last_name = form_data['last_name'] facebook_uid = data['user_id'] img_url = form_data['avatar'] email = email username_to_try = username_base = form_data['first_name'] index = 1 temp_password = User.objects.make_random_password(length=24) while not user_created: try: user = User(username=username_to_try, email=email, first_name=first_name, last_name=last_name) user.set_password(temp_password) user.save() user_created = True except: username_to_try = '%s%d' % (username_base, index) index += 1 if img_url: img = ContentFile(requests.get(img_url).content) name = img_url.split('/')[-1] user.picture.save(name, img, False) FacebookAccount.objects.create(uid=facebook_uid, user=user, avatar=img_url) user = authenticate(facebook=True, user=user) auth_login(request, user) return redirect('/') else: return redirect('auth:confirm_create_user', 'facebook', email) return redirect('auth:login')
def save_subtitles_for_lang(lang, video_pk, youtube_id): from videos.models import Video lc = lang.get('lang_code') if not lc in SUPPORTED_LANGUAGES_DICT: return try: video = Video.objects.get(pk=video_pk) except Video.DoesNotExist: return from videos.models import SubtitleLanguage, SubtitleVersion, Subtitle url = u'http://www.youtube.com/api/timedtext?v=%s&lang=%s&name=%s' url = url % (youtube_id, lc, urlquote(lang.get('name', u''))) xml = YoutubeVideoType._get_response_from_youtube(url) if not xml: return parser = YoutubeXMLParser(xml) if not parser: return language, create = SubtitleLanguage.objects.get_or_create(video=video, language=lc) language.is_original = False language.is_forked = True language.save() try: version_no = language.subtitleversion_set.order_by('-version_no')[:1] \ .get().version_no + 1 except SubtitleVersion.DoesNotExist: version_no = 0 version = SubtitleVersion(language=language) version.version_no = version_no version.datetime_started = datetime.now() version.user = User.get_youtube_anonymous() version.note = u'From youtube' version.is_forked = True version.save() for i, item in enumerate(parser): subtitle = Subtitle() subtitle.subtitle_text = item['subtitle_text'] subtitle.start_time = item['start_time'] subtitle.end_time = item['end_time'] subtitle.version = version subtitle.subtitle_id = int(random.random() * 10e12) subtitle.subtitle_order = i + 1 subtitle.save() assert subtitle.start_time or subtitle.end_time, item['subtitle_text'] version.finished = True version.save() language.has_version = True language.had_version = True language.is_complete = True language.save() from videos.tasks import video_changed_tasks video_changed_tasks.delay(video.pk)
def test_email_diff_subtitles(self): initial_count = len(mail.outbox) # set a user who can receive notification # make sure we have a different author, else he won't get notified author = User(username='******', email='*****@*****.**', notify_by_email=True, valid_email=True) author.save(send_email_confirmation=False) # bypass logic from hell author.valid_email = True author.save() # this is needed for the non_editor template check user2 = User(username='******', email='*****@*****.**', notify_by_email=True, valid_email=True) user2.save(send_email_confirmation=False) # bypass logic from hell user2.valid_email = True user2.save() # version is indentical to previous one video, video_url = Video.add("http://wwww.example.com/video-diff.mp4", None) video.followers.add(author) video.followers.add(user2) language = SubtitleLanguage(video=video, language_code='en') language.save() subs_data = [ [0, 1000, '1'], [1000, 2000, '2'], ] subtitles_1 = SubtitleSet.from_list('en', subs_data) old_version = language.add_version(subtitles=subtitles_1, author=author) # now we change the text on the second sub subs_data[1][2] = '2 changed' # add a regular sub subs_data.append([2000, 3000, 'new sub']) # add an unsyced subs_data.append([None, None, 'no sync']) subtitles_2 = SubtitleSet.from_list('en', subs_data) new_version = language.add_version(subtitles=subtitles_2) self.assertTrue(len(video.notification_list()) > 0) res = send_new_version_notification(new_version.pk) self.assertNotEqual(res, None) # we expect two emails, one is the new-edits-non-editor, and # the other for mail_notification.html self.assertEqual(len(mail.outbox), initial_count + 2) for email_number, email_msg in enumerate(mail.outbox): # make sure this is the right message self.assertIn("New edits to ", email_msg.subject) self.assertIn("video-diff.mp4", email_msg.subject) html = BeautifulSoup(email_msg.body) html_text = "".join(html.body(text=True)).replace("\n", "") if email_number == 0: # assert text and timing changes are correct self.assertIn('67% of the text', html_text) self.assertIn('33% of the timing was changed.', html_text) # find the listed text changes to make sure they match diff_table = html.findAll('table', attrs={'class': 'diffs'})[0] old_version_changes = [] new_version_changes = [] for i, node in enumerate(diff_table.findAll('td')): if i % 2 == 0: old_version_changes.append(node.text) else: new_version_changes.append(node.text) self.assertEqual(old_version_changes, [u'2', u'', u'']) self.assertEqual(new_version_changes, [ u'2 changed', u'new sub', u'no sync', ])
def setUp(self): self.video = make_video() self.u1 = UserFactory() self.u2 = UserFactory() self.anon = User.get_amara_anonymous()
def test_notfications(self): self.assertEqual(len(mail.outbox), 0) user = User(email='*****@*****.**', username='******') user.set_password("secret") user.save() self.assertEqual(len(mail.outbox), 1)
def setup_amara_db(db): CustomUser.get_amara_anonymous()