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 .models import Lesson, Attachment
8 from fnpdjango.utils.text.slughifi import slughifi
11 # TODO: Using sorl.thumbnail for now,
12 # but this should be done in Librarian,
13 # directly using convert or PIL as a fallback.
14 def get_image(src_img_path, width=None, default_width=1600, formats=('PNG', 'JPEG', 'GIF')):
15 """ Returns an object with `url` and `storage` attributes,
16 or None if using the original image is OK.
20 from sorl.thumbnail import get_thumbnail
22 # Does it need converting?
23 # Yes, if width is given explicitly.
24 convert = width is not None
26 # Looks like it doesn't need converting.
27 # But let's try opening it and checking its type.
29 simg = Image.open(src_img_path)
31 # It doesn't look like image,
32 # but maybe it's convertable to one.
35 if simg.format not in formats:
36 # It's an image, but it's in some weird format.
44 return get_thumbnail(src_img_path, '%sx%s' % (width, 10*width))
46 # hard to predict what solr raises on invalid image
52 class HtmlFormat(EduModuleFormat):
53 IMAGE_FORMATS = ('PNG', 'JPEG', 'GIF')
54 DEFAULT_IMAGE_WIDTH = 1600
56 def find_attachment(self, slug, fmt):
57 lesson_slug = self.wldoc.book_info.url.slug
59 # If already saved, use it.
60 att = Attachment.objects.get(lesson__slug=lesson_slug,
62 except Attachment.DoesNotExist, e:
63 # If attached to source IOFile, save now.
64 att_name = "%s.%s" % (slug, fmt)
66 att_file = self.wldoc.source.attachments[att_name]
68 print u"ATTACHMENT MISSING:", att_name
69 raise self.MaterialNotFound()
71 lesson = Lesson.objects.get(slug=lesson_slug)
72 att = lesson.attachment_set.create(slug=slug, ext=fmt)
73 att.file.save(att_name, File(att_file.get_file()))
78 def url_for_material(self, slug, fmt):
79 return self.find_attachment(slug, fmt).file.url
81 def url_for_image(self, slug, fmt, width=None):
83 src_img = self.find_attachment(slug, fmt).file
84 except self.MaterialNotFound:
86 img = get_image(src_img.path, width, self.DEFAULT_IMAGE_WIDTH, self.IMAGE_FORMATS)
87 return (img or src_img).url
89 def text_to_anchor(self, text):
92 def get_forma_url(self, forma):
94 reverse('catalogue_lesson', args=['metody']),
95 self.text_to_anchor(forma)
98 def get_help_url(self, naglowek):
100 '//edukacjamedialna.edu.pl',
101 reverse('info', args=['jak-korzystac/']),
102 self.naglowek_to_anchor(naglowek)
106 class PdfFormat(EduModulePDFFormat):
107 IMAGE_FORMATS = ('PNG', 'JPEG', 'GIF')
108 DEFAULT_IMAGE_WIDTH = 1600
110 def get_image(self, name):
111 src_img = super(PdfFormat, self).get_image(name)
113 src_img.get_filename(),
114 default_width=self.DEFAULT_IMAGE_WIDTH,
115 formats=self.IMAGE_FORMATS)
117 return IOFile.from_filename(img.storage.path(img))
122 class OrmDocProvider(DocProvider):
123 def by_slug(self, slug):
124 """Should return a file-like object with a WL document XML."""
125 return IOFile.from_filename(Lesson.objects.get(slug=slug).xml_file.path)