Main page.
[wolnelektury.git] / src / catalogue / management / commands / pack.py
old mode 100755 (executable)
new mode 100644 (file)
index 98ad7d8..22b36e9
@@ -1,39 +1,44 @@
-# -*- coding: utf-8 -*-
 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
 #
-from optparse import make_option
-
+import zipfile
 from django.core.management.base import BaseCommand
 from django.core.management.color import color_style
-import zipfile
-
 from catalogue.models import Book, Tag
 
 
 class Command(BaseCommand):
-    option_list = BaseCommand.option_list + (
-        make_option('-t', '--tags', dest='tags', metavar='SLUG,...',
-                    help='Use only books tagged with this tags'),
-        make_option('-i', '--include', dest='include', metavar='SLUG,...',
-                    help='Include specific books by slug'),
-        make_option('-e', '--exclude', dest='exclude', metavar='SLUG,...',
-                    help='Exclude specific books by slug')
-    )
     help = 'Prepare ZIP package with files of given type.'
-    args = '[%s] output_path.zip' % '|'.join(Book.formats)
 
-    def handle(self, ftype, path, **options):
+    def add_arguments(self, parser):
+        parser.add_argument(
+                '-t', '--tags', dest='tags', metavar='SLUG,...',
+                help='Use only books tagged with this tags')
+        parser.add_argument(
+                '-i', '--include', dest='include', metavar='SLUG,...',
+                help='Include specific books by slug')
+        parser.add_argument(
+                '-e', '--exclude', dest='exclude', metavar='SLUG,...',
+                help='Exclude specific books by slug')
+        parser.add_argument(
+                '--top-level', dest='top_level', action='store_true')
+        parser.add_argument('ftype', metavar='|'.join(Book.formats))
+        parser.add_argument('path', metavar='output_path.zip')
+
+    def handle(self, **options):
         self.style = color_style()
+        ftype = options['ftype']
+        path = options['path']
         verbose = int(options.get('verbosity'))
         tags = options.get('tags')
         include = options.get('include')
         exclude = options.get('exclude')
+        top_level = options.get('top_level')
 
         if ftype in Book.formats:
             field = "%s_file" % ftype
         else:
-            print self.style.ERROR('Unknown file type.')
+            print(self.style.ERROR('Unknown file type.'))
             return
 
         books = []
@@ -44,7 +49,10 @@ class Command(BaseCommand):
         if tags:
             books += list(Book.tagged.with_all(Tag.objects.filter(slug__in=tags.split(','))).only('slug', field))
         elif not include:
-            books = list(Book.objects.all().only('slug', field))
+            books = Book.objects.all()
+            if top_level:
+                books = books.filter(parent=None)
+            books = list(books.only('slug', field))
 
         if exclude:
             books = [book for book in books if book.slug not in exclude.split(',')]
@@ -54,11 +62,11 @@ class Command(BaseCommand):
         processed = skipped = 0
         for book in books:
             if verbose >= 2:
-                print 'Parsing', book.slug
+                print('Parsing', book.slug)
             content = getattr(book, field)
             if not content:
                 if verbose >= 1:
-                    print self.style.NOTICE('%s has no %s file' % (book.slug, ftype))
+                    print(self.style.NOTICE('%s has no %s file' % (book.slug, ftype)))
                 skipped += 1
                 continue
             archive.write(content.path, str('%s.%s' % (book.slug, ftype)))
@@ -67,11 +75,11 @@ class Command(BaseCommand):
 
         if not processed:
             if skipped:
-                print self.style.ERROR("No books with %s files found" % ftype)
+                print(self.style.ERROR("No books with %s files found" % ftype))
             else:
-                print self.style.ERROR("No books found")
+                print(self.style.ERROR("No books found"))
             return
 
         if verbose >= 1:
-            print "%d processed, %d skipped" % (processed, skipped)
-            print "Results written to %s" % path
+            print("%d processed, %d skipped" % (processed, skipped))
+            print("Results written to %s" % path)