def create_subtitles(request, video_id): try: video = Video.cache.get_instance_by_video_id(video_id, 'video-page') except Video.DoesNotExist: raise Http404 workflow = video.get_workflow() if not workflow.user_can_create_new_subtitles(request.user): raise PermissionDenied() team_slug = request.GET.get('team', None) if request.method == 'POST': if team_slug: form = TeamCreateSubtitlesForm(request, video, team_slug, request.POST) else: form = CreateSubtitlesForm(request, video, request.POST) if form.is_valid(): form.set_primary_audio_language() response_renderer = AJAXResponseRenderer(request) response_renderer.redirect(form.editor_url()) return response_renderer.render() else: if team_slug: form = TeamCreateSubtitlesForm(request, video, team_slug) else: form = CreateSubtitlesForm(request, video) response_renderer = AJAXResponseRenderer(request) response_renderer.show_modal('future/videos/create-subtitles-modal.html', { 'form': form, 'video': video, }) return response_renderer.render()
def language_select(request): url = request.META.get('HTTP_REFERER').split('/') template_name = 'future/language_switcher.html' response_renderer = AJAXResponseRenderer(request) context = {} context['languages'] = [] valid_options = [code for code, label in settings.LANGUAGES] for code, name in get_language_choices(flat=True, limit_to=valid_options): url[3] = code context['languages'] += [('/'.join(url), code, name)] response_renderer.show_modal(template_name, context) return response_renderer.render()
def comments_form(request, obj, replace_id): if not request.user.is_authenticated(): raise PermissionDenied() comment_form = CommentForm(obj, data=request.POST) success = comment_form.is_valid() if success: comment_form.save(request.user) # reset the comment form to a fresh state comment_form = CommentForm(obj) if request.is_ajax(): response_renderer = AJAXResponseRenderer(request) response_renderer.replace( replace_id, 'future/videos/tabs/comments.html', { 'comments': Comment.get_for_object(obj), 'comment_form': comment_form, }) return response_renderer.render() else: return redirect(request.get_full_path())
def filter_box(request): styleguide = get_styleguide() section = styleguide.sections['filter-box'] context = { 'styleguide': styleguide, 'section': section, 'active_section': 'filter-box', 'form': get_form_for_section(request, 'filter-box'), 'colors': calc_filter_box_colors(request), 'shapes': calc_filter_box_shapes(request), } if request.is_ajax(): response_renderer = AJAXResponseRenderer(request) response_renderer.replace('#content-list', 'styleguide/filter-box-content.html', context) return response_renderer.render() else: return render(request, 'styleguide/filter-box.html', context)
def subtitles_ajax_form(request, video, subtitle_language, version): try: form_name = request.GET['form'] FormClass = subtitles_form_map[form_name] except KeyError: raise Http404() if form_name == 'notes' and request.POST['body'] == '': return AJAXResponseRenderer(request).render() form = FormClass(request.user, video, subtitle_language, version, data=request.POST if request.method == 'POST' else None) if not form.check_permissions(): raise PermissionDenied() if form.is_bound and form.is_valid() and form_name == 'language': form.submit(request) redirect_url = subtitle_language.get_absolute_url() + '?team=' + request.GET['team'] return redirect(redirect_url) elif request.is_ajax(): if form.is_bound and form.is_valid(): form.submit(request) return handle_subtitles_ajax_form_success( request, video, subtitle_language, version, form_name, form) response_renderer = AJAXResponseRenderer(request) template = 'future/videos/subtitles-forms/{}.html'.format( form_name) response_renderer.show_modal(template, { 'video': video, 'subtitle_language': subtitle_language, 'version': version, 'form': form, }) return response_renderer.render() else: # TODO implement the not-AJAX case return redirect(request.get_full_path())
def handle_subtitles_ajax_form_success(request, video, subtitle_language, version, form_name, form): response_renderer = AJAXResponseRenderer(request) if form_name == 'notes': notes = (video.get_workflow() .get_editor_notes(request.user, subtitle_language.language_code) .fetch_notes()) response_renderer.replace( '#subtitles_notes', 'future/videos/tabs/notes.html', { 'notes': notes, 'notes_form': SubtitlesNotesForm( request.user, video, subtitle_language, version) }) else: response_renderer.reload_page() return response_renderer.render()
def video_make_url_primary_form(request, video): if not permissions.can_user_edit_video_urls(video, request.user): raise PermissionDenied() try: video_url = video.videourl_set.get(id=request.POST.get('id', -1)) except VideoUrl.DoesNotExist: success = False else: video_url.make_primary(request.user) success = True response_renderer = AJAXResponseRenderer(request) if success: response_renderer.replace(*urls_tab_replacement_data(request, video)) response_renderer.hide_modal() return response_renderer.render()
def subtitles(request, video_id, lang, lang_id, version_id=None): if should_use_old_view(request): return oldviews.language_subtitles(request, video_id, lang, lang_id, version_id) request.use_cached_user() try: video, subtitle_language, version = get_objects_for_subtitles_page( request.user, video_id, lang, lang_id, version_id) except ObjectDoesNotExist: raise Http404() # Fetch the comments now. We only support posting new comments if the # subtitles already have comments on them comments = Comment.get_for_object(subtitle_language) if 'form' in request.GET: return subtitles_ajax_form(request, video, subtitle_language, version) elif request.POST.get('form') == 'comment' and comments: # Handle the comments form specially, and only handle it if there are # already comments on the video return comments_form(request, subtitle_language, '#subtitles_comments') workflow = video.get_workflow() if request.user.is_authenticated() and comments: comment_form = CommentForm(subtitle_language) else: comment_form = None customization = behaviors.subtitles_page_customize(request, video, subtitle_language) all_activities = (ActivityRecord.objects.for_video(video, customization.team) .filter(language_code=lang)) if request.is_ajax() and request.GET.get('show-all', None): response_renderer = AJAXResponseRenderer(request) response_renderer.replace( '#subtitles_activity', "future/videos/tabs/activity.html", { 'activity': all_activities, }, ) return response_renderer.render() if request.is_ajax() and request.GET.get('update-sync-history', None): response_renderer = AJAXResponseRenderer(request) response_renderer.replace( '#subtitles_sync_history', "future/videos/tabs/sync-history.html", sync_history_context(video, subtitle_language), ) return response_renderer.render() all_subtitle_versions = subtitle_language.versions_for_user( request.user).order_by('-version_number') team_video = video.get_team_video() activity = all_activities[:ACTIVITY_PER_PAGE] show_all = False if len(activity) >= len(all_activities) else True context = { 'video': video, 'team_video': team_video, 'metadata': video.get_metadata().convert_for_display(), 'subtitle_language': subtitle_language, 'subtitle_version': version, 'enable_delete_subtitles': workflow.user_can_delete_subtitles( request.user, subtitle_language.language_code), 'enable_change_language': user_can_change_subtitle_language(request.user, video), 'show_rollback': version and not version.is_tip(public=False), 'all_subtitle_versions': all_subtitle_versions, 'enabled_compare': len(all_subtitle_versions) >= 2, 'has_private_version': any(v.is_private() for v in all_subtitle_versions), 'downloadable_formats': downloadable_formats(request.user), 'activity': activity, 'activity_count': 1, 'show_all': show_all, 'comments': comments, 'comment_form': comment_form, 'enable_edit_in_admin': request.user.is_superuser, 'steps': customization.steps, 'cta': customization.cta, 'due_date': customization.due_date, 'can_edit': workflow.user_can_edit_subtitles( request.user, subtitle_language.language_code), 'header': customization.header, 'extra_page_controls': customization.extra_page_controls, } if workflow.user_can_view_notes(request.user, subtitle_language.language_code): editor_notes = workflow.get_editor_notes(request.user, subtitle_language.language_code) if editor_notes: context['show_notes'] = True context['notes'] = editor_notes.fetch_notes() if workflow.user_can_post_notes(request.user, subtitle_language.language_code): context['notes_form'] = SubtitlesNotesForm( request.user, video, subtitle_language, version) else: context['show_notes'] = False else: context['show_notes'] = False if team_video and can_resync(team_video.team, request.user): context.update(sync_history_context(video, subtitle_language)) context['show_sync_history'] = True else: context['show_sync_history'] = False context['can_resync'] = False sanity_check_video_urls(request, video) return render(request, 'future/videos/subtitles.html', context)
def video_add_url_form(request, video): if not permissions.can_user_edit_video_urls(video, request.user): raise PermissionDenied() create_url_form = NewCreateVideoUrlForm(video, request.user, data=request.POST) response_renderer = AJAXResponseRenderer(request) if create_url_form.is_valid(): create_url_form.save() response_renderer.clear_form('#add-url-form form') response_renderer.replace(*urls_tab_replacement_data(request, video)) response_renderer.hide_modal() else: response_renderer.replace( '#add-url-form', "future/videos/forms/create-url.html", { 'create_url_form': create_url_form, }) return response_renderer.render()
def video(request, video_id, video_url=None, title=None): if should_use_old_view(request): return oldviews.video(request, video_id, video_url, title) if request.is_ajax() and 'form' in request.POST: return video_ajax_form(request, video_id) request.use_cached_user() try: video = Video.cache.get_instance_by_video_id(video_id, 'video-page') except Video.DoesNotExist: raise Http404 if not video.can_user_see(request.user): raise PermissionDenied() if video_url: video_url = get_object_or_404(video.videourl_set, pk=video_url) else: video_url = video.get_primary_videourl_obj() workflow = video.get_workflow() if workflow.user_can_create_new_subtitles(request.user): form_name = request.GET.get('form', '') if form_name == 'create-subtitles': return create_subtitles(request, video_id) else: # this is the old code for creating the CreateSubtitlesForm create_subtitles_form = CreateSubtitlesForm(request,video) else: create_subtitles_form = None if request.user.is_authenticated(): comment_form = CommentForm(video) else: comment_form = None if permissions.can_user_edit_video_urls(video, request.user): create_url_form = NewCreateVideoUrlForm(video, request.user) allow_delete = allow_make_primary = True else: create_url_form = None allow_delete = allow_make_primary = False customization = behaviors.video_page_customize(request, video) all_activities = ActivityRecord.objects.for_video( video, customization.team) if request.is_ajax() and request.GET.get('show-all', None): response_renderer = AJAXResponseRenderer(request) response_renderer.replace( '#video_activity', "future/videos/tabs/activity.html", { 'activity': all_activities, }, ) return response_renderer.render() activity = all_activities[:ACTIVITY_PER_PAGE] show_all = False if len(activity) >= len(all_activities) else True sanity_check_video_urls(request, video) return render(request, 'future/videos/video.html', { 'video': video, 'player_url': video_url.url, 'team': video.get_team(), 'team_video': video.get_team_video(), 'tab': request.GET.get('tab', 'info'), 'allow_delete': allow_delete, 'allow_make_primary': allow_make_primary, 'create_subtitles_form': create_subtitles_form, 'comment_form': comment_form, 'create_url_form': create_url_form, 'comments': Comment.get_for_object(video), 'activity': activity, 'activity_count': 1, 'show_all': show_all, 'metadata': video.get_metadata().convert_for_display(), 'custom_sidebar': customization.sidebar, 'header': customization.header, 'use_old_messages': True, 'video_urls': [ (vurl, get_sync_account(video, vurl)) for vurl in video.get_video_urls() ], })
def render_management_form_submit(request, form): response_renderer = AJAXResponseRenderer(request) if form.should_process_in_task(): job = tasks.process_management_form.delay(type(form), form.get_pickle_state()) response_renderer.show_modal_progress(0.0, _("Processing")) response_renderer.perform_request(TASK_UPDATE_INTERVAL, "ui:task-progress", job.id) else: response_renderer = AJAXResponseRenderer(request) form.submit() message = form.message() if message: messages.success(request, message) for error in form.error_messages(): messages.error(request, error) response_renderer.reload_page() return response_renderer.render()
def task_progress(request, job_id): if not request.is_ajax(): raise Http404 job = Job.fetch(job_id, queue='high') job_meta = job.get_meta() if job else {} status = job_meta.get('form_status') response_renderer = AJAXResponseRenderer(request) if status == 'PROGRESS': progress = float(job_meta['current']) / job_meta['total'] response_renderer.show_modal_progress( progress, fmt(_("Processing: %(current)s / %(total)s"), current=job_meta['current'], total=job_meta['total'])) response_renderer.perform_request(TASK_UPDATE_INTERVAL, "ui:task-progress", job.id) elif status == 'SUCCESS': response_renderer.show_modal_progress(1.0, _("Complete")) add_job_messages(request, job_meta) response_renderer.reload_page() elif status == 'FAILURE': add_job_messages(request, job_meta) response_renderer.reload_page() else: response_renderer.show_modal_progress(0.0, _("Processing")) response_renderer.perform_request(TASK_UPDATE_INTERVAL, "ui:task-progress", job_id) return response_renderer.render()