def chooser(request): Document = get_document_model() if permission_policy.user_has_permission(request.user, 'add'): DocumentForm = get_document_form(Document) uploadform = DocumentForm(user=request.user) else: uploadform = None documents = Document.objects.all() # allow hooks to modify the queryset for hook in hooks.get_hooks('construct_document_chooser_queryset'): documents = hook(documents, request) q = None if 'q' in request.GET or 'p' in request.GET or 'collection_id' in request.GET: collection_id = request.GET.get('collection_id') if collection_id: documents = documents.filter(collection=collection_id) searchform = SearchForm(request.GET) if searchform.is_valid(): q = searchform.cleaned_data['q'] documents = documents.search(q) is_searching = True else: documents = documents.order_by('-created_at') is_searching = False # Pagination paginator, documents = paginate(request, documents, per_page=10) return render( request, "wagtaildocs/chooser/results.html", { 'documents': documents, 'query_string': q, 'is_searching': is_searching, }) else: searchform = SearchForm() collections = Collection.objects.all() if len(collections) < 2: collections = None documents = documents.order_by('-created_at') paginator, documents = paginate(request, documents, per_page=10) return render_modal_workflow( request, 'wagtaildocs/chooser/chooser.html', 'wagtaildocs/chooser/chooser.js', { 'documents': documents, 'uploadform': uploadform, 'searchform': searchform, 'collections': collections, 'is_searching': False, })
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) collections = permission_policy.collections_user_has_any_permission_for( self.request.user, ["add", "change"]) if len(collections) < 2: collections = None Document = get_document_model() context.update({ "search_form": self.form, "popular_tags": popular_tags_for_model(get_document_model()), "user_can_add": permission_policy.user_has_permission(self.request.user, "add"), "collections": collections, "current_collection": self.current_collection, "app_label": Document._meta.app_label, "model_name": Document._meta.model_name, }) return context
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) collections = permission_policy.collections_user_has_any_permission_for( self.request.user, ['add', 'change']) if len(collections) < 2: collections = None Document = get_document_model() context.update({ 'search_form': self.form, 'popular_tags': popular_tags_for_model(get_document_model()), 'user_can_add': permission_policy.user_has_permission(self.request.user, 'add'), 'collections': collections, 'current_collection': self.current_collection, 'app_label': Document._meta.app_label, 'model_name': Document._meta.model_name, }) return context
def chooser(request): Document = get_document_model() if permission_policy.user_has_permission(request.user, 'add'): DocumentForm = get_document_form(Document) uploadform = DocumentForm(user=request.user) else: uploadform = None documents = Document.objects.all() # allow hooks to modify the queryset for hook in hooks.get_hooks('construct_document_chooser_queryset'): documents = hook(documents, request) q = None if 'q' in request.GET or 'p' in request.GET or 'collection_id' in request.GET: collection_id = request.GET.get('collection_id') if collection_id: documents = documents.filter(collection=collection_id) searchform = SearchForm(request.GET) if searchform.is_valid(): q = searchform.cleaned_data['q'] documents = documents.search(q) is_searching = True else: documents = documents.order_by('-created_at') is_searching = False # Pagination paginator, documents = paginate(request, documents, per_page=10) return render(request, "wagtaildocs/chooser/results.html", { 'documents': documents, 'query_string': q, 'is_searching': is_searching, }) else: searchform = SearchForm() collections = Collection.objects.all() if len(collections) < 2: collections = None documents = documents.order_by('-created_at') paginator, documents = paginate(request, documents, per_page=10) return render_modal_workflow(request, 'wagtaildocs/chooser/chooser.html', 'wagtaildocs/chooser/chooser.js', { 'documents': documents, 'uploadform': uploadform, 'searchform': searchform, 'collections': collections, 'is_searching': False, 'uploadid': uuid.uuid4(), })
def get(self, request): Document = get_document_model() if permission_policy.user_has_permission(request.user, 'add'): DocumentForm = get_document_form(Document) self.uploadform = DocumentForm(user=request.user, prefix='document-chooser-upload') else: self.uploadform = None documents = permission_policy.instances_user_has_any_permission_for( request.user, ['choose']) # allow hooks to modify the queryset for hook in hooks.get_hooks('construct_document_chooser_queryset'): documents = hook(documents, request) self.q = None self.is_searching = False self.collection_id = request.GET.get('collection_id') if self.collection_id: documents = documents.filter(collection=self.collection_id) self.documents_exist = documents.exists() if 'q' in request.GET: self.searchform = SearchForm(request.GET) if self.searchform.is_valid(): self.q = self.searchform.cleaned_data['q'] documents = documents.search(self.q) self.is_searching = True else: self.searchform = SearchForm() if not self.is_searching: documents = documents.order_by('-created_at') paginator = Paginator(documents, per_page=10) self.documents = paginator.get_page(request.GET.get('p')) self.collections = permission_policy.collections_user_has_permission_for( request.user, 'choose') if len(self.collections) < 2: self.collections = None return self.render_to_response()
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 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): Document = get_document_model() if permission_policy.user_has_permission(request.user, 'add'): DocumentForm = get_document_form(Document) uploadform = DocumentForm(user=request.user, prefix='document-chooser-upload') else: uploadform = None documents = permission_policy.instances_user_has_any_permission_for( request.user, ['choose']) # allow hooks to modify the queryset for hook in hooks.get_hooks('construct_document_chooser_queryset'): documents = hook(documents, request) q = None if 'q' in request.GET or 'p' in request.GET or 'collection_id' in request.GET: collection_id = request.GET.get('collection_id') if collection_id: documents = documents.filter(collection=collection_id) documents_exist = documents.exists() searchform = SearchForm(request.GET) if searchform.is_valid(): q = searchform.cleaned_data['q'] documents = documents.search(q) is_searching = True else: documents = documents.order_by('-created_at') is_searching = False # Pagination paginator = Paginator(documents, per_page=10) documents = paginator.get_page(request.GET.get('p')) return TemplateResponse( request, "wagtaildocs/chooser/results.html", { 'documents': documents, 'documents_exist': documents_exist, 'uploadform': uploadform, 'query_string': q, 'is_searching': is_searching, 'collection_id': collection_id, }) else: searchform = SearchForm() collections = permission_policy.collections_user_has_permission_for( request.user, 'choose') if len(collections) < 2: collections = None documents = documents.order_by('-created_at') documents_exist = documents.exists() paginator = Paginator(documents, per_page=10) documents = paginator.get_page(request.GET.get('p')) return render_modal_workflow(request, 'wagtaildocs/chooser/chooser.html', None, { 'documents': documents, 'documents_exist': documents_exist, 'uploadform': uploadform, 'searchform': searchform, 'collections': collections, 'is_searching': False, }, json_data=get_chooser_context())
def get(self, request): Document = get_document_model() if permission_policy.user_has_permission(request.user, "add"): DocumentForm = get_document_form(Document) self.uploadform = DocumentForm( user=request.user, prefix="document-chooser-upload" ) else: self.uploadform = None documents = permission_policy.instances_user_has_any_permission_for( request.user, ["choose"] ) # allow hooks to modify the queryset for hook in hooks.get_hooks("construct_document_chooser_queryset"): documents = hook(documents, request) self.q = None self.is_searching = False self.collection_id = request.GET.get("collection_id") if self.collection_id: documents = documents.filter(collection=self.collection_id) self.documents_exist = documents.exists() if "q" in request.GET: self.searchform = SearchForm(request.GET) if self.searchform.is_valid(): self.q = self.searchform.cleaned_data["q"] documents = documents.search(self.q) self.is_searching = True else: self.searchform = SearchForm() if not self.is_searching: documents = documents.order_by("-created_at") paginator = Paginator(documents, per_page=10) self.documents = paginator.get_page(request.GET.get("p")) self.collections = permission_policy.collections_user_has_permission_for( request.user, "choose" ) if len(self.collections) < 2: self.collections = None columns = [ TitleColumn( "title", label=_("Title"), url_name="wagtaildocs:document_chosen", link_classname="document-choice", ), DownloadColumn("filename", label=_("File")), DateColumn("created_at", label=_("Created"), width="16%"), ] if self.collections: columns.insert(2, Column("collection", label=_("Collection"))) self.table = Table(columns, self.documents) return self.render_to_response()