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 catalogue.models import Lesson
16 #from search import Index
19 class Command(BaseCommand):
20 option_list = BaseCommand.option_list + (
21 make_option('-q', '--quiet', action='store_false', dest='verbose', default=True,
22 help='Verbosity level; 0=minimal output, 1=normal output, 2=all output'),
24 help = 'Imports lessons from the specified directories.'
25 args = 'directory [directory ...]'
27 def import_book(self, file_path, options):
28 verbose = options.get('verbose')
29 with open(file_path) as f:
30 lesson = Lesson.publish(f)
32 def handle(self, *directories, **options):
33 from django.db import transaction
35 self.style = color_style()
37 verbose = options.get('verbose')
39 # Start transaction management.
40 transaction.commit_unless_managed()
41 transaction.enter_transaction_management()
42 transaction.managed(True)
47 for dir_name in directories:
48 if not os.path.isdir(dir_name):
49 print self.style.ERROR("%s: Not a directory. Skipping." % dir_name)
52 files = sorted(os.listdir(dir_name))
55 file_name = files.pop(0)
56 file_path = os.path.join(dir_name, file_name)
57 file_base, ext = os.path.splitext(file_path)
59 # Skip files that are not XML files
64 print "Parsing '%s'" % file_path
70 self.import_book(file_path, options)
76 print "Results: %d files imported, %d skipped, %d total." % (
77 files_imported, files_skipped, files_imported + files_skipped)
81 transaction.leave_transaction_management()