Update NOTICE.
[wolnelektury.git] / apps / reporting / utils.py
index cc4e97a..8ecb9b0 100755 (executable)
@@ -30,7 +30,7 @@ def render_to_pdf(output_path, template, context=None, add_files=None):
 
     rendered = render_to_string(template, context)
     texml = StringIO(rendered.encode('utf-8'))
 
     rendered = render_to_string(template, context)
     texml = StringIO(rendered.encode('utf-8'))
-    tempdir = mkdtemp(prefix = "render_to_pdf-")
+    tempdir = mkdtemp(prefix="render_to_pdf-")
     tex_path = os.path.join(tempdir, "doc.tex")
     with open(tex_path, 'w') as tex_file:
         Texml.processor.process(texml, tex_file, encoding="utf-8")
     tex_path = os.path.join(tempdir, "doc.tex")
     with open(tex_path, 'w') as tex_file:
         Texml.processor.process(texml, tex_file, encoding="utf-8")
@@ -59,6 +59,27 @@ def render_to_pdf(output_path, template, context=None, add_files=None):
         shutil.rmtree(tempdir)
 
 
         shutil.rmtree(tempdir)
 
 
+def render_to_csv(output_path, template, context=None, add_files=None):
+    """Renders a TeXML document into a PDF file.
+
+    :param str output_path: is where the PDF file should go
+    :param str template: is a TeXML template path
+    :param context: is context for rendering the template
+    :param dict add_files: a dictionary of additional files XeTeX will need
+    """
+
+    from django.template.loader import render_to_string
+
+    try:
+        os.makedirs(os.path.dirname(output_path))
+    except:
+        pass
+
+    rendered = render_to_string(template, context)
+    with open(output_path, 'w') as csv_file:
+        csv_file.write(rendered.encode('utf-8'))
+
+
 def read_chunks(f, size=8192):
     chunk = f.read(size)
     while chunk:
 def read_chunks(f, size=8192):
     chunk = f.read(size)
     while chunk:
@@ -68,7 +89,6 @@ def read_chunks(f, size=8192):
 
 def generated_file_view(file_name, mime_type, send_name=None, signals=None):
     file_path = os.path.join(settings.MEDIA_ROOT, file_name)
 
 def generated_file_view(file_name, mime_type, send_name=None, signals=None):
     file_path = os.path.join(settings.MEDIA_ROOT, file_name)
-    file_url = os.path.join(settings.MEDIA_URL, file_name)
     if send_name is None:
         send_name = os.path.basename(file_name)
 
     if send_name is None:
         send_name = os.path.basename(file_name)
 
@@ -93,7 +113,7 @@ def generated_file_view(file_name, mime_type, send_name=None, signals=None):
             else:
                 name = send_name
 
             else:
                 name = send_name
 
-            response = HttpResponse(mimetype=mime_type)
+            response = HttpResponse(content_type=mime_type)
             response['Content-Disposition'] = 'attachment; filename=%s' % name
             with open(file_path) as f:
                 for chunk in read_chunks(f):
             response['Content-Disposition'] = 'attachment; filename=%s' % name
             with open(file_path) as f:
                 for chunk in read_chunks(f):