X-Git-Url: https://git.mdrn.pl/edumed.git/blobdiff_plain/996389725d454494a8e0046f6b7b4f03d9407463..6c9978c5bf8b1ef20871557c22b8b02df647d700:/catalogue/management/commands/importlessons.py diff --git a/catalogue/management/commands/importlessons.py b/catalogue/management/commands/importlessons.py index 1d65d18..e4c09e6 100755 --- a/catalogue/management/commands/importlessons.py +++ b/catalogue/management/commands/importlessons.py @@ -4,62 +4,80 @@ # import os import sys -import time from optparse import make_option -from django.conf import settings + from django.core.management.base import BaseCommand from django.core.management.color import color_style -from django.core.files import File +from django.db import transaction -from librarian import IOFile from catalogue.models import Lesson, Section +from librarian import IOFile -#from search import Index +# from search import Index 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'), + 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.'), + make_option('--ignore-incomplete', action='store_true', dest='ignore_incomplete', default=False, + 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, ignore_incomplete=False): verbose = options.get('verbose') - iofile = IOFile.from_filename(file_path) - basename, ext = file_path.rsplit('.', 1) - if os.path.isdir(basename): - for att_name in os.listdir(basename): - iofile.attachments[att_name] = IOFile.from_filename( - os.path.join(basename, att_name)) - lesson = Lesson.publish(iofile) + iofile = IOFile.from_filename(os.path.join(self.curdir, file_path)) + iofile.attachments = attachments + return Lesson.publish(iofile, ignore_incomplete) + + @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.decode('utf-8')] = f + files.setdefault(name.replace(" ", "").decode('utf-8'), f) + + read_dir(path) + return files + + @transaction.atomic def handle(self, *directories, **options): - from django.db import transaction + levels = set() self.style = color_style() verbose = options.get('verbose') - - # Start transaction management. - transaction.commit_unless_managed() - transaction.enter_transaction_management() - transaction.managed(True) + self.curdir = os.path.abspath(os.curdir) files_imported = 0 files_skipped = 0 for dir_name in directories: - if not os.path.isdir(dir_name): - print self.style.ERROR("%s: Not a directory. Skipping." % dir_name) + abs_dir = os.path.join(self.curdir, dir_name) + if not os.path.isdir(abs_dir): + print self.style.ERROR("%s: Not a directory. Skipping." % abs_dir) else: + att_dir = os.path.join(abs_dir, options['attachments']) + attachments = self.all_attachments(att_dir) + # files queue - files = sorted(os.listdir(dir_name)) + files = sorted(os.listdir(abs_dir)) postponed = {} + ignore_incomplete = set() while files: file_name = files.pop(0) - file_path = os.path.join(dir_name, file_name) + file_path = os.path.join(abs_dir, file_name) file_base, ext = os.path.splitext(file_path) # Skip files that are not XML files @@ -74,28 +92,43 @@ class Command(BaseCommand): # Import book files try: - self.import_book(file_path, options) - files_imported += 1 - transaction.commit() - except Section.IncompleteError: + lesson = self.import_book( + file_path, options, attachments, + ignore_incomplete=file_name in ignore_incomplete) + 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 + elif options['ignore_incomplete'] and file_name not in ignore_incomplete: + files.append(file_name) + ignore_incomplete.add(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: - print e + except BaseException: + import traceback + traceback.print_exc() files_skipped += 1 + else: + files_imported += 1 + if hasattr(lesson, 'level'): + levels.add(lesson.level) + finally: + if verbose > 0: + print + + if levels: + print "Rebuilding level packages:" + for level in levels: + print level.name + level.build_packages() # Print results print print "Results: %d files imported, %d skipped, %d total." % ( files_imported, files_skipped, files_imported + files_skipped) print - - transaction.commit() - transaction.leave_transaction_management()