def _update_changes_on_nonzero_version(version, last_version): subtitles = version.subtitles() last_subtitles = dict([(item.subtitle_id, item) for item in last_version.subtitles()]) time_count_changed, text_count_changed = 0, 0 new_subtitles_ids = set() for subtitle in subtitles: new_subtitles_ids.add(subtitle.subtitle_id) if subtitle.subtitle_id in last_subtitles: last_subtitle = last_subtitles[subtitle.subtitle_id] if not last_subtitle.text == subtitle.text: text_count_changed += 1 if not subtitle.has_same_timing(last_subtitle): time_count_changed += 1 else: time_count_changed += 1 text_count_changed += 1 for subtitle_id in last_subtitles.keys(): if subtitle_id not in new_subtitles_ids: text_count_changed += 1 time_count_changed += 1 subs_length = len(subtitles) version.time_change = min(time_count_changed / 1. / subs_length, 1) version.text_change = min(text_count_changed / 1. / subs_length, 1) if user_can_moderate(version.video, version.user): version.moderation_status = APPROVED
def video(request, video_id, video_url=None, title=None): video = get_object_or_404(Video, video_id=video_id) if video_url: video_url = get_object_or_404(VideoUrl, pk=video_url) if not video_url and ((video.title_for_url() and not video.title_for_url() == title) or (not video.title and title)): return redirect(video, permanent=True) video.update_view_counter() # TODO: make this more pythonic, prob using kwargs context = widget.add_onsite_js_files({}) context['video'] = video original = video.subtitle_language() if original: original.pending_moderation_count = get_pending_count(video.subtitle_language()) context['autosub'] = 'true' if request.GET.get('autosub', False) else 'false' translations = list(video.subtitlelanguage_set.filter(had_version=True) \ .filter(is_original=False).select_related('video')) translations.sort(key=lambda f: f.get_language_display()) context['translations'] = translations context["user_can_moderate"] = user_can_moderate(video, request.user) if context["user_can_moderate"]: # FIXME: use amore efficient count for l in translations: l.pending_moderation_count = get_pending_count(l) context['widget_params'] = _widget_params(request, video, language=None, video_url=video_url and video_url.effective_url) _add_share_panel_context_for_video(context, video) context['lang_count'] = video.subtitlelanguage_set.filter(has_version=True).count() context['original'] = video.subtitle_language() return render_to_response('videos/video.html', context, context_instance=RequestContext(request))
def finished_subtitles(self, request, session_pk, subtitles=None, new_title=None, completed=None, forked=False, throw_exception=False): session = SubtitlingSession.objects.get(pk=session_pk) if not request.user.is_authenticated(): return { 'response': 'not_logged_in' } if not session.language.can_writelock(request): return { "response" : "unlockable" } if not session.matches_request(request): return { "response" : "does not match request" } if throw_exception: raise Exception('purposeful exception for testing') from apps.teams.moderation import is_moderated, user_can_moderate language = session.language new_version = None if subtitles is not None and \ (len(subtitles) > 0 or language.latest_version(public_only=False) is not None): new_version = self._create_version_from_session(session, request.user, forked) new_version.save() self._save_subtitles( new_version.subtitle_set, subtitles, new_version.is_forked) language.release_writelock() if completed is not None: language.is_complete = completed if new_title is not None: language.title = new_title language.save() if new_version is not None: video_changed_tasks.delay(language.video.id, new_version.id) else: video_changed_tasks.delay(language.video.id) # we have a default user message, since the UI lets users save non # changed subs, but the backend will realize and will not save that # version. In those cases, we want to show the defatul user message. user_message = "Thank you for uploading. It will take a minute or so for your subtitles to appear." if new_version is not None and new_version.version_no == 0: user_message = "Thank you for uploading. It will take a minute or so for your subtitles to appear." elif new_version and is_moderated(new_version): if user_can_moderate(language.video, request.user) is False: user_message = """This video is moderated by %s. You will not see your subtitles in our widget when you leave this page, they will only appear on our site. We have saved your work for the team moderator to review. After they approve your subtitles, they will show up on our site and in the widget. """ % (new_version.video.moderated_by.name) return { 'user_message': user_message, 'response': 'ok' }
def revision(request, pk): version = get_object_or_404(SubtitleVersion, pk=pk) context = widget.add_onsite_js_files({}) context['video'] = version.video context['version'] = version context['next_version'] = version.next_version() context['prev_version'] = version.prev_version() language = version.language context['language'] = language context["user_can_moderate"] = user_can_moderate(version.video, request.user) context['widget_params'] = _widget_params(request, \ language.video, version.version_no, language) context['latest_version'] = language.latest_version() version.ordered_subtitles() return render_to_response('videos/revision.html', context, context_instance=RequestContext(request))
def video(request, video_id, video_url=None, title=None): video = get_object_or_404(Video, video_id=video_id) if video_url: video_url = get_object_or_404(VideoUrl, pk=video_url) if not video_url and ( (video.title_for_url() and not video.title_for_url() == title) or (not video.title and title)): return redirect(video, permanent=True) video.update_view_counter() # TODO: make this more pythonic, prob using kwargs context = widget.add_onsite_js_files({}) context['video'] = video original = video.subtitle_language() if original: original.pending_moderation_count = get_pending_count( video.subtitle_language()) context['autosub'] = 'true' if request.GET.get('autosub', False) else 'false' translations = list(video.subtitlelanguage_set.filter(had_version=True) \ .filter(is_original=False).select_related('video')) translations.sort(key=lambda f: f.get_language_display()) context['translations'] = translations context["user_can_moderate"] = user_can_moderate(video, request.user) if context["user_can_moderate"]: # FIXME: use amore efficient count for l in translations: l.pending_moderation_count = get_pending_count(l) context['widget_params'] = _widget_params(request, video, language=None, video_url=video_url and video_url.effective_url) _add_share_panel_context_for_video(context, video) context['lang_count'] = video.subtitlelanguage_set.filter( has_version=True).count() context['original'] = video.subtitle_language() return render_to_response('videos/video.html', context, context_instance=RequestContext(request))
def save_finished(self, user, session, subtitles, new_title=None, completed=None, forked=False): from apps.teams.moderation import is_moderated, user_can_moderate language = session.language new_version = None if subtitles is not None and \ (len(subtitles) > 0 or language.latest_version(public_only=False) is not None): new_version = self._create_version_from_session( session, user, forked) new_version.save() self._save_subtitles(new_version.subtitle_set, subtitles, new_version.is_forked) language.release_writelock() if completed is not None: language.is_complete = completed if new_title is not None: language.title = new_title language.save() if new_version is not None: video_changed_tasks.delay(language.video.id, new_version.id) else: video_changed_tasks.delay(language.video.id) # we have a default user message, since the UI lets users save non # changed subs, but the backend will realize and will not save that # version. In those cases, we want to show the defatul user message. user_message = "Your changes have been saved. It will take a minute or so for your subtitles to appear." if new_version is not None and new_version.version_no == 0: user_message = "Your changes have been saved. It will take a minute or so for your subtitles to appear." elif new_version and is_moderated(new_version): if user_can_moderate(language.video, user) is False: user_message = """This video is moderated by %s. You will not see your subtitles in our widget when you leave this page, they will only appear on our site. We have saved your work for the team moderator to review. After they approve your subtitles, they will show up on our site and in the widget. """ % (new_version.video.moderated_by.name) return {'user_message': user_message, 'response': 'ok'}
def revision(request, pk): version = get_object_or_404(SubtitleVersion, pk=pk) context = widget.add_onsite_js_files({}) context['video'] = version.video context['version'] = version context['next_version'] = version.next_version() context['prev_version'] = version.prev_version() language = version.language context['language'] = language context["user_can_moderate"] = False if feature_is_on("MODERATION"): context["user_can_moderate"] = user_can_moderate(video, request.user) context['widget_params'] = _widget_params(request, \ language.video, version.version_no, language) context['latest_version'] = language.latest_version() version.ordered_subtitles() return render_to_response('videos/revision.html', context, context_instance=RequestContext(request))
def save_finished(self, user, session, subtitles, new_title=None, completed=None, forked=False): from apps.teams.moderation import is_moderated, user_can_moderate language = session.language new_version = None if subtitles is not None and (len(subtitles) > 0 or language.latest_version(public_only=False) is not None): new_version = self._create_version_from_session(session, user, forked) new_version.save() self._save_subtitles(new_version.subtitle_set, subtitles, new_version.is_forked) language.release_writelock() if completed is not None: language.is_complete = completed if new_title is not None: language.title = new_title language.save() if new_version is not None: video_changed_tasks.delay(language.video.id, new_version.id) else: video_changed_tasks.delay(language.video.id) # we have a default user message, since the UI lets users save non # changed subs, but the backend will realize and will not save that # version. In those cases, we want to show the defatul user message. user_message = "Your changes have been saved. It will take a minute or so for your subtitles to appear." if new_version is not None and new_version.version_no == 0: user_message = "Your changes have been saved. It will take a minute or so for your subtitles to appear." elif new_version and is_moderated(new_version): if user_can_moderate(language.video, user) is False: user_message = """This video is moderated by %s. You will not see your subtitles in our widget when you leave this page, they will only appear on our site. We have saved your work for the team moderator to review. After they approve your subtitles, they will show up on our site and in the widget. """ % ( new_version.video.moderated_by.name ) return {"user_message": user_message, "response": "ok"}
def diffing(request, first_pk, second_pk): first_version = get_object_or_404(SubtitleVersion, pk=first_pk) language = first_version.language second_version = get_object_or_404(SubtitleVersion, pk=second_pk, language=language) video = first_version.language.video if second_version.datetime_started > first_version.datetime_started: first_version, second_version = second_version, first_version second_captions = dict([(item.subtitle_id, item) for item in second_version.ordered_subtitles()]) first_captions = dict([(item.subtitle_id, item) for item in first_version.ordered_subtitles()]) subtitles = {} for id, item in first_captions.items(): if not id in subtitles: subtitles[id] = item.start_time for id, item in second_captions.items(): if not id in subtitles: subtitles[id] = item.start_time subtitles = [item for item in subtitles.items()] subtitles.sort(key=lambda item: item[1]) captions = [] for subtitle_id, t in subtitles: try: scaption = second_captions[subtitle_id] except KeyError: scaption = None try: fcaption = first_captions[subtitle_id] except KeyError: fcaption = None if fcaption is None or scaption is None: changed = dict(text=True, time=True) else: changed = { 'text': (not fcaption.text == scaption.text), 'time': (not fcaption.start_time == scaption.start_time), 'end_time': (not fcaption.end_time == scaption.end_time) } data = [fcaption, scaption, changed] captions.append(data) context = widget.add_onsite_js_files({}) context['video'] = video context['captions'] = captions context['language'] = language context['first_version'] = first_version context['second_version'] = second_version context['latest_version'] = language.latest_version() context["user_can_moderate"] = user_can_moderate(video, request.user) context['widget0_params'] = \ _widget_params(request, video, first_version.version_no) context['widget1_params'] = \ _widget_params(request, video, second_version.version_no) return render_to_response('videos/diffing.html', context, context_instance=RequestContext(request))
def history(request, video_id, lang=None, lang_id=None): video = get_object_or_404(Video, video_id=video_id) video.update_view_counter() context = widget.add_onsite_js_files({}) if lang_id: try: language = video.subtitlelanguage_set.get(pk=lang_id) except SubtitleLanguage.DoesNotExist: raise Http404 else: language = video.subtitle_language(lang) if not language: if lang in dict(settings.ALL_LANGUAGES): config = {} config["videoID"] = video.video_id config["languageCode"] = lang url = reverse('onsite_widget')+'?config='+urlquote_plus(json.dumps(config)) return redirect(url) elif video.subtitlelanguage_set.count() > 0: language = video.subtitlelanguage_set.all()[0] else: raise Http404 qs = language.subtitleversion_set.select_related('user') ordering, order_type = request.GET.get('o'), request.GET.get('ot') order_fields = { 'date': 'datetime_started', 'user': '******', 'note': 'note', 'time': 'time_change', 'text': 'text_change' } if ordering in order_fields and order_type in ['asc', 'desc']: qs = qs.order_by(('-' if order_type == 'desc' else '')+order_fields[ordering]) context['ordering'], context['order_type'] = ordering, order_type context['video'] = video original = video.subtitle_language() if original: original.pending_moderation_count = get_pending_count(video.subtitle_language()) translations = list(video.subtitlelanguage_set.filter(is_original=False) \ .filter(had_version=True).select_related('video')) context["user_can_moderate"] = user_can_moderate(video, request.user) if context["user_can_moderate"]: # FIXME: use amore efficient count for l in translations: l.pending_moderation_count = get_pending_count(l) translations.sort(key=lambda f: f.get_language_display()) context['translations'] = translations context['last_version'] = language.latest_version(public_only=False) context['widget_params'] = _widget_params(request, video, version_no=None, language=language) context['language'] = language context['edit_url'] = language.get_widget_url() _add_share_panel_context_for_history(context, video, lang) return object_list(request, queryset=qs, allow_empty=True, paginate_by=settings.REVISIONS_ONPAGE, page=request.GET.get('page', 1), template_name='videos/history.html', template_object_name='revision', extra_context=context)
def history(request, video_id, lang=None, lang_id=None): video = get_object_or_404(Video, video_id=video_id) video.update_view_counter() context = widget.add_onsite_js_files({}) if lang_id: try: language = video.subtitlelanguage_set.get(pk=lang_id) except SubtitleLanguage.DoesNotExist: raise Http404 else: language = video.subtitle_language(lang) if not language: if lang in dict(settings.ALL_LANGUAGES): config = {} config["videoID"] = video.video_id config["languageCode"] = lang url = reverse('onsite_widget') + '?config=' + urlquote_plus( json.dumps(config)) return redirect(url) elif video.subtitlelanguage_set.count() > 0: language = video.subtitlelanguage_set.all()[0] else: raise Http404 qs = language.subtitleversion_set.select_related('user') ordering, order_type = request.GET.get('o'), request.GET.get('ot') order_fields = { 'date': 'datetime_started', 'user': '******', 'note': 'note', 'time': 'time_change', 'text': 'text_change' } if ordering in order_fields and order_type in ['asc', 'desc']: qs = qs.order_by(('-' if order_type == 'desc' else '') + order_fields[ordering]) context['ordering'], context['order_type'] = ordering, order_type context['video'] = video original = video.subtitle_language() if original: original.pending_moderation_count = get_pending_count( video.subtitle_language()) translations = list(video.subtitlelanguage_set.filter(is_original=False) \ .filter(had_version=True).select_related('video')) context["user_can_moderate"] = False if feature_is_on("MODERATION"): context["user_can_moderate"] = user_can_moderate(video, request.user) if context["user_can_moderate"]: # FIXME: use amore efficient count for l in translations: l.pending_moderation_count = get_pending_count(l) translations.sort(key=lambda f: f.get_language_display()) context['translations'] = translations context['last_version'] = language.latest_version(public_only=False) context['widget_params'] = _widget_params(request, video, version_no=None, language=language) context['language'] = language context['edit_url'] = language.get_widget_url() _add_share_panel_context_for_history(context, video, lang) return object_list(request, queryset=qs, allow_empty=True, paginate_by=settings.REVISIONS_ONPAGE, page=request.GET.get('page', 1), template_name='videos/history.html', template_object_name='revision', extra_context=context)