Forráskód Böngészése

Cambios para intentar que la imagen se vea en pdf. Al arreglar que se vea en version_detail creo que se ha arreglado.

Celestino Rey 5 hónapja
szülő
commit
63637a9fc5

+ 2 - 0
K8S/trgenerator-deployment.yaml

@@ -67,6 +67,7 @@ spec:
         ports:
         - containerPort: 8000
           protocol: TCP
+
         volumeMounts:
         - mountPath: /app/mediafiles
           name: trgenerator-media
@@ -82,6 +83,7 @@ spec:
 
         - mountPath: /app/staticfiles
           name: static-volume
+          
       imagePullSecrets:
       - name: myregistrykey
       restartPolicy: Always

+ 0 - 11
nginx/default.conf

@@ -12,7 +12,6 @@ server {
     location / {
         proxy_pass http://django_project;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
-	proxy_set_header X-Real-IP $remote_addr;
         proxy_set_header Host $host;
         proxy_redirect off;
         client_max_body_size 100M;
@@ -24,16 +23,6 @@ server {
 
     location /media/ {
         alias /app/mediafiles/;
-	expires 1y;
-	add_header Cache-Control "public, max-age=31536000, immutable";
-
-	# Optimize for small files, combine small packets, and send directly
-	tcp_nodelay on;
-	tcp_nopush on;
-	sendfile on;
-
-	# Logging off for high-traffic image serving
-	access_log off;
     }
 
     error_page   500 502 503 504  /50x.html;

+ 6 - 1
src/doc_generator/settings.py

@@ -56,6 +56,7 @@ MIDDLEWARE = [
     'django.contrib.auth.middleware.AuthenticationMiddleware',
     'django.contrib.messages.middleware.MessageMiddleware',
     'django.middleware.clickjacking.XFrameOptionsMiddleware',
+    'whitenoise.middleware.WhiteNoiseMiddleware',  # For serving static files in production
 ]
 
 ROOT_URLCONF = 'doc_generator.urls'
@@ -143,7 +144,8 @@ USE_TZ = True
 STATIC_URL = '/static/'
 STATIC_ROOT = BASE_DIR / "staticfiles"
 STATICFILES_DIRS = [BASE_DIR / "static",]
-STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"
+STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'  # For production static file handling
+
 MEDIA_URL = '/media/'
 MEDIA_ROOT = BASE_DIR / "mediafiles"
 
@@ -166,4 +168,7 @@ TINYMCE_DEFAULT_CONFIG = {
     "custom_undo_redo_levels": 10,
     
     "images_upload_url": reverse_lazy('tinymce_image_upload'),
+    
+    "relative_urls": False,  # Important for correct media URL handling in PDFs
+    "remove_script_host": True,  # Important for correct media URL handling in PDFs
 }

+ 0 - 1
src/templates/textdocs/version_detail.html

@@ -30,7 +30,6 @@
             </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>

+ 0 - 14
src/textdocs/storages.py

@@ -1,14 +0,0 @@
-# textdocs/storages.py
-from whitenoise.storage import CompressedManifestStaticFilesStorage
-
-class WhiteNoiseMediaStorage(CompressedManifestStaticFilesStorage):
-    """
-    A custom storage backend for serving user-uploaded media files
-    with WhiteNoise when DEBUG is False.
-    """
-    # This is the crucial part: we tell it to use the MEDIA_ROOT
-    # and MEDIA_URL settings instead of the STATIC_* settings.
-    def __init__(self, *args, **kwargs):
-        kwargs['root'] = self.location
-        kwargs['base_url'] = self.base_url
-        super().__init__(*args, **kwargs)

+ 27 - 0
src/textdocs/utils.py

@@ -0,0 +1,27 @@
+# textdocs/utils.py
+import re
+from django.conf import settings
+
+def convert_media_urls_to_file_paths(html_string):
+    """
+    Replaces relative media URLs in an HTML string with absolute
+    file:// paths so that WeasyPrint can read images directly
+    from the shared filesystem, bypassing HTTP entirely.
+    
+    Example:
+        src="/media/tinymce_uploads/image.png"
+        becomes
+        src="file:///app/media/tinymce_uploads/image.png"
+    """
+    media_url = settings.MEDIA_URL  # e.g., '/media/'
+    media_root = str(settings.MEDIA_ROOT)  # e.g., '/app/media'
+
+    # This regex finds src attributes that start with MEDIA_URL
+    # It handles both single and double quotes
+    pattern = r'(src=["\'])' + re.escape(media_url) + r'(.*?["\'])'
+    
+    replacement = r'\1file://' + media_root + r'/\2'
+    
+    converted_html = re.sub(pattern, replacement, html_string)
+    
+    return converted_html

+ 7 - 2
src/textdocs/views.py

@@ -14,6 +14,7 @@ import os
 
 from .models import Document, DocumentVersion # Changed from Document
 from .forms import DocumentVersionForm, DocumentCreateForm # Changed from DocumentForm
+from .utils import convert_media_urls_to_file_paths # NEW: Utility function for media URL conversion
 
 @login_required
 def document_list(request):
@@ -126,8 +127,10 @@ def generate_pdf(request, pk):
     # REUSE: We can use our existing version_pdf.html template!
     html_string = render_to_string('textdocs/version_pdf.html', {'version': latest_version})
     
+    html_string = convert_media_urls_to_file_paths(html_string) # NEW: Convert media URLs to file paths for WeasyPrint
+    
     # Generate the PDF with WeasyPrint
-    html = HTML(string=html_string, base_url=request.build_absolute_uri())
+    html = HTML(string=html_string)
     pdf = html.write_pdf()
     
     # Create the HTTP response with the PDF file
@@ -151,8 +154,10 @@ def generate_version_pdf(request, pk, 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})
     
+    html_string = convert_media_urls_to_file_paths(html_string)  # Convert media URLs to file paths for WeasyPrint
+    
     # Generate the PDF with WeasyPrint
-    html = HTML(string=html_string, base_url=request.build_absolute_uri())
+    html = HTML(string=html_string)
     pdf = html.write_pdf()
     
     # Create the HTTP response with the PDF file