From: Radek Czajka Date: Mon, 11 Feb 2013 15:53:27 +0000 (+0100) Subject: Loading materials from a dir. X-Git-Url: https://git.mdrn.pl/edumed.git/commitdiff_plain/f8b34544329e05b7c27990ffe9c241df877cd95c Loading materials from a dir. --- diff --git a/catalogue/management/commands/importlessons.py b/catalogue/management/commands/importlessons.py index 1eefc46..87fd16e 100755 --- a/catalogue/management/commands/importlessons.py +++ b/catalogue/management/commands/importlessons.py @@ -21,20 +21,36 @@ 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') 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)) + 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 @@ -54,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 = {} @@ -74,7 +93,7 @@ class Command(BaseCommand): # Import book files try: - self.import_book(file_path, options) + self.import_book(file_path, options, attachments) files_imported += 1 transaction.commit() except Section.IncompleteError, e: diff --git a/catalogue/models.py b/catalogue/models.py index 916c5b1..b42a463 100644 --- a/catalogue/models.py +++ b/catalogue/models.py @@ -110,25 +110,17 @@ class Lesson(models.Model): slug = wldoc.book_info.url.slug try: lesson = cls.objects.get(slug=slug) + lesson.attachment_set.all().delete() except cls.DoesNotExist: lesson = cls(slug=slug, order=0) - lesson.attachment_set.all().delete() - for att_name, att_file in infile.attachments.items(): - try: - slug, ext = att_name.rsplit('.', 1) - except ValueError: - slug, ext = att_name, '' - attachment = lesson.attachment_set.create(slug=slug, ext=ext) - attachment.file.save(att_name, ContentFile(att_file.get_string())) - # Save XML file lesson.xml_file.save('%s.xml' % slug, ContentFile(xml), save=False) lesson.title = wldoc.book_info.title lesson.level = Level.objects.get(slug=wldoc.book_info.audience) lesson.populate_dc() - lesson.build_html() + lesson.build_html(infile=infile) lesson.build_package() lesson.build_package(student=True) return lesson @@ -150,10 +142,15 @@ class Lesson(models.Model): courses.add(curr.course) self.curriculum_courses = courses - def build_html(self): + def build_html(self, infile=None): from librarian.parser import WLDocument - wldoc = WLDocument.from_file(self.xml_file.path) - html = wldoc.as_html() + from .publish import HtmlFormat + + if infile is None: + wldoc = WLDocument.from_file(self.xml_file.path) + else: + wldoc = WLDocument(infile) + html = HtmlFormat(wldoc).build() self.html_file.save("%s.html" % self.slug, File(open(html.get_filename()))) diff --git a/catalogue/publish.py b/catalogue/publish.py new file mode 100755 index 0000000..023e8bd --- /dev/null +++ b/catalogue/publish.py @@ -0,0 +1,35 @@ +# -*- coding: utf-8 +from django.core.files import File +from librarian.pyhtml import EduModuleFormat +from .models import Lesson, Attachment + + +class HtmlFormat(EduModuleFormat): + def url_for_material(self, slug, fmt=None): + lesson_slug = self.wldoc.book_info.url.slug + if fmt is None: + # We could try and find the file by slug here, but we won't. + # User should supply the format explicitly anyway. + fmt = self.DEFAULT_MATERIAL_FORMAT + + try: + # If already saved, use it. + att = Attachment.objects.get(lesson__slug=lesson_slug, + slug=slug, ext=fmt) + except Attachment.DoesNotExist, e: + # If attached to source IOFile, save now. + att_name = "%s.%s" % (slug, fmt) + try: + att_file = self.wldoc.source.attachments[att_name] + except KeyError: + print u"ATTACHMENT MISSING:", att_name + raise self.MaterialNotFound() + else: + lesson = Lesson.objects.get(slug=lesson_slug) + att = lesson.attachment_set.create(slug=slug, ext=fmt) + att.file.save(att_name, File(att_file.get_file())) + return att.file.url + + + else: + return att.file.url