def add(request): VideoForm = get_video_form(Video) if request.POST: video = Video(uploaded_by_user=request.user) form = VideoForm(request.POST, request.FILES, instance=video, user=request.user) if form.is_valid(): # Save video = form.save(commit=False) video.file_size = video.file.size video.save() # Success! Send back an edit form for backend in get_search_backends(): backend.add(video) messages.success(request, _("Video '{0}' added.").format(video.title), buttons=[ messages.button(reverse('wagtailvideos:edit', args=(video.id,)), _('Edit')) ]) return redirect('wagtailvideos:index') else: messages.error(request, _("The video could not be created due to errors.")) else: form = VideoForm(user=request.user) return render(request, "wagtailvideos/videos/add.html", { 'form': form, })
def chooser_upload(request): VideoForm = get_video_form(Video) searchform = SearchForm() if request.POST: video = Video(uploaded_by_user=request.user) form = VideoForm(request.POST, request.FILES, instance=video) if form.is_valid(): video.uploaded_by_user = request.user video.save() # Reindex the video to make sure all tags are indexed search_index.insert_or_update_object(video) return render_modal_workflow( request, None, 'wagtailvideos/chooser/video_chosen.js', {'video_json': get_video_json(video)} ) else: form = VideoForm() videos = Video.objects.order_by('title') paginator, videos = paginate(request, videos, per_page=12) return render_modal_workflow( request, 'wagtailvideos/chooser/chooser.html', 'wagtailvideos/chooser/chooser.js', {'videos': videos, 'uploadform': form, 'searchform': searchform} )
def chooser(request): VideoForm = get_video_form(Video) uploadform = VideoForm() videos = Video.objects.order_by('-created_at') q = None if ('q' in request.GET or 'p' in request.GET or 'tag' in request.GET or 'collection_id' in request.GET): # this request is triggered from search, pagination or 'popular tags'; # we will just render the results.html fragment collection_id = request.GET.get('collection_id') if collection_id: videos = videos.filter(collection=collection_id) searchform = SearchForm(request.GET) if searchform.is_valid(): q = searchform.cleaned_data['q'] videos = videos.search(q) is_searching = True else: is_searching = False tag_name = request.GET.get('tag') if tag_name: videos = videos.filter(tags__name=tag_name) # Pagination paginator = Paginator(videos, per_page=12) page = paginator.get_page(request.GET.get('p')) return render(request, "wagtailvideos/chooser/results.html", { 'videos': page, 'is_searching': is_searching, 'query_string': q, }) else: searchform = SearchForm() collections = Collection.objects.all() if len(collections) < 2: collections = None paginator = Paginator(videos, per_page=12) page = paginator.get_page(request.GET.get('p')) return render_modal_workflow( request, 'wagtailvideos/chooser/chooser.html', None, { 'videos': page, 'uploadform': uploadform, 'searchform': searchform, 'is_searching': False, 'query_string': q, 'popular_tags': popular_tags_for_model(Video), 'collections': collections, }, json_data=get_chooser_js_data())
def chooser_upload(request): VideoForm = get_video_form(Video) searchform = SearchForm() if request.POST: video = Video(uploaded_by_user=request.user) form = VideoForm(request.POST, request.FILES, instance=video) if form.is_valid(): video.uploaded_by_user = request.user video.save() # Reindex the video to make sure all tags are indexed search_index.insert_or_update_object(video) return render_modal_workflow( request, None, json_data={ 'step': 'video_chosen', 'result': get_video_json(video) } ) else: form = VideoForm() videos = Video.objects.order_by('title') paginator = Paginator(videos, per_page=12) page = paginator.get_page(request.GET.get('p')) return render_modal_workflow( request, 'wagtailvideos/chooser/chooser.html', None, template_vars={'videos': page, 'uploadform': form, 'searchform': searchform}, json_data=get_chooser_js_data() )
def add(request): Video = get_video_model() VideoForm = get_video_form(Video) collections = permission_policy.collections_user_has_permission_for(request.user, 'add') if len(collections) > 1: collections_to_choose = collections else: # no need to show a collections chooser collections_to_choose = None if request.method == 'POST': if not request.is_ajax(): return HttpResponseBadRequest("Cannot POST to this view without AJAX") if not request.FILES: return HttpResponseBadRequest("Must upload a file") # Build a form for validation form = VideoForm({ 'title': request.FILES['files[]'].name, 'collection': request.POST.get('collection'), }, { 'file': request.FILES['files[]'], }) if form.is_valid(): # Save video = form.save(commit=False) video.uploaded_by_user = request.user video.save() # Success! Send back an edit form return JsonResponse({ 'success': True, 'video_id': int(video.id), 'form': render_to_string('wagtailvideos/multiple/edit_form.html', { 'video': video, 'form': get_video_edit_form(Video)( instance=video, prefix='video-%d' % video.id), }, request=request), }) else: # Validation error return JsonResponse({ 'success': False, # https://github.com/django/django/blob/stable/1.6.x/django/forms/util.py#L45 'error_message': '\n'.join(['\n'.join([force_text(i) for i in v]) for k, v in form.errors.items()]), }) else: form = VideoForm() return render(request, 'wagtailvideos/multiple/add.html', { 'max_filesize': form.fields['file'].max_upload_size, 'help_text': form.fields['file'].help_text, 'error_max_file_size': form.fields['file'].error_messages['file_too_large_unknown_size'], 'error_accepted_file_types': form.fields['file'].error_messages['invalid_video_format'], 'collections': collections_to_choose, })
def edit(request, video_id): Video = get_video_model() VideoForm = get_video_form(Video) video = get_object_or_404(Video, id=video_id) if request.POST: original_file = video.file form = VideoForm(request.POST, request.FILES, instance=video) if form.is_valid(): if 'file' in form.changed_data: # if providing a new video file, delete the old one and all renditions. # NB Doing this via original_file.delete() clears the file field, # which definitely isn't what we want... original_file.storage.delete(original_file.name) # Set new video file size video.file_size = video.file.size video = form.save() video.save() # Reindex the video to make sure all tags are indexed for backend in get_search_backends(): backend.add(video) messages.success(request, _("Video '{0}' updated.").format(video.title)) else: messages.error(request, _("The video could not be saved due to errors.")) else: form = VideoForm(instance=video) if not video._meta.get_field('file').storage.exists(video.file.name): # Give error if video file doesn't exist messages.error(request, _( "The source video file could not be found. Please change the source or delete the video." ).format(video.title), buttons=[ messages.button(reverse('wagtailvideos:delete', args=(video.id,)), _('Delete')) ]) if is_modeladmin_installed(): url_helper = AdminURLHelper(Video.get_track_listing_model()) if hasattr(video, 'track_listing'): action_url = url_helper.get_action_url('edit', instance_pk=video.track_listing.pk) else: action_url = url_helper.create_url else: action_url = '' return render(request, "wagtailvideos/videos/edit.html", { 'video': video, 'form': form, 'filesize': video.get_file_size(), 'can_transcode': ffmpeg.installed(), 'transcodes': video.transcodes.all(), 'transcode_form': VideoTranscodeAdminForm(video=video), 'tracks_action_url': action_url, 'user_can_delete': permission_policy.user_has_permission_for_instance(request.user, 'delete', video) })
def chooser(request): VideoForm = get_video_form(Video) uploadform = VideoForm() videos = Video.objects.order_by('-created_at') q = None if ( 'q' in request.GET or 'p' in request.GET or 'tag' in request.GET or 'collection_id' in request.GET ): # this request is triggered from search, pagination or 'popular tags'; # we will just render the results.html fragment collection_id = request.GET.get('collection_id') if collection_id: videos = videos.filter(collection=collection_id) searchform = SearchForm(request.GET) if searchform.is_valid(): q = searchform.cleaned_data['q'] videos = videos.search(q) is_searching = True else: is_searching = False tag_name = request.GET.get('tag') if tag_name: videos = videos.filter(tags__name=tag_name) # Pagination paginator, videos = paginate(request, videos, per_page=12) return render(request, "wagtailvideos/chooser/results.html", { 'videos': videos, 'is_searching': is_searching, 'query_string': q, }) else: searchform = SearchForm() collections = Collection.objects.all() if len(collections) < 2: collections = None paginator, videos = paginate(request, videos, per_page=12) return render_modal_workflow(request, 'wagtailvideos/chooser/chooser.html', 'wagtailvideos/chooser/chooser.js', { 'videos': videos, 'uploadform': uploadform, 'searchform': searchform, 'is_searching': False, 'query_string': q, 'popular_tags': popular_tags_for_model(Video), 'collections': collections, })
def get_video_edit_form(VideoModel): VideoForm = get_video_form(VideoModel) # Make a new form with the file and focal point fields excluded class VideoEditForm(VideoForm): class Meta(VideoForm.Meta): model = VideoModel exclude = ('file', ) return VideoEditForm