|
@@ -1,5 +1,7 @@
|
|
|
# textdocs/views.py
|
|
# textdocs/views.py
|
|
|
|
|
+
|
|
|
from django.shortcuts import render, get_object_or_404, redirect
|
|
from django.shortcuts import render, get_object_or_404, redirect
|
|
|
|
|
+from django.urls import reverse_lazy # NEW: Useful for delete success URL
|
|
|
from django.http import HttpResponse
|
|
from django.http import HttpResponse
|
|
|
from django.template.loader import render_to_string
|
|
from django.template.loader import render_to_string
|
|
|
from weasyprint import HTML
|
|
from weasyprint import HTML
|
|
@@ -28,18 +30,39 @@ def document_create(request):
|
|
|
form = DocumentForm()
|
|
form = DocumentForm()
|
|
|
return render(request, 'textdocs/document_form.html', {'form': form})
|
|
return render(request, 'textdocs/document_form.html', {'form': form})
|
|
|
|
|
|
|
|
|
|
+# --- NEW: View for updating a document ---
|
|
|
|
|
+def document_update(request, pk):
|
|
|
|
|
+ """Handles editing and saving an existing document."""
|
|
|
|
|
+ document = get_object_or_404(Document, pk=pk)
|
|
|
|
|
+ if request.method == 'POST':
|
|
|
|
|
+ # Pass the instance to the form to update it
|
|
|
|
|
+ form = DocumentForm(request.POST, instance=document)
|
|
|
|
|
+ if form.is_valid():
|
|
|
|
|
+ form.save()
|
|
|
|
|
+ return redirect('document_detail', pk=document.pk)
|
|
|
|
|
+ else:
|
|
|
|
|
+ # Pre-populate the form with the existing document's data
|
|
|
|
|
+ form = DocumentForm(instance=document)
|
|
|
|
|
+ return render(request, 'textdocs/document_form.html', {'form': form})
|
|
|
|
|
+
|
|
|
|
|
+# --- NEW: View for deleting a document ---
|
|
|
|
|
+def document_delete(request, pk):
|
|
|
|
|
+ """Handles the deletion of a document after confirmation."""
|
|
|
|
|
+ document = get_object_or_404(Document, pk=pk)
|
|
|
|
|
+ if request.method == 'POST':
|
|
|
|
|
+ # This is after the user confirms the deletion
|
|
|
|
|
+ document.delete()
|
|
|
|
|
+ return redirect('document_list') # Redirect to the list after deletion
|
|
|
|
|
+
|
|
|
|
|
+ # If it's a GET request, show the confirmation page
|
|
|
|
|
+ return render(request, 'textdocs/document_confirm_delete.html', {'document': document})
|
|
|
|
|
+
|
|
|
def generate_pdf(request, pk):
|
|
def generate_pdf(request, pk):
|
|
|
"""Generates a PDF for a specific document."""
|
|
"""Generates a PDF for a specific document."""
|
|
|
document = get_object_or_404(Document, pk=pk)
|
|
document = get_object_or_404(Document, pk=pk)
|
|
|
-
|
|
|
|
|
- # Render the HTML template with the document context
|
|
|
|
|
html_string = render_to_string('textdocs/document_pdf.html', {'document': document})
|
|
html_string = render_to_string('textdocs/document_pdf.html', {'document': document})
|
|
|
-
|
|
|
|
|
- # Use WeasyPrint to create the PDF
|
|
|
|
|
html = HTML(string=html_string, base_url=request.build_absolute_uri())
|
|
html = HTML(string=html_string, base_url=request.build_absolute_uri())
|
|
|
pdf = html.write_pdf()
|
|
pdf = html.write_pdf()
|
|
|
-
|
|
|
|
|
- # Create an HTTP response with the PDF
|
|
|
|
|
response = HttpResponse(pdf, content_type='application/pdf')
|
|
response = HttpResponse(pdf, content_type='application/pdf')
|
|
|
response['Content-Disposition'] = f'attachment; filename="{document.title}.pdf"'
|
|
response['Content-Disposition'] = f'attachment; filename="{document.title}.pdf"'
|
|
|
return response
|
|
return response
|