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 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 image_chooser_and_upload(request): Image = get_image_model() ImageForm = get_image_form(Image) if request.method == "POST": image = Image(uploaded_by_user=request.user) form = ImageForm( request.POST, request.FILES, instance=image, user=request.user, prefix="image-chooser-upload", ) if form.is_valid(): # Set image file size image.file_size = image.file.size # Set image file hash image.file.seek(0) image._set_file_hash(image.file.read()) image.file.seek(0) form.save() # Reindex the image to make sure all tags are indexed search_index.insert_or_update_object(image) if request.GET.get("select_format"): form = ImageInsertionForm( initial={"alt_text": image.default_alt_text}, prefix="image-chooser-insertion", ) return render_modal_workflow( request, "non_admin_draftail/image/select_format.html", None, { "image": image, "form": form }, json_data={"step": "select_format"}, ) else: # not specifying a format; return the image details now return render_modal_workflow( request, None, None, None, json_data={ "step": "image_chosen", "result": get_image_result_data(image), }, ) else: form = ImageForm(user=request.user, prefix="image-chooser-upload") context = get_chooser_context(request) context.update({"uploadform": form}) return render_modal_workflow( request, "non_admin_draftail/image/upload.html", None, context, json_data=get_chooser_js_data(), )
def chooser(request): Image = get_image_model() # get collection permissions according to user groups collections = None groups = request.user.groups.all() if groups: collection_permissions = GroupCollectionPermission.objects.filter(group__in=groups) if collection_permissions: collections = [cp.collection for cp in collection_permissions.all()] if not collections: collections = Collection.objects.all() if permission_policy.user_has_permission(request.user, 'add'): ImageForm = get_image_form(Image) uploadform = ImageForm(user=request.user) else: uploadform = None images = None temp = None for collection in collections: images = Image.objects.filter(collection_id=collection.id) if temp != None: images = temp | images temp = images root_collection = Collection.objects.get(name="Root").id images = images | Image.objects.filter(collection_id=root_collection) images = images.distinct().order_by('-created_at') # allow hooks to modify the queryset for hook in hooks.get_hooks('construct_image_chooser_queryset'): images = hook(images, request) 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: images = images.filter(collection=collection_id) searchform = SearchForm(request.GET) if searchform.is_valid(): q = searchform.cleaned_data['q'] images = images.search(q) is_searching = True else: is_searching = False q = None tag_name = request.GET.get('tag') if tag_name: images = images.filter(tags__name=tag_name) # Pagination paginator, images = paginate(request, images, per_page=12) return render(request, "wagtailimages/chooser/results.html", { 'images': images, 'is_searching': is_searching, 'query_string': q, 'will_select_format': request.GET.get('select_format') }) else: paginator, images = paginate(request, images, per_page=12) context = get_chooser_context(request) context.update({ 'images': images, 'uploadform': uploadform, }) return render_modal_workflow( request, 'wagtailimages/chooser/chooser.html', None, context, json_data=get_chooser_js_data() )