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.
43 return get_thumbnail(src_img_path, '%sx%s' % (width, 10*width))
48 class HtmlFormat(EduModuleFormat):
49 IMAGE_FORMATS = ('PNG', 'JPEG', 'GIF')
50 DEFAULT_IMAGE_WIDTH = 1600
52 def find_attachment(self, slug, fmt):
53 lesson_slug = self.wldoc.book_info.url.slug
55 # If already saved, use it.
56 att = Attachment.objects.get(lesson__slug=lesson_slug,
58 except Attachment.DoesNotExist, e:
59 # If attached to source IOFile, save now.
60 att_name = "%s.%s" % (slug, fmt)
62 att_file = self.wldoc.source.attachments[att_name]
64 print u"ATTACHMENT MISSING:", att_name
65 raise self.MaterialNotFound()
67 lesson = Lesson.objects.get(slug=lesson_slug)
68 att = lesson.attachment_set.create(slug=slug, ext=fmt)
69 att.file.save(att_name, File(att_file.get_file()))
74 def url_for_material(self, slug, fmt):
75 return self.find_attachment(slug, fmt).file.url
77 def url_for_image(self, slug, fmt, width=None):
79 src_img = self.find_attachment(slug, fmt).file
80 except self.MaterialNotFound:
82 img = get_image(src_img.path, width, self.DEFAULT_IMAGE_WIDTH, self.IMAGE_FORMATS)
83 return (img or src_img).url
85 def text_to_anchor(self, text):
88 def get_forma_url(self, forma):
90 reverse('catalogue_lesson', args=['metody']),
91 self.text_to_anchor(forma)
94 def get_help_url(self, naglowek):
96 '//edukacjamedialna.edu.pl',
97 reverse('info', args=['jak-korzystac/']),
98 self.naglowek_to_anchor(naglowek)
102 class PdfFormat(EduModulePDFFormat):
103 IMAGE_FORMATS = ('PNG', 'JPEG', 'GIF')
104 DEFAULT_IMAGE_WIDTH = 1600
106 def get_image(self, name):
107 src_img = super(PdfFormat, self).get_image(name)
109 src_img.get_filename(),
110 default_width=self.DEFAULT_IMAGE_WIDTH,
111 formats=self.IMAGE_FORMATS)
113 return IOFile.from_filename(img.storage.path(img))
118 class OrmDocProvider(DocProvider):
119 def by_slug(self, slug):
120 """Should return a file-like object with a WL document XML."""
121 return IOFile.from_filename(Lesson.objects.get(slug=slug).xml_file.path)