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.
7 from optparse import make_option
9 from django.core.management.base import BaseCommand
10 from django.db import transaction
12 from catalogue.models import Lesson, Section
13 from librarian import IOFile
16 class Command(BaseCommand):
17 option_list = BaseCommand.option_list + (
18 make_option('-q', '--quiet', action='store_false', dest='verbose', default=True,
19 help='Verbosity level; 0=minimal output, 1=normal output, 2=all output'),
20 make_option('-a', '--attachments', dest='attachments', metavar="PATH", default='materialy',
21 help='Attachments dir path.'),
22 make_option('--ignore-incomplete', action='store_true', dest='ignore_incomplete', default=False,
23 help='Attachments dir path.'),
25 help = 'Imports lessons from the specified directories.'
26 args = 'directory [directory ...]'
29 super(Command, self).__init__()
34 def all_attachments(path):
36 if not os.path.isdir(path):
40 for name in os.listdir(path):
41 fullname = os.path.join(path, name)
42 if os.path.isdir(fullname):
45 f = IOFile.from_filename(fullname)
46 files[name.decode('utf-8')] = f
47 files.setdefault(name.replace(" ", "").decode('utf-8'), f)
53 def handle(self, *directories, **options):
57 curdir = os.path.abspath(os.curdir)
58 self.options = options
63 for dir_name in directories:
64 abs_dir = os.path.join(curdir, dir_name)
65 if not os.path.isdir(abs_dir):
66 print self.style.ERROR("%s: Not a directory. Skipping." % abs_dir)
68 files_imported_dir, files_skipped_dir = self.import_from_dir(abs_dir)
69 files_imported += files_imported_dir
70 files_skipped += files_skipped_dir
73 print "Rebuilding level packages:"
74 for level in self.levels:
76 level.build_packages()
80 print "Results: %d files imported, %d skipped, %d total." % (
81 files_imported, files_skipped, files_imported + files_skipped)
84 def import_from_dir(self, abs_dir):
85 verbose = self.options.get('verbose')
88 att_dir = os.path.join(abs_dir, self.options['attachments'])
89 attachments = self.all_attachments(att_dir)
91 files = sorted(os.listdir(abs_dir))
93 ignore_incomplete = set()
95 file_name = files.pop(0)
96 file_path = os.path.join(abs_dir, file_name)
97 file_base, ext = os.path.splitext(file_path)
99 if os.path.isdir(file_path):
100 dir_imported, dir_skipped = self.import_from_dir(file_path)
101 files_imported += dir_imported
102 files_skipped += files_skipped
105 # Skip files that are not XML files
106 if not ext == '.xml':
110 print "Parsing '%s'" % file_path
112 sys.stdout.write('.')
116 iofile = IOFile.from_filename(file_path)
117 iofile.attachments = attachments
118 lesson = Lesson.publish(iofile, file_name in ignore_incomplete)
119 except Section.IncompleteError:
120 if file_name not in postponed or postponed[file_name] < files_imported:
121 # Push it back into the queue, maybe the missing lessons will show up.
123 print self.style.NOTICE('Waiting for missing lessons.')
124 files.append(file_name)
125 postponed[file_name] = files_imported
126 elif self.options['ignore_incomplete'] and file_name not in ignore_incomplete:
127 files.append(file_name)
128 ignore_incomplete.add(file_name)
129 postponed[file_name] = files_imported
131 # We're in a loop, nothing's being imported - some lesson is really missing.
133 except BaseException:
135 traceback.print_exc()
139 if hasattr(lesson, 'level'):
140 self.levels.add(lesson.level)
144 return files_imported, files_skipped