X-Git-Url: https://git.mdrn.pl/edumed.git/blobdiff_plain/f8b34544329e05b7c27990ffe9c241df877cd95c..932383c33aa99a6ae6e61a2049ecdacc5ecf6cc3:/catalogue/publish.py diff --git a/catalogue/publish.py b/catalogue/publish.py index 023e8bd..9f79dae 100755 --- a/catalogue/publish.py +++ b/catalogue/publish.py @@ -1,17 +1,15 @@ # -*- coding: utf-8 +from django.core.files.base import ContentFile from django.core.files import File +from librarian import DocProvider, IOFile from librarian.pyhtml import EduModuleFormat +from librarian.pypdf import EduModulePDFFormat from .models import Lesson, Attachment class HtmlFormat(EduModuleFormat): - def url_for_material(self, slug, fmt=None): + def find_attachment(self, slug, fmt): 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, @@ -28,8 +26,46 @@ class HtmlFormat(EduModuleFormat): 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 + return att + else: + return att + def url_for_material(self, slug, fmt): + return self.find_attachment(slug, fmt).file.url - else: - return att.file.url + def url_for_image(self, slug, fmt, width=None): + if width is None: + return self.url_for_material(slug, fmt) + + lesson_slug = self.wldoc.book_info.url.slug + th_slug = "thumb/%s__th%d" % (slug, width) + try: + # If already saved, use it. + att = Attachment.objects.get(lesson__slug=lesson_slug, + slug=th_slug, ext=fmt) + except Attachment.DoesNotExist, e: + from PIL import Image + from StringIO import StringIO + # Find full image, create thumbnail, save. + src_att = self.find_attachment(slug, fmt) + simg = Image.open(src_att.file.path) + size = (width, simg.size[1]*width/simg.size[0]) + simg = simg.resize(size, Image.ANTIALIAS) + + tempfile = StringIO() + img_format = "JPEG" if fmt.upper() == "JPG" else fmt + simg.save(tempfile, format=img_format) + att_name = "%s.%s" % (th_slug, fmt) + lesson = Lesson.objects.get(slug=lesson_slug) + att = lesson.attachment_set.create(slug=th_slug, ext=fmt) + att.file.save(att_name, ContentFile(tempfile.getvalue())) + return att.file.url + +class PdfFormat(EduModulePDFFormat): + pass + + +class OrmDocProvider(DocProvider): + def by_slug(self, slug): + """Should return a file-like object with a WL document XML.""" + return IOFile.from_filename(Lesson.objects.get(slug=slug).xml_file.path)