Browse Source

html2pdf now search static files in app statix folder. This allow to gen pdf in dev mode without collectstatic

Fabs 11 years ago
parent
commit
0fbd278da7
1 changed files with 43 additions and 30 deletions
  1. 43 30
      coin/html2pdf.py

+ 43 - 30
coin/html2pdf.py

@@ -1,44 +1,57 @@
 # -*- coding: utf-8 -*-
 # -*- coding: utf-8 -*-
 import os
 import os
+import re
 from django.conf import settings
 from django.conf import settings
 from tempfile import NamedTemporaryFile
 from tempfile import NamedTemporaryFile
 from xhtml2pdf import pisa
 from xhtml2pdf import pisa
 from django.template import loader, Context
 from django.template import loader, Context
 
 
-# Convert HTML URIs to absolute system paths so xhtml2pdf can access
-# those resources
+
 def link_callback(uri, rel):
 def link_callback(uri, rel):
-	sUrl = settings.STATIC_URL     # Typically /static/
-	sRoot = settings.STATIC_ROOT   # Typically /home/userX/project_static/
-	mUrl = settings.MEDIA_URL      # Typically /static/media/
-	mRoot = settings.MEDIA_ROOT    # Typically /home/userX/project_static/media/
-
-	# convert URIs to absolute system paths
-	if uri.startswith(mUrl):
-	    path = os.path.join(mRoot, uri.replace(mUrl, ""))
-	elif uri.startswith(sUrl):
-	    path = os.path.join(sRoot, uri.replace(sUrl, ""))
-	# make sure that file exists
-	if not os.path.isfile(path):
-	        raise Exception(
-	                'media URI must start with %s or %s' % \
-	                (sUrl, mUrl))
-	return path
+    """
+    # Convert HTML URIs to absolute system paths so xhtml2pdf can access
+    # those resources
+    """
+    sUrl = settings.STATIC_URL    # Typically /static/
+    sRoot = settings.STATIC_ROOT  # Typically /home/userX/project_static/
+    mUrl = settings.MEDIA_URL     # Typically /static/media/
+    mRoot = settings.MEDIA_ROOT   # Typically /home/userX/project_static/media/
+    projectDir = settings.PROJECT_PATH  # Typically /home/userX/project/
+
+    # convert URIs to absolute system paths
+    if uri.startswith(mUrl):
+        path = os.path.join(mRoot, uri.replace(mUrl, ""))
+    elif uri.startswith(sUrl):
+        path = os.path.join(sRoot, uri.replace(sUrl, ""))
+
+    # If file doesn't exist try to find it in app static folder
+    # This case occur in developpement env
+    if not os.path.isfile(path):
+        app_search = re.search(r'^(%s|%s)(.*)/.*' % (sUrl, mUrl), uri)
+        app = app_search.group(2)
+        path = os.path.join(projectDir, app, uri[1:])
+
+    # make sure that file exists
+    if not os.path.isfile(path):
+        raise Exception(
+            'media URI must start with %s or %s' %
+            (sUrl, mUrl))
+    return path
 
 
 
 
 def render_as_pdf(template, context):
 def render_as_pdf(template, context):
-	"""
-	Génére le template donné avec les données du context en HTML et le
-	converti en PDF via le module xhtml2pdf
-	"""
-	template = loader.get_template(template)
-	html = template.render(Context(context))
-	file = NamedTemporaryFile()
+    """
+    Génére le template indiqué avec les données du context en HTML et le
+    converti en PDF via le module xhtml2pdf
+    """
+    template = loader.get_template(template)
+    html = template.render(Context(context))
+    file = NamedTemporaryFile()
 
 
-	pisaStatus = pisa.CreatePDF(html, dest=file, link_callback=link_callback)
+    pisaStatus = pisa.CreatePDF(html, dest=file, link_callback=link_callback)
 
 
-	file.seek(0)
-	pdf = file.read()
-	file.close()
+    file.seek(0)
+    pdf = file.read()
+    file.close()
 
 
-	return pdf
+    return pdf