More Py3 compatibility fixes.
[wolnelektury.git] / src / 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.contrib.admin.views.decorators import staff_member_required
9 from django.db.models import Count
10 from django.shortcuts import render
11
12 from catalogue.models import Book, BookMedia
13 from reporting.utils import render_to_pdf, render_to_csv, generated_file_view
14
15
16 @staff_member_required
17 def stats_page(request):
18     media_types = BookMedia.objects.values('type').annotate(count=Count('type')).order_by('type')
19     for mt in media_types:
20         mt['size'] = sum(b.file.size for b in BookMedia.objects.filter(type=mt['type']).iterator())
21         if mt['type'] in ('mp3', 'ogg'):
22             deprecated = BookMedia.objects.filter(type=mt['type'], source_sha1=None)
23             mt['deprecated'] = deprecated.count()
24             mt['deprecated_files'] = deprecated.order_by('book', 'name')
25         else:
26             mt['deprecated'] = '-'
27
28     licenses = set(
29         (b.extra_info.get('license'), b.extra_info.get('license_description'))
30         for b in Book.objects.all().iterator() if b.extra_info.get('license'))
31
32     return render(request, 'reporting/main.html', {
33         'media_types': media_types,
34         'licenses': licenses,
35     })
36
37
38 @generated_file_view('reports/katalog.pdf', 'application/pdf',
39                      send_name=lambda: 'wolnelektury_%s.pdf' % date.today(), signals=[Book.published])
40 def catalogue_pdf(path):
41     books_by_author, orphans, books_by_parent = Book.book_list()
42     render_to_pdf(path, 'reporting/catalogue.texml', {
43         'books_by_author': books_by_author,
44         'orphans': orphans,
45         'books_by_parent': books_by_parent,
46     }, {
47         "wl-logo.png": os.path.join(settings.STATIC_ROOT, "img/logo-big.png"),
48     })
49
50
51 @generated_file_view('reports/katalog.csv', 'application/csv',
52                      send_name=lambda: 'wolnelektury_%s.csv' % date.today(), signals=[Book.published])
53 def catalogue_csv(path):
54     books_by_author, orphans, books_by_parent = Book.book_list()
55     render_to_csv(path, 'reporting/catalogue.csv', {
56         'books_by_author': books_by_author,
57         'orphans': orphans,
58         'books_by_parent': books_by_parent,
59     })