CSV raport
authorMarcin Koziej <marcin.koziej@nowoczesnapolska.org.pl>
Thu, 24 May 2012 13:20:22 +0000 (15:20 +0200)
committerMarcin Koziej <marcin.koziej@nowoczesnapolska.org.pl>
Thu, 24 May 2012 13:20:40 +0000 (15:20 +0200)
apps/catalogue/templatetags/catalogue_tags.py
apps/reporting/templates/reporting/catalogue.csv [new file with mode: 0755]
apps/reporting/templates/reporting/catalogue.texml
apps/reporting/urls.py
apps/reporting/utils.py
apps/reporting/views.py

index 4f84dad..139aa54 100644 (file)
@@ -55,7 +55,6 @@ def html_title_from_tags(tags):
         template.render(Context({'tag': tag, 'category': _(tag.category)})) for tag in tags))
     
 
         template.render(Context({'tag': tag, 'category': _(tag.category)})) for tag in tags))
     
 
-
 def simple_title(tags):
     title = []
     for tag in tags:
 def simple_title(tags):
     title = []
     for tag in tags:
@@ -169,13 +168,36 @@ def book_tree_texml(book_list, books_by_parent, depth=1):
             %(children)s
             """ % {
                 "depth": depth,
             %(children)s
             """ % {
                 "depth": depth,
-                "title": book.title, 
+                "title": book.title,
                 "audiences": ", ".join(book.audiences_pl()),
                 "audiobook": "audiobook" if book.has_media('mp3') else "",
                 "children": book_tree_texml(books_by_parent.get(book.id, ()), books_by_parent, depth + 1)
             } for book in book_list)
 
 
                 "audiences": ", ".join(book.audiences_pl()),
                 "audiobook": "audiobook" if book.has_media('mp3') else "",
                 "children": book_tree_texml(books_by_parent.get(book.id, ()), books_by_parent, depth + 1)
             } for book in book_list)
 
 
+@register.simple_tag
+def book_tree_csv(author, book_list, books_by_parent, depth=1, max_depth=3, delimeter="\t"):
+    def quote_if_necessary(s):
+        try:
+            s.index(delimeter)
+            s.replace('"', '\\"')
+            return '"%s"' % s
+        except ValueError:
+            return s
+        
+    return "".join("""%(author)s%(d)s%(preindent)s%(title)s%(d)s%(postindent)s%(audiences)s%(d)s%(audiobook)s
+%(children)s""" % {
+                "d": delimeter,
+                "preindent": delimeter * (depth - 1),
+                "postindent": delimeter * (max_depth - depth), 
+                "depth": depth,
+                "author": quote_if_necessary(author.name),
+                "title": quote_if_necessary(book.title),
+                "audiences": ", ".join(book.audiences_pl()),
+                "audiobook": "audiobook" if book.has_media('mp3') else "",
+                "children": book_tree_csv(author, books_by_parent.get(book.id, ()), books_by_parent, depth + 1)
+            } for book in book_list)
+
 @register.simple_tag
 def all_editors(extra_info):
     editors = []
 @register.simple_tag
 def all_editors(extra_info):
     editors = []
diff --git a/apps/reporting/templates/reporting/catalogue.csv b/apps/reporting/templates/reporting/catalogue.csv
new file mode 100755 (executable)
index 0000000..eb2ebd9
--- /dev/null
@@ -0,0 +1 @@
+{% load catalogue_tags %}{% book_tree_texml "" orphans books_by_parent %}{% for author, group in books_by_author.items %}{% if group %}{% book_tree_csv author group books_by_parent %}{% endif %}{% endfor %}
index c17ed07..d6b081f 100755 (executable)
@@ -107,7 +107,7 @@ LetterSpace=-1.0
                     <cmd name="name"><parm>{{ author }}</parm></cmd>
                     <ctrl ch='\' />
 
                     <cmd name="name"><parm>{{ author }}</parm></cmd>
                     <ctrl ch='\' />
 
-                    {% book_tree_texml group books_by_parent %}
+                    {% book_tree_texml author group books_by_parent %}
                 {% endif %}
             {% endfor %}
         </TeXML>
                 {% endif %}
             {% endfor %}
         </TeXML>
index f509215..cb59987 100755 (executable)
@@ -8,5 +8,6 @@ from django.conf.urls.defaults import *
 urlpatterns = patterns('reporting.views',
     url(r'^$', 'stats_page', name='reporting_stats'),
     url(r'^katalog.pdf$', 'catalogue_pdf', name='reporting_catalogue_pdf'),
 urlpatterns = patterns('reporting.views',
     url(r'^$', 'stats_page', name='reporting_stats'),
     url(r'^katalog.pdf$', 'catalogue_pdf', name='reporting_catalogue_pdf'),
+    url(r'^katalog.csv$', 'catalogue_csv', name='reporting_catalogue_csv'),
 )
 
 )
 
index cc4e97a..a8a6bb1 100755 (executable)
@@ -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:
index cb87f81..961a682 100644 (file)
@@ -11,7 +11,7 @@ from django.shortcuts import render_to_response
 from django.template import RequestContext
 
 from catalogue.models import Book, BookMedia
 from django.template import RequestContext
 
 from catalogue.models import Book, BookMedia
-from reporting.utils import render_to_pdf, generated_file_view
+from reporting.utils import render_to_pdf, render_to_csv, generated_file_view
 
 
 @staff_member_required
 
 
 @staff_member_required
@@ -42,3 +42,11 @@ def catalogue_pdf(path):
     render_to_pdf(path, 'reporting/catalogue.texml', locals(), {
             "wl-logo.png": os.path.join(settings.STATIC_ROOT, "img/logo-big.png"),
         })
     render_to_pdf(path, 'reporting/catalogue.texml', locals(), {
             "wl-logo.png": os.path.join(settings.STATIC_ROOT, "img/logo-big.png"),
         })
+
+
+@generated_file_view('reports/katalog.csv', 'application/csv', 
+        send_name=lambda: 'wolnelektury_%s.csv' % date.today(),
+        signals=[Book.published])
+def catalogue_csv(path):
+    books_by_author, orphans, books_by_parent = Book.book_list()
+    render_to_csv(path, 'reporting/catalogue.csv', locals())