1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
5 from django.core.management.base import BaseCommand
6 from django.core.management.color import color_style
7 from catalogue.models import Book, Tag
10 class Command(BaseCommand):
11 help = 'Prepare ZIP package with files of given type.'
13 def add_arguments(self, parser):
15 '-t', '--tags', dest='tags', metavar='SLUG,...',
16 help='Use only books tagged with this tags')
18 '-i', '--include', dest='include', metavar='SLUG,...',
19 help='Include specific books by slug')
21 '-e', '--exclude', dest='exclude', metavar='SLUG,...',
22 help='Exclude specific books by slug')
24 '--top-level', dest='top_level', action='store_true')
25 parser.add_argument('ftype', metavar='|'.join(Book.formats))
26 parser.add_argument('path', metavar='output_path.zip')
28 def handle(self, **options):
29 self.style = color_style()
30 ftype = options['ftype']
31 path = options['path']
32 verbose = int(options.get('verbosity'))
33 tags = options.get('tags')
34 include = options.get('include')
35 exclude = options.get('exclude')
36 top_level = options.get('top_level')
38 if ftype in Book.formats:
39 field = "%s_file" % ftype
41 print(self.style.ERROR('Unknown file type.'))
47 books += list(Book.objects.filter(slug__in=include.split(',')).only('slug', field))
50 books += list(Book.tagged.with_all(Tag.objects.filter(slug__in=tags.split(','))).only('slug', field))
52 books = Book.objects.all()
54 books = books.filter(parent=None)
55 books = list(books.only('slug', field))
58 books = [book for book in books if book.slug not in exclude.split(',')]
60 archive = zipfile.ZipFile(path, 'w', zipfile.ZIP_DEFLATED)
62 processed = skipped = 0
65 print('Parsing', book.slug)
66 content = getattr(book, field)
69 print(self.style.NOTICE('%s has no %s file' % (book.slug, ftype)))
72 archive.write(content.path, str('%s.%s' % (book.slug, ftype)))
78 print(self.style.ERROR("No books with %s files found" % ftype))
80 print(self.style.ERROR("No books found"))
84 print("%d processed, %d skipped" % (processed, skipped))
85 print("Results written to %s" % path)