- # 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
+ src_img = self.find_attachment(slug, fmt).file
+ except self.MaterialNotFound:
+ return ''
+ img = get_image(src_img.path, width,
+ self.DEFAULT_IMAGE_WIDTH, self.IMAGE_FORMATS)
+ return (img or src_img).url
+
+ def text_to_anchor(self, text):
+ return slughifi(text)
+
+ def get_forma_url(self, forma):
+ return '%s#%s' % (
+ reverse('catalogue_lesson', args=['metody']),
+ self.text_to_anchor(forma)
+ )
+
+ def get_help_url(self, naglowek):
+ return '%s%s#%s' % (
+ '//edukacjamedialna.edu.pl',
+ reverse('info', args=['jak-korzystac/']),
+ self.naglowek_to_anchor(naglowek)
+ )
+
+
+class PdfFormat(EduModulePDFFormat):
+ IMAGE_FORMATS = ('PNG', 'JPEG', 'GIF')
+ DEFAULT_IMAGE_WIDTH = 1600
+
+ def get_image(self, name):
+ src_img = super(PdfFormat, self).get_image(name)
+ img = get_image(src_img.get_filename(),
+ default_width=self.DEFAULT_IMAGE_WIDTH,
+ formats=self.IMAGE_FORMATS
+ )
+ if img:
+ return IOFile.from_filename(img.storage.path(img))
+ else:
+ return src_img