def chooser_upload(request): if request.POST: document = Document(uploaded_by_user=request.user) form = DocumentForm(request.POST, request.FILES, instance=document) if form.is_valid(): form.save() # Reindex the document to make sure all tags are indexed for backend in get_search_backends(): backend.add(document) return render_modal_workflow( request, None, 'wagtaildocs/chooser/document_chosen.js', {'document_json': get_document_json(document)} ) else: form = DocumentForm() documents = Document.objects.order_by('title') return render_modal_workflow( request, 'wagtaildocs/chooser/chooser.html', 'wagtaildocs/chooser/chooser.js', {'documents': documents, 'uploadform': form} )
def edit(request, document_id): doc = get_object_or_404(Document, id=document_id) if not doc.is_editable_by_user(request.user): raise PermissionDenied if request.POST: original_file = doc.file form = DocumentForm(request.POST, request.FILES, instance=doc) if form.is_valid(): if 'file' in form.changed_data: # if providing a new document file, delete the old one. # 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) doc = form.save() messages.success(request, _("Document '{0}' updated").format(doc.title)) return redirect('wagtaildocs_index') else: messages.error(request, _("The document could not be saved due to errors.")) else: form = DocumentForm(instance=doc) return render(request, "wagtaildocs/documents/edit.html", { 'document': doc, 'form': form, })
def edit(request, document_id): doc = get_object_or_404(Document, id=document_id) if not doc.is_editable_by_user(request.user): raise PermissionDenied if request.POST: original_file = doc.file form = DocumentForm(request.POST, request.FILES, instance=doc) if form.is_valid(): if 'file' in form.changed_data: # if providing a new document file, delete the old one. # 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) doc = form.save() # Reindex the document to make sure all tags are indexed for backend in get_search_backends(): backend.add(doc) messages.success(request, _("Document '{0}' updated").format(doc.title), buttons=[ messages.button(reverse('wagtaildocs:edit', args=(doc.id,)), _('Edit')) ]) return redirect('wagtaildocs:index') else: messages.error(request, _("The document could not be saved due to errors.")) else: form = DocumentForm(instance=doc) filesize = None # Get file size when there is a file associated with the Document object if doc.file: try: filesize = doc.file.size except OSError: # File doesn't exist pass if not filesize: messages.error( request, _("The file could not be found. Please change the source or delete the document"), buttons=[messages.button(reverse('wagtaildocs:delete', args=(doc.id,)), _('Delete'))] ) return render(request, "wagtaildocs/documents/edit.html", { 'document': doc, 'filesize': filesize, 'form': form })
def add(request): if request.POST: doc = Document(uploaded_by_user=request.user) form = DocumentForm(request.POST, request.FILES, instance=doc) if form.is_valid(): form.save() messages.success(request, _("Document '{0}' added.").format(doc.title)) return redirect('wagtaildocs_index') else: messages.error(request, _("The document could not be saved due to errors.")) else: form = DocumentForm() return render(request, "wagtaildocs/documents/add.html", { 'form': form, })
def chooser(request): if request.user.has_perm('wagtaildocs.add_document'): uploadform = DocumentForm() else: uploadform = None documents = [] q = None is_searching = False if 'q' in request.GET or 'p' in request.GET: searchform = SearchForm(request.GET) if searchform.is_valid(): q = searchform.cleaned_data['q'] documents = Document.objects.search(q) is_searching = True else: documents = Document.objects.order_by('-created_at') is_searching = False # Pagination p = request.GET.get("p", 1) paginator = Paginator(documents, 10) try: documents = paginator.page(p) except PageNotAnInteger: documents = paginator.page(1) except EmptyPage: documents = paginator.page(paginator.num_pages) return render( request, "wagtaildocs/chooser/results.html", { 'documents': documents, 'query_string': q, 'is_searching': is_searching, }) else: searchform = SearchForm() documents = Document.objects.order_by('-created_at') p = request.GET.get("p", 1) paginator = Paginator(documents, 10) try: documents = paginator.page(p) except PageNotAnInteger: documents = paginator.page(1) except EmptyPage: documents = paginator.page(paginator.num_pages) return render_modal_workflow( request, 'wagtaildocs/chooser/chooser.html', 'wagtaildocs/chooser/chooser.js', { 'documents': documents, 'uploadform': uploadform, 'searchform': searchform, 'is_searching': False, })
def chooser_upload(request): if request.POST: document = Document(uploaded_by_user=request.user) form = DocumentForm(request.POST, request.FILES, instance=document) if form.is_valid(): form.save() # Reindex the document to make sure all tags are indexed for backend in get_search_backends(): backend.add(document) document_json = json.dumps({ 'id': document.id, 'title': document.title }) return render_modal_workflow( request, None, 'wagtaildocs/chooser/document_chosen.js', {'document_json': document_json}) else: form = DocumentForm() documents = Document.objects.order_by('title') return render_modal_workflow(request, 'wagtaildocs/chooser/chooser.html', 'wagtaildocs/chooser/chooser.js', { 'documents': documents, 'uploadform': form })
def add(request): if request.POST: doc = Document(uploaded_by_user=request.user) form = DocumentForm(request.POST, request.FILES, instance=doc) if form.is_valid(): form.save() # Reindex the document to make sure all tags are indexed for backend in get_search_backends(): backend.add(doc) messages.success(request, _("Document '{0}' added.").format(doc.title), buttons=[ messages.button( reverse('wagtaildocs:edit', args=(doc.id, )), _('Edit')) ]) return redirect('wagtaildocs:index') else: messages.error(request, _("The document could not be saved due to errors.")) else: form = DocumentForm() return render(request, "wagtaildocs/documents/add.html", { 'form': form, })
def add(request): if request.POST: doc = Document(uploaded_by_user=request.user) form = DocumentForm(request.POST, request.FILES, instance=doc) if form.is_valid(): form.save() # Reindex the document to make sure all tags are indexed for backend in get_search_backends(): backend.add(doc) messages.success(request, _("Document '{0}' added.").format(doc.title)) return redirect("wagtaildocs_index") else: messages.error(request, _("The document could not be saved due to errors.")) else: form = DocumentForm() return render(request, "wagtaildocs/documents/add.html", {"form": form})
def chooser_upload(request): if request.POST: document = Document(uploaded_by_user=request.user) form = DocumentForm(request.POST, request.FILES, instance=document) if form.is_valid(): form.save() document_json = json.dumps({'id': document.id, 'title': document.title}) return render_modal_workflow( request, None, 'wagtaildocs/chooser/document_chosen.js', {'document_json': document_json} ) else: form = DocumentForm() documents = Document.objects.order_by('title') return render_modal_workflow( request, 'wagtaildocs/chooser/chooser.html', 'wagtaildocs/chooser/chooser.js', {'documents': documents, 'uploadform': form} )
def edit(request, document_id): doc = get_object_or_404(Document, id=document_id) if not doc.is_editable_by_user(request.user): raise PermissionDenied if request.POST: original_file = doc.file form = DocumentForm(request.POST, request.FILES, instance=doc) if form.is_valid(): if 'file' in form.changed_data: # if providing a new document file, delete the old one. # 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) doc = form.save() # Reindex the document to make sure all tags are indexed for backend in get_search_backends(): backend.add(doc) messages.success(request, _("Document '{0}' updated").format(doc.title), buttons=[ messages.button(reverse('wagtaildocs_edit_document', args=(doc.id,)), _('Edit')) ]) return redirect('wagtaildocs_index') else: messages.error(request, _("The document could not be saved due to errors.")) else: form = DocumentForm(instance=doc) return render(request, "wagtaildocs/documents/edit.html", { 'document': doc, 'form': form })
def edit(request, document_id): doc = get_object_or_404(Document, id=document_id) if not doc.is_editable_by_user(request.user): raise PermissionDenied if request.POST: original_file = doc.file form = DocumentForm(request.POST, request.FILES, instance=doc) if form.is_valid(): if 'file' in form.changed_data: # if providing a new document file, delete the old one. # 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) doc = form.save() messages.success(request, "Document '%s' updated" % doc.title) return redirect('wagtaildocs_index') else: messages.error(request, "The document could not be saved due to errors.") else: form = DocumentForm(instance=doc) return render(request, "wagtaildocs/documents/edit.html", { 'document': doc, 'form': form, })
def chooser_upload(request): if request.POST: document = Document(uploaded_by_user=request.user) form = DocumentForm(request.POST, request.FILES, instance=document) if form.is_valid(): form.save() document_json = json.dumps({"id": document.id, "title": document.title}) return render_modal_workflow( request, None, "wagtaildocs/chooser/document_chosen.js", {"document_json": document_json} ) else: form = DocumentForm() documents = Document.objects.order_by("title") return render_modal_workflow( request, "wagtaildocs/chooser/chooser.html", "wagtaildocs/chooser/chooser.js", {"documents": documents, "uploadform": form}, )
def add(request): if request.POST: doc = Document(uploaded_by_user=request.user) form = DocumentForm(request.POST, request.FILES, instance=doc) if form.is_valid(): form.save() # Reindex the document to make sure all tags are indexed for backend in get_search_backends(): backend.add(doc) messages.success(request, _("Document '{0}' added.").format(doc.title), buttons=[ messages.button(reverse('wagtaildocs_edit_document', args=(doc.id,)), _('Edit')) ]) return redirect('wagtaildocs_index') else: messages.error(request, _("The document could not be saved due to errors.")) else: form = DocumentForm() return render(request, "wagtaildocs/documents/add.html", { 'form': form, })
def add(request): if request.POST: doc = Document(uploaded_by_user=request.user) form = DocumentForm(request.POST, request.FILES, instance=doc) if form.is_valid(): form.save() messages.success(request, "Document '%s' added." % doc.title) return redirect('wagtaildocs_index') else: messages.error(request, "The document could not be saved due to errors.") else: form = DocumentForm() return render(request, "wagtaildocs/documents/add.html", { 'form': form, })
def chooser(request): if request.user.has_perm('wagtaildocs.add_document'): uploadform = DocumentForm() else: uploadform = None documents = [] q = None is_searching = False if 'q' in request.GET or 'p' in request.GET: searchform = SearchForm(request.GET) if searchform.is_valid(): q = searchform.cleaned_data['q'] documents = Document.objects.search(q) is_searching = True else: documents = Document.objects.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() documents = Document.objects.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, 'is_searching': False, })