add a filter
[wolnelektury.git] / src / reporting / views.py
1 # This file is part of Wolne Lektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Wolne Lektury. 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, Tag
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     etags = []
34     all_books = Book.objects.all()
35     all_books_count = all_books.count()
36     noparent_books = Book.objects.filter(children=None)
37     noparent_books_count = noparent_books.count()
38     for field in Book._meta.fields:
39         if not getattr(field, 'with_etag', None): continue
40         etag = field.get_current_etag()
41         d = {
42             'field': field.name,
43             'etag': etag,
44         }
45         if field.for_parents:
46             books = all_books
47             n_books = all_books_count
48         else:
49             books = noparent_books
50             n_books = noparent_books_count
51         tags = books.values_list(field.etag_field.name).order_by(
52             '-' + field.etag_field.name).distinct().annotate(c=Count('*'))
53         d['tags'] = [
54             {
55                 'tag': t[0],
56                 'count': t[1],
57                 'perc': round(100 * t[1] / n_books, 2)
58             }
59             for t in tags
60         ]
61         etags.append(d)
62
63     unused_tags = Tag.objects.exclude(category='set').filter(items=None, book=None)
64         
65     return render(request, 'reporting/main.html', {
66         'media_types': media_types,
67         'licenses': licenses,
68         'etags': etags,
69         'unused_tags': unused_tags,
70     })
71
72
73 @generated_file_view('reports/katalog.pdf', 'application/pdf',
74                      send_name=lambda: 'wolnelektury_%s.pdf' % date.today(), signals=[Book.published])
75 def catalogue_pdf(path):
76     books_by_author, orphans, books_by_parent = Book.book_list()
77     render_to_pdf(path, 'reporting/catalogue.texml', {
78         'books_by_author': books_by_author,
79         'orphans': orphans,
80         'books_by_parent': books_by_parent,
81     }, {
82         "wl-logo.png": os.path.join(settings.STATIC_ROOT, "img/logo-big.png"),
83     })
84
85
86 @generated_file_view('reports/katalog.csv', 'application/csv',
87                      send_name=lambda: 'wolnelektury_%s.csv' % date.today(), signals=[Book.published])
88 def catalogue_csv(path):
89     books_by_author, orphans, books_by_parent = Book.book_list()
90     render_to_csv(path, 'reporting/catalogue.csv', {
91         'books_by_author': books_by_author,
92         'orphans': orphans,
93         'books_by_parent': books_by_parent,
94     })