2 from django.core.files import File
3 from librarian import DocProvider, IOFile
4 from librarian.pyhtml import EduModuleFormat
5 from .models import Lesson, Attachment
8 class HtmlFormat(EduModuleFormat):
9 def url_for_material(self, slug, fmt=None):
10 lesson_slug = self.wldoc.book_info.url.slug
12 # We could try and find the file by slug here, but we won't.
13 # User should supply the format explicitly anyway.
14 fmt = self.DEFAULT_MATERIAL_FORMAT
17 # If already saved, use it.
18 att = Attachment.objects.get(lesson__slug=lesson_slug,
20 except Attachment.DoesNotExist, e:
21 # If attached to source IOFile, save now.
22 att_name = "%s.%s" % (slug, fmt)
24 att_file = self.wldoc.source.attachments[att_name]
26 print u"ATTACHMENT MISSING:", att_name
27 raise self.MaterialNotFound()
29 lesson = Lesson.objects.get(slug=lesson_slug)
30 att = lesson.attachment_set.create(slug=slug, ext=fmt)
31 att.file.save(att_name, File(att_file.get_file()))
37 class OrmDocProvider(DocProvider):
38 def by_slug(self, slug):
39 """Should return a file-like object with a WL document XML."""
40 return IOFile.from_filename(Lesson.objects.get(slug=slug).xml_file.path)