Merge remote-tracking branch 'zawadzki/new-design'
[wolnelektury.git] / src / reporting / views.py
1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
3 #
4 import os.path
5 from datetime import date
6 from django.conf import settings
7 from django.contrib.admin.views.decorators import staff_member_required
8 from django.db.models import Count
9 from django.shortcuts import render
10
11 from catalogue.models import Book, BookMedia
12 from reporting.utils import render_to_pdf, render_to_csv, generated_file_view
13
14
15 @staff_member_required
16 def stats_page(request):
17     media_types = BookMedia.objects.values('type').annotate(count=Count('type')).order_by('type')
18     for mt in media_types:
19         mt['size'] = sum(b.file.size for b in BookMedia.objects.filter(type=mt['type']).iterator())
20         if mt['type'] in ('mp3', 'ogg'):
21             deprecated = BookMedia.objects.filter(type=mt['type'], source_sha1=None)
22             mt['deprecated'] = deprecated.count()
23             mt['deprecated_files'] = deprecated.order_by('book', 'name')
24         else:
25             mt['deprecated'] = '-'
26
27     licenses = set()
28     for b in Book.objects.all().iterator():
29         extra_info = b.get_extra_info_json()
30         if extra_info.get('license'):
31             licenses.add((extra_info.get('license'), extra_info.get('license_description')))
32
33     return render(request, 'reporting/main.html', {
34         'media_types': media_types,
35         'licenses': licenses,
36     })
37
38
39 @generated_file_view('reports/katalog.pdf', 'application/pdf',
40                      send_name=lambda: 'wolnelektury_%s.pdf' % date.today(), signals=[Book.published])
41 def catalogue_pdf(path):
42     books_by_author, orphans, books_by_parent = Book.book_list()
43     render_to_pdf(path, 'reporting/catalogue.texml', {
44         'books_by_author': books_by_author,
45         'orphans': orphans,
46         'books_by_parent': books_by_parent,
47     }, {
48         "wl-logo.png": os.path.join(settings.STATIC_ROOT, "img/logo-big.png"),
49     })
50
51
52 @generated_file_view('reports/katalog.csv', 'application/csv',
53                      send_name=lambda: 'wolnelektury_%s.csv' % date.today(), signals=[Book.published])
54 def catalogue_csv(path):
55     books_by_author, orphans, books_by_parent = Book.book_list()
56     render_to_csv(path, 'reporting/catalogue.csv', {
57         'books_by_author': books_by_author,
58         'orphans': orphans,
59         'books_by_parent': books_by_parent,
60     })