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
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 = {}
# 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:
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
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())))
--- /dev/null
+# -*- 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