Fix section counting/targetting from search snippet
[wolnelektury.git] / apps / reporting / views.py
1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4 #
5 import os.path
6 from datetime import date
7 from django.conf import settings
8 from django.db.models import Count
9 from django.shortcuts import render_to_response
10 from django.template import RequestContext
11
12 from catalogue.models import Book, BookMedia
13 from reporting.utils import render_to_pdf, generated_file_view
14
15
16 def stats_page(request):
17     media = BookMedia.objects.count()
18     media_types = BookMedia.objects.values('type').\
19             annotate(count=Count('type')).\
20             order_by('type')
21     for mt in media_types:
22         mt['size'] = sum(b.file.size for b in BookMedia.objects.filter(type=mt['type']))
23         if mt['type'] in ('mp3', 'ogg'):
24             deprecated = BookMedia.objects.filter(
25                     type=mt['type'], source_sha1=None)
26             mt['deprecated'] = deprecated.count()
27             mt['deprecated_files'] = deprecated.order_by('book', 'name')
28         else:
29             mt['deprecated'] = '-'
30
31     return render_to_response('reporting/main.html',
32                 locals(), context_instance=RequestContext(request))
33
34
35 @generated_file_view('reports/katalog.pdf', 'application/pdf', 
36         send_name=lambda: 'wolnelektury_%s.pdf' % date.today(),
37         signals=[Book.published])
38 def catalogue_pdf(path):
39     books_by_author, orphans, books_by_parent = Book.book_list()
40     render_to_pdf(path, 'reporting/catalogue.texml', locals(), {
41             "wl-logo.png": os.path.join(settings.STATIC_ROOT, "img/logo-big.png"),
42         })