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
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
76 self.import_book(file_path, options)
82 print "Results: %d files imported, %d skipped, %d total." % (
83 files_imported, files_skipped, files_imported + files_skipped)
87 transaction.leave_transaction_management()