slowniczek + metody
[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=None):
10         lesson_slug = self.wldoc.book_info.url.slug
11         if fmt is None:
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
15
16         try:
17             # If already saved, use it.
18             att = Attachment.objects.get(lesson__slug=lesson_slug,
19                                          slug=slug, ext=fmt)
20         except Attachment.DoesNotExist, e:
21             # If attached to source IOFile, save now.
22             att_name = "%s.%s" % (slug, fmt)
23             try:
24                 att_file = self.wldoc.source.attachments[att_name]
25             except KeyError:
26                 print u"ATTACHMENT MISSING:", att_name
27                 raise self.MaterialNotFound()
28             else:
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()))
32                 return att.file.url
33         else:
34             return att.file.url
35
36
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)