X-Git-Url: https://git.mdrn.pl/edumed.git/blobdiff_plain/c5f4d6f7fc2c876a56e7fcac2a9965e64f05c191..f8b34544329e05b7c27990ffe9c241df877cd95c:/catalogue/publish.py diff --git a/catalogue/publish.py b/catalogue/publish.py new file mode 100755 index 0000000..023e8bd --- /dev/null +++ b/catalogue/publish.py @@ -0,0 +1,35 @@ +# -*- coding: utf-8 +from django.core.files import File +from librarian.pyhtml import EduModuleFormat +from .models import Lesson, Attachment + + +class HtmlFormat(EduModuleFormat): + def url_for_material(self, slug, fmt=None): + lesson_slug = self.wldoc.book_info.url.slug + if fmt is None: + # We could try and find the file by slug here, but we won't. + # User should supply the format explicitly anyway. + fmt = self.DEFAULT_MATERIAL_FORMAT + + try: + # If already saved, use it. + att = Attachment.objects.get(lesson__slug=lesson_slug, + slug=slug, ext=fmt) + except Attachment.DoesNotExist, e: + # If attached to source IOFile, save now. + att_name = "%s.%s" % (slug, fmt) + try: + att_file = self.wldoc.source.attachments[att_name] + except KeyError: + print u"ATTACHMENT MISSING:", att_name + raise self.MaterialNotFound() + else: + lesson = Lesson.objects.get(slug=lesson_slug) + att = lesson.attachment_set.create(slug=slug, ext=fmt) + att.file.save(att_name, File(att_file.get_file())) + return att.file.url + + + else: + return att.file.url