check for lessons without section
[edumed.git] / catalogue / publish.py
1 # -*- coding: utf-8
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
9
10
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.
17     """
18
19     from PIL import Image
20     from sorl.thumbnail import get_thumbnail
21
22     # Does it need converting?
23     # Yes, if width is given explicitly.
24     convert = width is not None
25     if not convert:
26         # Looks like it doesn't need converting.
27         # But let's try opening it and checking its type.
28         try:
29             simg = Image.open(src_img_path)
30         except IOError:
31             # It doesn't look like image,
32             # but maybe it's convertable to one.
33             convert = True
34         else:
35             if simg.format not in formats:
36                 # It's an image, but it's in some weird format.
37                 convert = True
38                 width = simg.size[0]
39
40     if convert:
41         if width is None:
42             width = default_width
43         return get_thumbnail(src_img_path, '%sx%s' % (width, 10*width))
44     else:
45         return None
46
47
48 class HtmlFormat(EduModuleFormat):
49     IMAGE_FORMATS = ('PNG', 'JPEG', 'GIF')
50     DEFAULT_IMAGE_WIDTH = 1600
51
52     def find_attachment(self, slug, fmt):
53         lesson_slug = self.wldoc.book_info.url.slug
54         try:
55             # If already saved, use it.
56             att = Attachment.objects.get(lesson__slug=lesson_slug,
57                                          slug=slug, ext=fmt)
58         except Attachment.DoesNotExist, e:
59             # If attached to source IOFile, save now.
60             att_name = "%s.%s" % (slug, fmt)
61             try:
62                 att_file = self.wldoc.source.attachments[att_name]
63             except KeyError:
64                 print u"ATTACHMENT MISSING:", att_name
65                 raise self.MaterialNotFound()
66             else:
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()))
70                 return att
71         else:
72             return att
73
74     def url_for_material(self, slug, fmt):
75         return self.find_attachment(slug, fmt).file.url
76
77     def url_for_image(self, slug, fmt, width=None):
78         try:
79             src_img = self.find_attachment(slug, fmt).file
80         except self.MaterialNotFound:
81             return ''
82         img = get_image(src_img.path, width, self.DEFAULT_IMAGE_WIDTH, self.IMAGE_FORMATS)
83         return (img or src_img).url
84
85     def text_to_anchor(self, text):
86         return slughifi(text)
87
88     def get_forma_url(self, forma):
89         return '%s#%s' % (
90             reverse('catalogue_lesson', args=['metody']),
91             self.text_to_anchor(forma)
92         )
93
94     def get_help_url(self, naglowek):
95         return '%s%s#%s' % (
96             '//edukacjamedialna.edu.pl',
97             reverse('info', args=['jak-korzystac/']),
98             self.naglowek_to_anchor(naglowek)
99         )
100
101
102 class PdfFormat(EduModulePDFFormat):
103     IMAGE_FORMATS = ('PNG', 'JPEG', 'GIF')
104     DEFAULT_IMAGE_WIDTH = 1600
105
106     def get_image(self, name):
107         src_img = super(PdfFormat, self).get_image(name)
108         img = get_image(
109             src_img.get_filename(),
110             default_width=self.DEFAULT_IMAGE_WIDTH,
111             formats=self.IMAGE_FORMATS)
112         if img:
113             return IOFile.from_filename(img.storage.path(img))
114         else:
115             return src_img
116
117
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)