Exercises: retry implied on move; add handlers to sortable
[edumed.git] / catalogue / publish.py
1 # -*- coding: utf-8
2 from django.core.files import File
3 from librarian.pyhtml import EduModuleFormat
4 from .models import Lesson, Attachment
5
6
7 class HtmlFormat(EduModuleFormat):
8     def url_for_material(self, slug, fmt=None):
9         lesson_slug = self.wldoc.book_info.url.slug
10         if fmt is None:
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
14
15         try:
16             # If already saved, use it.
17             att = Attachment.objects.get(lesson__slug=lesson_slug,
18                                          slug=slug, ext=fmt)
19         except Attachment.DoesNotExist, e:
20             # If attached to source IOFile, save now.
21             att_name = "%s.%s" % (slug, fmt)
22             try:
23                 att_file = self.wldoc.source.attachments[att_name]
24             except KeyError:
25                 print u"ATTACHMENT MISSING:", att_name
26                 raise self.MaterialNotFound()
27             else:
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()))
31                 return att.file.url
32
33
34         else:
35             return att.file.url