|
|
@@ -7,59 +7,96 @@ from django.template.loader import render_to_string
|
|
|
from weasyprint import HTML
|
|
|
from django.contrib.auth.decorators import login_required
|
|
|
|
|
|
-from .models import Document
|
|
|
-from .forms import DocumentForm
|
|
|
+from .models import Document, DocumentVersion # Changed from Document
|
|
|
+from .forms import DocumentVersionForm, DocumentCreateForm # Changed from DocumentForm
|
|
|
|
|
|
@login_required
|
|
|
def document_list(request):
|
|
|
"""Shows a list of all created documents."""
|
|
|
- documents = Document.objects.order_by('-created_at')
|
|
|
+ documents = Document.objects.filter(author=request.user).order_by('-created_at')
|
|
|
return render(request, 'textdocs/document_list.html', {'documents': documents})
|
|
|
|
|
|
@login_required
|
|
|
def document_detail(request, pk):
|
|
|
"""Shows the details of a single document."""
|
|
|
- document = get_object_or_404(Document, pk=pk)
|
|
|
+ document = get_object_or_404(Document, pk=pk, author=request.user)
|
|
|
return render(request, 'textdocs/document_detail.html', {'document': document})
|
|
|
|
|
|
@login_required
|
|
|
def document_create(request):
|
|
|
- """Handles the creation of a new document via a form."""
|
|
|
+ """
|
|
|
+ Handles the creation of a new parent Document and its first version.
|
|
|
+ This is a two-form process now, but we'll combine them logically.
|
|
|
+ """
|
|
|
if request.method == 'POST':
|
|
|
- print("Method is POST. Processing form data.") # Log POST request
|
|
|
-
|
|
|
- form = DocumentForm(request.POST)
|
|
|
- if form.is_valid():
|
|
|
- document = form.save()
|
|
|
- return redirect('document_detail', pk=document.pk)
|
|
|
+ title_form = DocumentCreateForm(request.POST)
|
|
|
+ content_form = DocumentVersionForm(request.POST)
|
|
|
+ if title_form.is_valid() and content_form.is_valid():
|
|
|
+ # Create the parent Document
|
|
|
+ document = Document.objects.create(
|
|
|
+ author=request.user,
|
|
|
+ title=title_form.cleaned_data['title']
|
|
|
+ )
|
|
|
+ # Create the first version
|
|
|
+ version = content_form.save(commit=False)
|
|
|
+ version.document = document
|
|
|
+ version.version_number = 1
|
|
|
+ version.save()
|
|
|
+ return redirect(document.get_absolute_url())
|
|
|
else:
|
|
|
- form = DocumentForm()
|
|
|
- return render(request, 'textdocs/document_form.html', {'form': form})
|
|
|
+ title_form = DocumentCreateForm()
|
|
|
+ content_form = DocumentVersionForm()
|
|
|
+
|
|
|
+ return render(request, 'textdocs/document_form.html', {
|
|
|
+ 'title_form': title_form,
|
|
|
+ 'content_form': content_form
|
|
|
+ })
|
|
|
+
|
|
|
|
|
|
-# --- NEW: View for updating a document ---
|
|
|
@login_required
|
|
|
-def document_update(request, pk):
|
|
|
- """Handles editing and saving an existing document."""
|
|
|
- document = get_object_or_404(Document, pk=pk)
|
|
|
+def document_edit(request, pk):
|
|
|
+ """
|
|
|
+ Handles creating a NEW version of an existing document.
|
|
|
+ This replaces the old 'update' view.
|
|
|
+ """
|
|
|
+ document = get_object_or_404(Document, pk=pk, author=request.user)
|
|
|
+ latest_version = document.latest_version
|
|
|
+
|
|
|
if request.method == 'POST':
|
|
|
- # Pass the instance to the form to update it
|
|
|
- form = DocumentForm(request.POST, instance=document)
|
|
|
+ form = DocumentVersionForm(request.POST)
|
|
|
if form.is_valid():
|
|
|
- form.save()
|
|
|
- return redirect('document_detail', pk=document.pk)
|
|
|
+ new_version = form.save(commit=False)
|
|
|
+ new_version.document = document
|
|
|
+ # Increment the version number
|
|
|
+ new_version.version_number = latest_version.version_number + 1
|
|
|
+ new_version.save()
|
|
|
+ return redirect(document.get_absolute_url())
|
|
|
else:
|
|
|
- # Pre-populate the form with the existing document's data
|
|
|
- form = DocumentForm(instance=document)
|
|
|
- return render(request, 'textdocs/document_form.html', {'form': form})
|
|
|
+ # Pre-populate the form with the content of the latest version
|
|
|
+ form = DocumentVersionForm(instance=latest_version)
|
|
|
+
|
|
|
+ return render(request, 'textdocs/document_edit_form.html', {
|
|
|
+ 'form': form,
|
|
|
+ 'document': document
|
|
|
+ })
|
|
|
+
|
|
|
+# NEW VIEW: To see old versions
|
|
|
+@login_required
|
|
|
+def view_version(request, pk, version_number):
|
|
|
+ document = get_object_or_404(Document, pk=pk, author=request.user)
|
|
|
+ version = get_object_or_404(DocumentVersion, document=document, version_number=version_number)
|
|
|
+ return render(request, 'textdocs/version_detail.html', {'version': version})
|
|
|
|
|
|
# --- NEW: View for deleting a document ---
|
|
|
@login_required
|
|
|
def document_delete(request, pk):
|
|
|
- """Handles the deletion of a document after confirmation."""
|
|
|
- document = get_object_or_404(Document, pk=pk)
|
|
|
+ """Handles the deletion of a document and all its versions."""
|
|
|
+ document = get_object_or_404(Document, pk=pk, author=request.user)
|
|
|
if request.method == 'POST':
|
|
|
# This is after the user confirms the deletion
|
|
|
- document.delete()
|
|
|
+ document.versions.all().delete() # First delete all versions
|
|
|
+ document.delete() # Then delete the document itself
|
|
|
+
|
|
|
return redirect('document_list') # Redirect to the list after deletion
|
|
|
|
|
|
# If it's a GET request, show the confirmation page
|
|
|
@@ -67,8 +104,8 @@ def document_delete(request, pk):
|
|
|
|
|
|
@login_required
|
|
|
def generate_pdf(request, pk):
|
|
|
- """Generates a PDF for a specific document."""
|
|
|
- document = get_object_or_404(Document, pk=pk)
|
|
|
+ """Generates a PDF for a specific version of a document."""
|
|
|
+ document = get_object_or_404(Document, pk=pk, author=request.user)
|
|
|
html_string = render_to_string('textdocs/document_pdf.html', {'document': document})
|
|
|
html = HTML(string=html_string, base_url=request.build_absolute_uri())
|
|
|
pdf = html.write_pdf()
|