Răsfoiți Sursa

Funcionamiento básico con versiones

Celestino Rey 5 luni în urmă
părinte
comite
346542342e

+ 4 - 1
src/templates/textdocs/base.html

@@ -3,7 +3,10 @@
 <head>
     <title>{% block title %}Technical Report Generator{% endblock %}</title>
     <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
-    {{ form.media }}
+    <!-- This block will render the necessary JS/CSS for form widgets like TinyMCE -->
+    {% block form_media %}
+        {{ form.media }}
+    {% endblock %}
 </head>
 <body>
     <div class="container mt-4">

+ 5 - 0
src/templates/textdocs/document_form.html

@@ -1,5 +1,10 @@
 {% extends 'textdocs/base.html' %}
 
+{% block form_media %}
+    {{ title_form.media }}
+    {{ content_form.media }}
+{% endblock %}
+
 {% block title %}Create a New Document{% endblock %}
 
 {% block content %}

+ 8 - 6
src/templates/textdocs/version_detail.html

@@ -4,13 +4,11 @@
 
 {% block content %}
 
-    <!-- Navigation to get back to the main document page -->
     <a href="{{ version.document.get_absolute_url }}" class="btn btn-secondary mb-3">‹ Back to Latest Version</a>
 
-    <!-- A warning banner to prevent confusion -->
     {% if version.version_number != version.document.latest_version.version_number %}
     <div class="alert alert-warning" role="alert">
-        <strong>Heads up!</strong> You are viewing an older version of this document (v{{ version.version_number }}). The content may be out of date.
+        <strong>Heads up!</strong> You are viewing an older version of this document (v{{ version.version_number }}).
     </div>
     {% endif %}
 
@@ -22,13 +20,17 @@
                     <h5 class="card-subtitle text-success">
                         Viewing Version {{ version.version_number }}
                     </h5>
-                    <h6 class="card-subtitle text-muted mt-1">
-                        Created by {{ version.document.author.username }} on {{ version.created_at|date:"F d, Y" }}
-                    </h6>
+                </div>
+                <!-- --- NEW BUTTON --- -->
+                <div>
+                    <a href="{% url 'generate_version_pdf' pk=version.document.pk version_number=version.version_number %}" class="btn btn-info">
+                        Export This Version to PDF
+                    </a>
                 </div>
             </div>
         </div>
         <div class="card-body">
+            <!-- ... (rest of the template is the same) ... -->
             <h4 class="card-title">Introduction</h4>
             <div>{{ version.introduction|safe }}</div>
             <hr>

+ 61 - 0
src/templates/textdocs/version_pdf.html

@@ -0,0 +1,61 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <title>{{ version.document.title }}</title>
+    <style>
+        @page {
+            size: A4;
+            margin: 2cm;
+            @bottom-right {
+                content: "Page " counter(page) " of " counter(pages);
+                font-size: 10pt;
+            }
+        }
+        body {
+            font-family: "Helvetica", sans-serif;
+            font-size: 12pt;
+            line-height: 1.6;
+        }
+        h1, h2, h3, h4 {
+            font-family: "Times New Roman", serif;
+            color: #005595; /* Siemens-like blue */
+        }
+        h1 {
+            font-size: 24pt;
+            text-align: center;
+            margin-bottom: 0.5cm;
+        }
+        .version-subtitle {
+            font-size: 14pt;
+            text-align: center;
+            color: #555;
+            margin-bottom: 2cm;
+        }
+        h2 {
+            font-size: 18pt;
+            border-bottom: 2px solid #009999; /* Siemens-like teal */
+            padding-bottom: 5px;
+            margin-top: 1.5cm;
+        }
+        p {
+            text-align: justify;
+        }
+    </style>
+</head>
+<body>
+    <!-- Use the parent document's title -->
+    <h1>{{ version.document.title }}</h1>
+    <!-- Add a clear subtitle for the version number -->
+    <div class="version-subtitle">Version {{ version.version_number }}</div>
+
+    <h2>Introduction</h2>
+    <div>{{ version.introduction|safe }}</div>
+
+    <h2>Main Body</h2>
+    <div>{{ version.main_body|safe }}</div>
+
+    <h2>Conclusion</h2>
+    <div>{{ version.conclusion|safe }}</div>
+</body>
+</html>

+ 4 - 3
src/textdocs/urls.py

@@ -6,10 +6,11 @@ urlpatterns = [
     path('', views.document_list, name='document_list'),
     path('document/new/', views.document_create, name='document_create'),
     path('document/<int:pk>/', views.document_detail, name='document_detail'),
+    path('document/<int:pk>/version/<int:version_number>/', views.view_version, name='view_version'),
+
     path('document/<int:pk>/pdf/', views.generate_pdf, name='generate_pdf'),
-    
-    # --- NEW URLS for Edit and Delete ---
+    path('document/<int:pk>/version/<int:version_number>/pdf/', views.generate_version_pdf, name='generate_version_pdf'),
+
     path('document/<int:pk>/edit/', views.document_edit, name='document_edit'),
-    path('document/<int:pk>/version/<int:version_number>/', views.view_version, name='view_version'),
     path('document/<int:pk>/delete/', views.document_delete, name='document_delete'),
 ]

+ 25 - 0
src/textdocs/views.py

@@ -111,4 +111,29 @@ def generate_pdf(request, pk):
     pdf = html.write_pdf()
     response = HttpResponse(pdf, content_type='application/pdf')
     response['Content-Disposition'] = f'attachment; filename="{document.title}.pdf"'
+    return response
+
+@login_required
+def generate_version_pdf(request, pk, version_number):
+    """Generates a PDF for a SPECIFIC historical version of a document."""
+    # First, get the parent document to ensure ownership
+    document = get_object_or_404(Document, pk=pk, author=request.user)
+    
+    # Now, get the specific version associated with that document
+    version = get_object_or_404(DocumentVersion, document=document, version_number=version_number)
+    
+    # We'll create a dedicated PDF template for versions to keep things clean
+    html_string = render_to_string('textdocs/version_pdf.html', {'version': version})
+    
+    # Generate the PDF with WeasyPrint
+    html = HTML(string=html_string, base_url=request.build_absolute_uri())
+    pdf = html.write_pdf()
+    
+    # Create the HTTP response with the PDF file
+    response = HttpResponse(pdf, content_type='application/pdf')
+    
+    # Make the filename include the version number for clarity
+    filename = f'{version.document.title}_v{version.version_number}.pdf'
+    response['Content-Disposition'] = f'attachment; filename="{filename}"'
+    
     return response