material needs format
[edumed.git] / catalogue / publish.py
1 # -*- coding: utf-8
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
6
7
8 class HtmlFormat(EduModuleFormat):
9     def url_for_material(self, slug, fmt):
10         lesson_slug = self.wldoc.book_info.url.slug
11         try:
12             # If already saved, use it.
13             att = Attachment.objects.get(lesson__slug=lesson_slug,
14                                          slug=slug, ext=fmt)
15         except Attachment.DoesNotExist, e:
16             # If attached to source IOFile, save now.
17             att_name = "%s.%s" % (slug, fmt)
18             try:
19                 att_file = self.wldoc.source.attachments[att_name]
20             except KeyError:
21                 print u"ATTACHMENT MISSING:", att_name
22                 raise self.MaterialNotFound()
23             else:
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()))
27                 return att.file.url
28         else:
29             return att.file.url
30
31
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)