X-Git-Url: https://git.mdrn.pl/edumed.git/blobdiff_plain/39844fcccd43fc77f6beb350f5792bb0859135df..5c7bc1d4380a5c51f13aa274cbea211e4a444a9e:/catalogue/management/commands/importlessons.py diff --git a/catalogue/management/commands/importlessons.py b/catalogue/management/commands/importlessons.py index 6efd246..87fd16e 100755 --- a/catalogue/management/commands/importlessons.py +++ b/catalogue/management/commands/importlessons.py @@ -11,7 +11,8 @@ from django.core.management.base import BaseCommand from django.core.management.color import color_style from django.core.files import File -from catalogue.models import Lesson +from librarian import IOFile +from catalogue.models import Lesson, Section #from search import Index @@ -20,14 +21,35 @@ class Command(BaseCommand): option_list = BaseCommand.option_list + ( make_option('-q', '--quiet', action='store_false', dest='verbose', default=True, help='Verbosity level; 0=minimal output, 1=normal output, 2=all output'), + make_option('-a', '--attachments', dest='attachments', metavar="PATH", default='materialy', + help='Attachments dir path.'), ) help = 'Imports lessons from the specified directories.' args = 'directory [directory ...]' - def import_book(self, file_path, options): + def import_book(self, file_path, options, attachments): verbose = options.get('verbose') - with open(file_path) as f: - lesson = Lesson.publish(f) + iofile = IOFile.from_filename(file_path) + iofile.attachments = attachments + lesson = Lesson.publish(iofile) + + @staticmethod + def all_attachments(path): + files = {} + + def read_dir(path): + for name in os.listdir(path): + fullname = os.path.join(path, name) + if os.path.isdir(fullname): + read_dir(fullname) + else: + f = IOFile.from_filename(fullname) + files[name] = f + files.setdefault(name.replace(" ", ""), f) + + read_dir(path) + return files + def handle(self, *directories, **options): from django.db import transaction @@ -48,6 +70,9 @@ class Command(BaseCommand): if not os.path.isdir(dir_name): print self.style.ERROR("%s: Not a directory. Skipping." % dir_name) else: + att_dir = os.path.join(dir_name, options['attachments']) + attachments = self.all_attachments(att_dir) + # files queue files = sorted(os.listdir(dir_name)) postponed = {} @@ -67,9 +92,24 @@ class Command(BaseCommand): sys.stdout.flush() # Import book files - self.import_book(file_path, options) - files_imported += 1 - transaction.commit() + try: + self.import_book(file_path, options, attachments) + files_imported += 1 + transaction.commit() + except Section.IncompleteError, e: + if file_name not in postponed or postponed[file_name] < files_imported: + # Push it back into the queue, maybe the missing lessons will show up. + if verbose > 0: + print self.style.NOTICE('Waiting for missing lessons.') + files.append(file_name) + postponed[file_name] = files_imported + else: + # We're in a loop, nothing's being imported - some lesson is really missing. + raise e + except BaseException, e: + import traceback + traceback.print_exc() + files_skipped += 1 # Print results print