def chooser(request): EmbedVideo = get_embed_video_model() if request.user.has_perm('wagtail_embed_videos.add_embedvideo'): can_add = True else: can_add = False q = None embed_videos = EmbedVideo.objects.order_by('-created_at') if 'q' in request.GET or 'p' in request.GET: searchform = SearchForm(request.GET) if searchform.is_valid(): q = searchform.cleaned_data['q'] embed_videos = embed_videos.search(q) is_searching = True else: is_searching = False q = None # Pagination # paginator, embed_videos = paginate(request, embed_videos, per_page=12) #DEPRECATED paginator = Paginator(embed_videos, per_page=25) embed_videos = paginator.get_page(request.GET.get('p')) return render( request, "wagtail_embed_videos/chooser/results.html", { 'embed_videos': embed_videos, 'is_searching': is_searching, 'can_add': can_add, 'query_string': q, }) else: # paginator, embed_videos = paginate(request, embed_videos, per_page=12) #DEPRECATED paginator = Paginator(embed_videos, per_page=25) embed_videos = paginator.get_page(request.GET.get('p')) searchform = SearchForm() return render_modal_workflow( request, 'wagtail_embed_videos/chooser/chooser.html', None, #'wagtail_embed_videos/chooser/chooser.js', template_vars={ 'embed_videos': embed_videos, 'searchform': searchform, 'is_searching': False, 'can_add': can_add, 'query_string': q, 'popular_tags': popular_tags_for_model(EmbedVideo), }, json_data={ 'step': 'chooser', })
def index(request): Image = get_image_model() # Get images (filtered by user permission) images = permission_policy.instances_user_has_any_permission_for( request.user, ['change', 'delete'] ).order_by('-created_at') # Search query_string = None if 'q' in request.GET: form = SearchForm(request.GET, placeholder=_("Search images")) if form.is_valid(): query_string = form.cleaned_data['q'] images = images.search(query_string) else: form = SearchForm(placeholder=_("Search images")) # Filter by collection current_collection = None collection_id = request.GET.get('collection_id') if collection_id: try: current_collection = Collection.objects.get(id=collection_id) images = images.filter(collection=current_collection) except (ValueError, Collection.DoesNotExist): pass paginator = Paginator(images, per_page=20) images = paginator.get_page(request.GET.get('p')) collections = permission_policy.collections_user_has_any_permission_for( request.user, ['add', 'change'] ) if len(collections) < 2: collections = None else: collections = Collection.order_for_display(collections) # Create response if request.is_ajax(): return render(request, 'wagtailimages/images/results.html', { 'images': images, 'query_string': query_string, 'is_searching': bool(query_string), }) else: return render(request, 'wagtailimages/images/index.html', { 'images': images, 'query_string': query_string, 'is_searching': bool(query_string), 'search_form': form, 'popular_tags': popular_tags_for_model(Image), 'collections': collections, 'current_collection': current_collection, 'user_can_add': permission_policy.user_has_permission(request.user, 'add'), })
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 index(request): EmbedVideo = get_embed_video_model() # Get embed videos embed_videos = EmbedVideo.objects.order_by('-created_at') # Permissions if not request.user.has_perm('wagtail_embed_videos.change_embedvideo'): # restrict to the user's own embed videos embed_videos = embed_videos.filter(uploaded_by_user=request.user) # Search query_string = None if 'q' in request.GET: form = SearchForm(request.GET, placeholder=_("Search videos")) if form.is_valid(): query_string = form.cleaned_data['q'] if not request.user.has_perm( 'wagtail_embed_videos.change_embedvideo'): # restrict to the user's own embed videos embed_videos = EmbedVideo.objects.search( query_string, filters={'uploaded_by_user_id': request.user.id}) else: embed_videos = EmbedVideo.objects.search(query_string) else: form = SearchForm(placeholder=_("Search videos")) # Pagination p = request.GET.get('p', 1) paginator = Paginator(embed_videos, 20) try: embed_videos = paginator.page(p) except PageNotAnInteger: embed_videos = paginator.page(1) except EmptyPage: embed_videos = paginator.page(paginator.num_pages) # Create response if request.is_ajax(): return render( request, 'wagtail_embed_videos/embed_videos/results.html', { 'embed_videos': embed_videos, 'query_string': query_string, 'is_searching': bool(query_string), }) else: return render( request, 'wagtail_embed_videos/embed_videos/index.html', { 'embed_videos': embed_videos, 'query_string': query_string, 'is_searching': bool(query_string), 'search_form': form, 'popular_tags': popular_tags_for_model(EmbedVideo), })
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 chooser(request): EmbedVideo = get_embed_video_model() if request.user.has_perm('wagtail_embed_videos.add_embedvideo'): can_add = True else: can_add = False q = None embed_videos = EmbedVideo.objects.order_by('-created_at') if 'q' in request.GET or 'p' in request.GET: searchform = SearchForm(request.GET) if searchform.is_valid(): q = searchform.cleaned_data['q'] embed_videos = embed_videos.search(q) is_searching = True else: is_searching = False q = None # Pagination paginator, embed_videos = paginate(request, embed_videos, per_page=12) return render(request, "wagtail_embed_videos/chooser/results.html", { 'embed_videos': embed_videos, 'is_searching': is_searching, 'can_add': can_add, 'query_string': q, }) else: paginator, embed_videos = paginate(request, embed_videos, per_page=12) searchform = SearchForm() return render_modal_workflow( request, 'wagtail_embed_videos/chooser/chooser.html', 'wagtail_embed_videos/chooser/chooser.js', { 'embed_videos': embed_videos, 'searchform': searchform, 'is_searching': False, 'can_add': can_add, 'query_string': q, 'popular_tags': popular_tags_for_model(EmbedVideo), } )
def get_chooser_context(request): """Helper function to return common template context variables for the main chooser view""" collections = Collection.objects.all() if len(collections) < 2: collections = None return { 'searchform': SearchForm(), 'is_searching': False, 'query_string': None, 'will_select_format': request.GET.get('select_format'), 'popular_tags': popular_tags_for_model(get_image_model()), 'collections': collections, }
def get_chooser_context(request): """Helper function to return common template context variables for the main chooser view""" collections = Collection.objects.all() if len(collections) < 2: collections = None return { 'searchform': SearchForm(), 'is_searching': False, 'query_string': None, 'will_select_format': request.GET.get('select_format'), 'popular_tags': popular_tags_for_model(get_image_model()), 'collections': collections, }
def index(request): # Get Videos (filtered by user permission) videos = Video.objects.all() # Search query_string = None if 'q' in request.GET: form = SearchForm(request.GET, placeholder=_("Search videos")) if form.is_valid(): query_string = form.cleaned_data['q'] videos = videos.search(query_string) else: form = SearchForm(placeholder=_("Search videos")) # Filter by collection current_collection = None collection_id = request.GET.get('collection_id') if collection_id: try: current_collection = Collection.objects.get(id=collection_id) videos = videos.filter(collection=current_collection) except (ValueError, Collection.DoesNotExist): pass paginator = Paginator(videos, per_page=25) page = paginator.get_page(request.GET.get('p')) # Create response if request.is_ajax(): response = render( request, 'wagtailvideos/videos/results.html', { 'videos': page, 'query_string': query_string, 'is_searching': bool(query_string), }) return response else: response = render( request, 'wagtailvideos/videos/index.html', { 'videos': page, 'query_string': query_string, 'is_searching': bool(query_string), 'search_form': form, 'popular_tags': popular_tags_for_model(Video), 'current_collection': current_collection, }) return response
def get_chooser_context(request): """Helper function to return common template context variables for the main chooser view""" collections = Collection.objects.all() if len(collections) < 2: collections = None else: collections = Collection.order_for_display(collections) return { 'searchform': SearchForm(), 'is_searching': False, 'query_string': None, 'popular_tags': popular_tags_for_model(get_media_model()), 'collections': collections, }
def chooser(request): Video = get_video_model() video_files = [] if request.user.has_perm('wagtail_video.add_video'): can_add = True else: can_add = False q = None is_searching = False if 'q' in request.GET or 'p' in request.GET: video_files = Video.objects.all() collection_id = request.GET.get('collection_id') if collection_id: video_files = video_files.filter(collection=collection_id) searchform = SearchForm(request.GET) if searchform.is_valid(): q = searchform.cleaned_data['q'] video_files = video_files.search(q) is_searching = True else: video_files = video_files.order_by('-created_at') is_searching = False # Pagination paginator, video_files = paginate(request, video_files, per_page=10) return render( request, "wagtail_video/chooser/results.html", { 'video_files': video_files, 'query_string': q, 'is_searching': is_searching, }) else: searchform = SearchForm() collections = Collection.objects.all() if len(collections) < 2: collections = None video_files = Video.objects.order_by('-created_at') paginator, video_files = paginate(request, video_files, per_page=10) # return render_modal_workflow(request, 'wagtail_video/chooser/chooser.html', None, { # 'video_files': video_files, # 'searchform': searchform, # 'collections': collections, # 'is_searching': False, # }) return render_modal_workflow(request, 'wagtail_video/chooser/chooser.html', None, template_vars={ 'video_files': video_files, 'searchform': searchform, 'is_searching': False, 'can_add': can_add, 'query_string': q, 'popular_tags': popular_tags_for_model(Video), }, json_data={ 'step': 'chooser', })
def index(request): Media = get_media_model() # Get media files (filtered by user permission) media = permission_policy.instances_user_has_any_permission_for( request.user, ['change', 'delete']) # Ordering if 'ordering' in request.GET and request.GET['ordering'] in [ 'title', '-created_at' ]: ordering = request.GET['ordering'] else: ordering = '-created_at' media = media.order_by(ordering) # Filter by collection current_collection = None collection_id = request.GET.get('collection_id') if collection_id: try: current_collection = Collection.objects.get(id=collection_id) media = media.filter(collection=current_collection) except (ValueError, Collection.DoesNotExist): pass # Search query_string = None if 'q' in request.GET: form = SearchForm(request.GET, placeholder=_("Search media files")) if form.is_valid(): query_string = form.cleaned_data['q'] media = media.search(query_string) else: form = SearchForm(placeholder=_("Search media")) # Pagination paginator, media = paginate(request, media) collections = permission_policy.collections_user_has_any_permission_for( request.user, ['add', 'change']) if len(collections) < 2: collections = None # Create response if request.is_ajax(): return render( request, 'wagtailmedia/media/results.html', { 'ordering': ordering, 'media_files': media, 'query_string': query_string, 'is_searching': bool(query_string), }) else: return render( request, 'wagtailmedia/media/index.html', { 'ordering': ordering, 'media_files': media, 'query_string': query_string, 'is_searching': bool(query_string), 'search_form': form, 'popular_tags': popular_tags_for_model(Media), 'user_can_add': permission_policy.user_has_permission(request.user, 'add'), 'collections': collections, 'current_collection': current_collection, })
def chooser(request): Image = get_image_model() if permission_policy.user_has_permission(request.user, 'add'): ImageForm = get_image_form(Image) uploadform = ImageForm(user=request.user) else: uploadform = None will_select_format = request.GET.get('select_format') will_select_rendition = request.GET.get('select_rendition') images = Image.objects.order_by('-created_at') \ .filter(show_in_catalogue=True) # allow hooks to modify the queryset for hook in hooks.get_hooks('construct_image_chooser_queryset'): images = hook(images, request) 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: 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 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': will_select_format, 'will_select_rendition': will_select_rendition, 'post_processing_spec': request.GET.get('pps'), 'additional_params': get_cropper_params(request), }) else: searchform = SearchForm() collections = Collection.objects.all() if len(collections) < 2: collections = None paginator, images = paginate(request, images, per_page=12) return render_modal_workflow(request, 'wagtailimages/chooser/chooser.html', 'wagtailimages/chooser/chooser.js', { 'images': images, 'uploadform': uploadform, 'searchform': searchform, 'is_searching': False, 'query_string': q, 'will_select_format': will_select_format, 'will_select_rendition': will_select_rendition, 'popular_tags': popular_tags_for_model(Image), 'collections': collections, 'uploadid': uuid.uuid4(), 'post_processing_spec': request.GET.get('pps'), 'additional_params': get_cropper_params(request), 'allowed_extensions': ALLOWED_EXTENSIONS, 'error_max_file_size': uploadform.fields['file'] .error_messages['file_too_large_unknown_size'] if uploadform else None, 'error_accepted_file_types': uploadform.fields['file'].error_messages['invalid_image'] if uploadform else None, })
def chooser(request): Image = get_image_model() if permission_policy.user_has_permission(request.user, 'add'): ImageForm = get_image_form(Image) uploadform = ImageForm(user=request.user) else: uploadform = None images = Image.objects.order_by('-created_at') # allow hooks to modify the queryset for hook in hooks.get_hooks('construct_image_chooser_queryset'): images = hook(images, request) 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: 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 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: searchform = SearchForm() collections = Collection.objects.all() if len(collections) < 2: collections = None paginator, images = paginate(request, images, per_page=12) return render_modal_workflow(request, 'wagtailimages/chooser/chooser.html', 'wagtailimages/chooser/chooser.js', { 'images': images, 'uploadform': uploadform, 'searchform': searchform, 'is_searching': False, 'query_string': q, 'will_select_format': request.GET.get('select_format'), 'popular_tags': popular_tags_for_model(Image), 'collections': collections, })
def index(request): Document = get_document_model() # Get documents (filtered by user permission) documents = permission_policy.instances_user_has_any_permission_for( request.user, ['change', 'delete'] ) # Ordering if 'ordering' in request.GET and request.GET['ordering'] in ['title', '-created_at']: ordering = request.GET['ordering'] else: ordering = '-created_at' documents = documents.order_by(ordering) # Filter by collection current_collection = None collection_id = request.GET.get('collection_id') if collection_id: try: current_collection = Collection.objects.get(id=collection_id) documents = documents.filter(collection=current_collection) except (ValueError, Collection.DoesNotExist): pass # Search query_string = None if 'q' in request.GET: form = SearchForm(request.GET, placeholder=_("Search documents")) if form.is_valid(): query_string = form.cleaned_data['q'] documents = documents.search(query_string) else: form = SearchForm(placeholder=_("Search documents")) # Pagination paginator, documents = paginate(request, documents) collections = permission_policy.collections_user_has_any_permission_for( request.user, ['add', 'change'] ) if len(collections) < 2: collections = None # Create response if request.is_ajax(): return render(request, 'wagtaildocs/documents/results.html', { 'ordering': ordering, 'documents': documents, 'query_string': query_string, 'is_searching': bool(query_string), }) else: return render(request, 'wagtaildocs/documents/index.html', { 'ordering': ordering, 'documents': documents, 'query_string': query_string, 'is_searching': bool(query_string), 'search_form': form, 'popular_tags': popular_tags_for_model(Document), 'user_can_add': permission_policy.user_has_permission(request.user, 'add'), 'collections': collections, 'current_collection': current_collection, })
def chooser(request): EmbedVideo = get_embed_video_model() if permission_policy.user_has_permission(request.user, 'add'): EmbedVideoForm = get_embed_video_form(EmbedVideo) uploadform = EmbedVideoForm(user=request.user) else: uploadform = None embed_videos = EmbedVideo.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): collection_id = request.GET.get('collection_id') if collection_id: embed_videos = embed_videos.filter(collection=collection_id) searchform = SearchForm(request.GET) if searchform.is_valid(): q = searchform.cleaned_data['q'] embed_videos = embed_videos.search(q) is_searching = True else: is_searching = False tag_name = request.GET.get('tag') if tag_name: embed_videos = embed_videos.filter(tags__name=tag_name) paginator, embed_videos = paginate(request, embed_videos, per_page=12) return render( request, "wagtail_embed_videos/chooser/results.html", { 'embed_videos': embed_videos, 'is_searching': is_searching, 'query_string': q, 'will_select_format': request.GET.get('select_format') }) else: searchform = SearchForm() collections = Collection.objects.all() if len(collections) < 2: collections = None paginator, embed_videos = paginate(request, embed_videos, per_page=12) return render_modal_workflow( request, 'wagtail_embed_videos/chooser/chooser.html', None, template_vars={ 'embed_videos': embed_videos, 'uploadform': uploadform, 'searchform': searchform, 'is_searching': False, 'query_string': q, 'will_select_format': request.GET.get('select_format'), 'popular_tags': popular_tags_for_model(EmbedVideo), 'collections': collections, }, json_data={'step': 'chooser'})
def index(request): Document = get_document_model() # Get documents (filtered by user permission) documents = permission_policy.instances_user_has_any_permission_for( request.user, ['change', 'delete'] ) # Ordering if 'ordering' in request.GET and request.GET['ordering'] in ['title', '-created_at']: ordering = request.GET['ordering'] else: ordering = '-created_at' documents = documents.order_by(ordering) # Filter by collection current_collection = None collection_id = request.GET.get('collection_id') if collection_id: try: current_collection = Collection.objects.get(id=collection_id) documents = documents.filter(collection=current_collection) except (ValueError, Collection.DoesNotExist): pass # Search query_string = None if 'q' in request.GET: form = SearchForm(request.GET, placeholder=_("Search documents")) if form.is_valid(): query_string = form.cleaned_data['q'] documents = documents.search(query_string) else: form = SearchForm(placeholder=_("Search documents")) # Pagination paginator = Paginator(documents, per_page=20) documents = paginator.get_page(request.GET.get('p')) collections = permission_policy.collections_user_has_any_permission_for( request.user, ['add', 'change'] ) if len(collections) < 2: collections = None else: collections = Collection.order_for_display(collections) # Create response if request.is_ajax(): return render(request, 'wagtaildocs/documents/results.html', { 'ordering': ordering, 'documents': documents, 'query_string': query_string, 'is_searching': bool(query_string), }) else: return render(request, 'wagtaildocs/documents/index.html', { 'ordering': ordering, 'documents': documents, 'query_string': query_string, 'is_searching': bool(query_string), 'search_form': form, 'popular_tags': popular_tags_for_model(Document), 'user_can_add': permission_policy.user_has_permission(request.user, 'add'), 'collections': collections, 'current_collection': current_collection, })
def chooser(request): Image = get_image_model() if permission_policy.user_has_permission(request.user, 'add'): ImageForm = get_image_form(Image) uploadform = ImageForm(user=request.user) else: uploadform = None will_select_format = request.GET.get('select_format') will_select_rendition = request.GET.get('select_rendition') images = Image.objects.order_by('-created_at') \ .filter(show_in_catalogue=True) # allow hooks to modify the queryset for hook in hooks.get_hooks('construct_image_chooser_queryset'): images = hook(images, request) 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: 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 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': will_select_format, 'will_select_rendition': will_select_rendition, 'post_processing_spec': request.GET.get('pps'), 'additional_params': get_cropper_params(request), }) else: searchform = SearchForm() collections = Collection.objects.all() if len(collections) < 2: collections = None paginator, images = paginate(request, images, per_page=12) return render_modal_workflow( request, 'wagtailimages/chooser/chooser.html', 'wagtailimages/chooser/chooser.js', { 'images': images, 'uploadform': uploadform, 'searchform': searchform, 'is_searching': False, 'query_string': q, 'will_select_format': will_select_format, 'will_select_rendition': will_select_rendition, 'popular_tags': popular_tags_for_model(Image), 'collections': collections, 'uploadid': uuid.uuid4(), 'post_processing_spec': request.GET.get('pps'), 'additional_params': get_cropper_params(request), 'allowed_extensions': ALLOWED_EXTENSIONS, 'error_max_file_size': uploadform.fields['file']. error_messages['file_too_large_unknown_size'] if uploadform else None, 'error_accepted_file_types': uploadform.fields['file'].error_messages['invalid_image'] if uploadform else None, })
def index(request): Media = get_media_model() # Get media files (filtered by user permission) media = permission_policy.instances_user_has_any_permission_for( request.user, ["change", "delete"]) # Ordering if "ordering" in request.GET and request.GET["ordering"] in [ "title", "-created_at", ]: ordering = request.GET["ordering"] else: ordering = "-created_at" media = media.order_by(ordering) # Filter by collection current_collection = None collection_id = request.GET.get("collection_id") if collection_id: try: current_collection = Collection.objects.get(id=collection_id) media = media.filter(collection=current_collection) except (ValueError, Collection.DoesNotExist): pass # Search query_string = None if "q" in request.GET: form = SearchForm(request.GET, placeholder=_("Search media files")) if form.is_valid(): query_string = form.cleaned_data["q"] media = media.search(query_string) else: form = SearchForm(placeholder=_("Search media")) # Pagination paginator, media = paginate(request, media) collections = permission_policy.collections_user_has_any_permission_for( request.user, ["add", "change"]) if len(collections) < 2: collections = None # Create response if request.is_ajax(): return render( request, "wagtailmedia/media/results.html", { "ordering": ordering, "media_files": media, "query_string": query_string, "is_searching": bool(query_string), }, ) else: return render( request, "wagtailmedia/media/index.html", { "ordering": ordering, "media_files": media, "query_string": query_string, "is_searching": bool(query_string), "search_form": form, "popular_tags": popular_tags_for_model(Media), "user_can_add": permission_policy.user_has_permission(request.user, "add"), "collections": collections, "current_collection": current_collection, }, )