1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
6 from django.conf import settings
7 from django.core.management.base import BaseCommand
8 from django.core.management.color import color_style
9 from django.core.files import File
10 from django.db import transaction
11 from librarian.picture import ImageStore
13 from catalogue.models import Book
14 from picture.models import Picture
17 class Command(BaseCommand):
18 help = 'Imports books from the specified directories.'
20 def add_arguments(self, parser):
22 '-q', '--quiet', action='store_false', dest='verbose', default=True,
23 help='Verbosity level; 0=minimal output, 1=normal output, 2=all output')
25 '-f', '--force', action='store_true', dest='force',
26 default=False, help='Overwrite works already in the catalogue')
28 '-D', '--dont-build', dest='dont_build', metavar="FORMAT,...",
29 help="Skip building specified formats")
31 '-F', '--not-findable', action='store_false',
32 dest='findable', default=True,
33 help='Set book as not findable.')
35 '-p', '--picture', action='store_true', dest='import_picture',
36 default=False, help='Import pictures')
37 parser.add_argument('directory', nargs='+')
39 def import_book(self, file_path, options):
40 verbose = options.get('verbose')
41 if options.get('dont_build'):
42 dont_build = options.get('dont_build').lower().split(',')
45 file_base, ext = os.path.splitext(file_path)
46 book = Book.from_xml_file(file_path, overwrite=options.get('force'),
47 dont_build=dont_build,
48 findable=options.get('findable'),
49 remote_gallery_url='file://' + os.path.dirname(os.path.abspath(file_base)) + '/img/'
51 for ebook_format in Book.ebook_formats:
52 if os.path.isfile(file_base + '.' + ebook_format):
53 getattr(book, '%s_file' % ebook_format).save(
54 '%s.%s' % (book.slug, ebook_format),
55 File(file(file_base + '.' + ebook_format)),
59 print("Importing %s.%s" % (file_base, ebook_format))
62 def import_picture(self, file_path, options, continue_on_error=True):
64 image_store = ImageStore(os.path.dirname(file_path))
65 picture = Picture.from_xml_file(file_path, image_store=image_store, overwrite=options.get('force'))
66 except Exception as ex:
68 print("%s: %s" % (file_path, ex))
75 def handle(self, **options):
76 self.style = color_style()
78 verbose = options.get('verbose')
79 import_picture = options.get('import_picture')
84 for dir_name in options['directory']:
85 if not os.path.isdir(dir_name):
86 print(self.style.ERROR("%s: Not a directory. Skipping." % dir_name))
89 files = sorted(os.listdir(dir_name))
92 file_name = files.pop(0)
93 file_path = os.path.join(dir_name, file_name)
94 file_base, ext = os.path.splitext(file_path)
96 # Skip files that are not XML files
101 print("Parsing '%s'" % file_path)
103 sys.stdout.write('.')
109 self.import_picture(file_path, options)
111 self.import_book(file_path, options)
115 except (Book.AlreadyExists, Picture.AlreadyExists):
116 print(self.style.ERROR(
117 '%s: Book or Picture already imported. Skipping. To overwrite use --force.' %
121 except Book.DoesNotExist as e:
122 if file_name not in postponed or postponed[file_name] < files_imported:
123 # push it back into the queue, maybe the missing child will show up
125 print(self.style.NOTICE('Waiting for missing children'))
126 files.append(file_name)
127 postponed[file_name] = files_imported
129 # we're in a loop, nothing's being imported - some child is really missing
134 print("Results: %d files imported, %d skipped, %d total." % (
135 files_imported, files_skipped, files_imported + files_skipped))