2 from django.core.files import File
3 from librarian.pyhtml import EduModuleFormat
4 from .models import Lesson, Attachment
7 class HtmlFormat(EduModuleFormat):
8 def url_for_material(self, slug, fmt=None):
9 lesson_slug = self.wldoc.book_info.url.slug
11 # We could try and find the file by slug here, but we won't.
12 # User should supply the format explicitly anyway.
13 fmt = self.DEFAULT_MATERIAL_FORMAT
16 # If already saved, use it.
17 att = Attachment.objects.get(lesson__slug=lesson_slug,
19 except Attachment.DoesNotExist, e:
20 # If attached to source IOFile, save now.
21 att_name = "%s.%s" % (slug, fmt)
23 att_file = self.wldoc.source.attachments[att_name]
25 print u"ATTACHMENT MISSING:", att_name
26 raise self.MaterialNotFound()
28 lesson = Lesson.objects.get(slug=lesson_slug)
29 att = lesson.attachment_set.create(slug=slug, ext=fmt)
30 att.file.save(att_name, File(att_file.get_file()))