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):
10 lesson_slug = self.wldoc.book_info.url.slug
12 # If already saved, use it.
13 att = Attachment.objects.get(lesson__slug=lesson_slug,
15 except Attachment.DoesNotExist, e:
16 # If attached to source IOFile, save now.
17 att_name = "%s.%s" % (slug, fmt)
19 att_file = self.wldoc.source.attachments[att_name]
21 print u"ATTACHMENT MISSING:", att_name
22 raise self.MaterialNotFound()
24 lesson = Lesson.objects.get(slug=lesson_slug)
25 att = lesson.attachment_set.create(slug=slug, ext=fmt)
26 att.file.save(att_name, File(att_file.get_file()))
32 class OrmDocProvider(DocProvider):
33 def by_slug(self, slug):
34 """Should return a file-like object with a WL document XML."""
35 return IOFile.from_filename(Lesson.objects.get(slug=slug).xml_file.path)