X-Git-Url: https://git.mdrn.pl/edumed.git/blobdiff_plain/594930a0723314b1d76f5128638ed42ec31c9493..edbf8a1ec4c2958d95870a826f46fb629a4ba656:/catalogue/publish.py?ds=sidebyside diff --git a/catalogue/publish.py b/catalogue/publish.py index 87f4d90..9f79dae 100755 --- a/catalogue/publish.py +++ b/catalogue/publish.py @@ -1,12 +1,14 @@ # -*- 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): + def find_attachment(self, slug, fmt): lesson_slug = self.wldoc.book_info.url.slug try: # If already saved, use it. @@ -24,9 +26,43 @@ 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.file.url + return att + + def url_for_material(self, slug, fmt): + return self.find_attachment(slug, fmt).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):