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