1 # -*- coding: utf-8 -*-
2 # This file is part of EduMed, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
8 from optparse import make_option
9 from django.conf import settings
10 from django.core.management.base import BaseCommand
11 from django.core.management.color import color_style
12 from django.core.files import File
14 from librarian import IOFile
15 from catalogue.models import Lesson, Section
17 #from search import Index
20 class Command(BaseCommand):
21 option_list = BaseCommand.option_list + (
22 make_option('-q', '--quiet', action='store_false', dest='verbose', default=True,
23 help='Verbosity level; 0=minimal output, 1=normal output, 2=all output'),
25 help = 'Imports lessons from the specified directories.'
26 args = 'directory [directory ...]'
28 def import_book(self, file_path, options):
29 verbose = options.get('verbose')
30 iofile = IOFile.from_filename(file_path)
31 basename, ext = file_path.rsplit('.', 1)
32 if os.path.isdir(basename):
33 for att_name in os.listdir(basename):
34 iofile.attachments[att_name] = IOFile.from_filename(
35 os.path.join(basename, att_name))
36 lesson = Lesson.publish(iofile)
38 def handle(self, *directories, **options):
39 from django.db import transaction
41 self.style = color_style()
43 verbose = options.get('verbose')
45 # Start transaction management.
46 transaction.commit_unless_managed()
47 transaction.enter_transaction_management()
48 transaction.managed(True)
53 for dir_name in directories:
54 if not os.path.isdir(dir_name):
55 print self.style.ERROR("%s: Not a directory. Skipping." % dir_name)
58 files = sorted(os.listdir(dir_name))
61 file_name = files.pop(0)
62 file_path = os.path.join(dir_name, file_name)
63 file_base, ext = os.path.splitext(file_path)
65 # Skip files that are not XML files
70 print "Parsing '%s'" % file_path
77 self.import_book(file_path, options)
80 except Section.IncompleteError:
81 if file_name not in postponed or postponed[file_name] < files_imported:
82 # Push it back into the queue, maybe the missing lessons will show up.
84 print self.style.NOTICE('Waiting for missing lessons.')
85 files.append(file_name)
86 postponed[file_name] = files_imported
88 # We're in a loop, nothing's being imported - some lesson is really missing.
90 except BaseException, e:
97 print "Results: %d files imported, %d skipped, %d total." % (
98 files_imported, files_skipped, files_imported + files_skipped)
102 transaction.leave_transaction_management()