2 from django.core.files import File
 
   3 from django.core.urlresolvers import reverse
 
   4 from librarian import DocProvider, IOFile
 
   5 from librarian.pyhtml import EduModuleFormat
 
   6 from librarian.pypdf import EduModulePDFFormat
 
   7 from librarian.pdf_from_html import EduModulePdfFromHtmlFormat
 
   8 from .models import Lesson, Attachment
 
   9 from fnpdjango.utils.text.slughifi import slughifi
 
  12 # TODO: Using sorl.thumbnail for now,
 
  13 # but this should be done in Librarian,
 
  14 # directly using convert or PIL as a fallback.
 
  15 def get_image(src_img_path, width=None, default_width=1600, formats=('PNG', 'JPEG', 'GIF')):
 
  16     """ Returns an object with `url` and `storage` attributes,
 
  17         or None if using the original image is OK.
 
  21     from sorl.thumbnail import get_thumbnail
 
  23     # Does it need converting?
 
  24     # Yes, if width is given explicitly.
 
  25     convert = width is not None
 
  27         # Looks like it doesn't need converting.
 
  28         # But let's try opening it and checking its type.
 
  30             simg = Image.open(src_img_path)
 
  32             # It doesn't look like image,
 
  33             # but maybe it's convertable to one.
 
  36             if simg.format not in formats:
 
  37                 # It's an image, but it's in some weird format.
 
  45             return get_thumbnail(src_img_path, '%sx%s' % (width, 10*width))
 
  47             # hard to predict what solr raises on invalid image
 
  53 class HtmlFormat(EduModuleFormat):
 
  54     IMAGE_FORMATS = ('PNG', 'JPEG', 'GIF')
 
  55     DEFAULT_IMAGE_WIDTH = 1600
 
  57     def find_attachment(self, slug, fmt):
 
  58         lesson_slug = self.wldoc.book_info.url.slug
 
  60             # If already saved, use it.
 
  61             att = Attachment.objects.get(lesson__slug=lesson_slug,
 
  63         except Attachment.DoesNotExist, e:
 
  64             # If attached to source IOFile, save now.
 
  65             att_name = "%s.%s" % (slug, fmt)
 
  67                 att_file = self.wldoc.source.attachments[att_name]
 
  69                 print u"ATTACHMENT MISSING:", att_name
 
  70                 raise self.MaterialNotFound()
 
  72                 lesson = Lesson.objects.get(slug=lesson_slug)
 
  73                 att = lesson.attachment_set.create(slug=slug, ext=fmt)
 
  74                 att.file.save(att_name, File(att_file.get_file()))
 
  79     def url_for_material(self, slug, fmt):
 
  80         return self.find_attachment(slug, fmt).file.url
 
  82     def url_for_image(self, slug, fmt, width=None):
 
  84             src_img = self.find_attachment(slug, fmt).file
 
  85         except self.MaterialNotFound:
 
  87         img = get_image(src_img.path, width, self.DEFAULT_IMAGE_WIDTH, self.IMAGE_FORMATS)
 
  88         return (img or src_img).url
 
  90     def text_to_anchor(self, text):
 
  93     def get_forma_url(self, forma):
 
  95             reverse('catalogue_lesson', args=['metody']),
 
  96             self.text_to_anchor(forma)
 
  99     def get_help_url(self, naglowek):
 
 101             '//edukacjamedialna.edu.pl',
 
 102             reverse('info', args=['jak-korzystac/']),
 
 103             self.naglowek_to_anchor(naglowek)
 
 107 class PdfFormat(EduModulePDFFormat):
 
 108     IMAGE_FORMATS = ('PNG', 'JPEG', 'GIF')
 
 109     DEFAULT_IMAGE_WIDTH = 1600
 
 111     def get_image(self, name):
 
 112         src_img = super(PdfFormat, self).get_image(name)
 
 114             src_img.get_filename(),
 
 115             default_width=self.DEFAULT_IMAGE_WIDTH,
 
 116             formats=self.IMAGE_FORMATS)
 
 118             return IOFile.from_filename(img.storage.path(img))
 
 123 class PdfFromHtmlFormat(EduModulePdfFromHtmlFormat):
 
 124     IMAGE_FORMATS = ('PNG', 'JPEG', 'GIF')
 
 125     DEFAULT_IMAGE_WIDTH = 1600
 
 127     def find_attachment(self, slug, fmt):
 
 128         lesson_slug = self.wldoc.book_info.url.slug
 
 130             # If already saved, use it.
 
 131             att = Attachment.objects.get(lesson__slug=lesson_slug,
 
 133         except Attachment.DoesNotExist, e:
 
 134             # If attached to source IOFile, save now.
 
 135             att_name = "%s.%s" % (slug, fmt)
 
 137                 att_file = self.wldoc.source.attachments[att_name]
 
 139                 print u"ATTACHMENT MISSING:", att_name
 
 140                 raise self.MaterialNotFound()
 
 142                 lesson = Lesson.objects.get(slug=lesson_slug)
 
 143                 att = lesson.attachment_set.create(slug=slug, ext=fmt)
 
 144                 att.file.save(att_name, File(att_file.get_file()))
 
 149     def url_for_material(self, slug, fmt):
 
 150         return self.find_attachment(slug, fmt).file.url
 
 152     def image(self, slug, fmt, width=None):
 
 154             src_img = self.find_attachment(slug, fmt).file
 
 155         except self.MaterialNotFound:
 
 157         img = get_image(src_img.path, width, self.DEFAULT_IMAGE_WIDTH, self.IMAGE_FORMATS)
 
 158         return img or src_img
 
 160     def url_for_image(self, slug, fmt, image=None, **kwargs):
 
 161         img = image or self.image(slug, fmt, **kwargs)
 
 167     def text_to_anchor(self, text):
 
 168         return slughifi(text)
 
 171 class OrmDocProvider(DocProvider):
 
 172     def by_slug(self, slug):
 
 173         """Should return a file-like object with a WL document XML."""
 
 174         return IOFile.from_filename(Lesson.objects.get(slug=slug).xml_file.path)