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):
38 for name in os.listdir(path):
39 fullname = os.path.join(path, name)
40 if os.path.isdir(fullname):
43 f = IOFile.from_filename(fullname)
44 files[name.decode('utf-8')] = f
45 files.setdefault(name.replace(" ", "").decode('utf-8'), f)
51 def handle(self, *directories, **options):
55 curdir = os.path.abspath(os.curdir)
56 self.options = options
61 for dir_name in directories:
62 abs_dir = os.path.join(curdir, dir_name)
63 if not os.path.isdir(abs_dir):
64 print self.style.ERROR("%s: Not a directory. Skipping." % abs_dir)
66 files_imported_dir, files_skipped_dir = self.import_from_dir(abs_dir)
67 files_imported += files_imported_dir
68 files_skipped += files_skipped_dir
71 print "Rebuilding level packages:"
72 for level in self.levels:
74 level.build_packages()
78 print "Results: %d files imported, %d skipped, %d total." % (
79 files_imported, files_skipped, files_imported + files_skipped)
82 def import_from_dir(self, abs_dir):
83 verbose = self.options.get('verbose')
86 att_dir = os.path.join(abs_dir, self.options['attachments'])
87 attachments = self.all_attachments(att_dir)
89 files = sorted(os.listdir(abs_dir))
91 ignore_incomplete = set()
93 file_name = files.pop(0)
94 file_path = os.path.join(abs_dir, file_name)
95 file_base, ext = os.path.splitext(file_path)
97 # Skip files that are not XML files
102 print "Parsing '%s'" % file_path
104 sys.stdout.write('.')
108 iofile = IOFile.from_filename(file_path)
109 iofile.attachments = attachments
110 lesson = Lesson.publish(iofile, file_name in ignore_incomplete)
111 except Section.IncompleteError:
112 if file_name not in postponed or postponed[file_name] < files_imported:
113 # Push it back into the queue, maybe the missing lessons will show up.
115 print self.style.NOTICE('Waiting for missing lessons.')
116 files.append(file_name)
117 postponed[file_name] = files_imported
118 elif self.options['ignore_incomplete'] and file_name not in ignore_incomplete:
119 files.append(file_name)
120 ignore_incomplete.add(file_name)
121 postponed[file_name] = files_imported
123 # We're in a loop, nothing's being imported - some lesson is really missing.
125 except BaseException:
127 traceback.print_exc()
131 if hasattr(lesson, 'level'):
132 self.levels.add(lesson.level)
136 return files_imported, files_skipped