From: Marcin Koziej Date: Thu, 24 May 2012 13:20:22 +0000 (+0200) Subject: CSV raport X-Git-Url: https://git.mdrn.pl/wolnelektury.git/commitdiff_plain/d713c61258bf1fcebea7e21065ac2878f5bf45be?ds=inline;hp=c0fe1c29e6a99ca35360bfdae886b2181e00b4fd CSV raport --- diff --git a/apps/catalogue/templatetags/catalogue_tags.py b/apps/catalogue/templatetags/catalogue_tags.py index 4f84dadca..139aa54ce 100644 --- a/apps/catalogue/templatetags/catalogue_tags.py +++ b/apps/catalogue/templatetags/catalogue_tags.py @@ -55,7 +55,6 @@ def html_title_from_tags(tags): template.render(Context({'tag': tag, 'category': _(tag.category)})) 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, - "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) +@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 = [] diff --git a/apps/reporting/templates/reporting/catalogue.csv b/apps/reporting/templates/reporting/catalogue.csv new file mode 100755 index 000000000..eb2ebd982 --- /dev/null +++ b/apps/reporting/templates/reporting/catalogue.csv @@ -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 %} diff --git a/apps/reporting/templates/reporting/catalogue.texml b/apps/reporting/templates/reporting/catalogue.texml index c17ed0754..d6b081f6e 100755 --- a/apps/reporting/templates/reporting/catalogue.texml +++ b/apps/reporting/templates/reporting/catalogue.texml @@ -107,7 +107,7 @@ LetterSpace=-1.0 {{ author }} - {% book_tree_texml group books_by_parent %} + {% book_tree_texml author group books_by_parent %} {% endif %} {% endfor %} diff --git a/apps/reporting/urls.py b/apps/reporting/urls.py index f50921513..cb5998783 100755 --- a/apps/reporting/urls.py +++ b/apps/reporting/urls.py @@ -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'), + url(r'^katalog.csv$', 'catalogue_csv', name='reporting_catalogue_csv'), ) diff --git a/apps/reporting/utils.py b/apps/reporting/utils.py index cc4e97a29..a8a6bb1dd 100755 --- a/apps/reporting/utils.py +++ b/apps/reporting/utils.py @@ -59,6 +59,27 @@ def render_to_pdf(output_path, template, context=None, add_files=None): 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: diff --git a/apps/reporting/views.py b/apps/reporting/views.py index cb87f8122..961a682a1 100644 --- a/apps/reporting/views.py +++ b/apps/reporting/views.py @@ -11,7 +11,7 @@ from django.shortcuts import render_to_response 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 @@ -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"), }) + + +@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())