attachments by slugs with extensions
[edumed.git] / catalogue / models.py
1 # -*- coding: utf-8
2 from django.core.files import File
3 from django.core.urlresolvers import reverse
4 from django.db import models
5 from jsonfield import JSONField
6 from fnpdjango.storage import BofhFileSystemStorage
7
8 from curriculum.models import Level, Curriculum, CurriculumCourse
9 import logging
10
11 bofh_storage = BofhFileSystemStorage()
12
13
14 class Section(models.Model):
15     title = models.CharField(max_length=255, unique=True)
16     slug = models.SlugField(max_length=255, unique=True)
17     order = models.IntegerField()
18     xml_file = models.FileField(
19         upload_to="catalogue/section/xml",
20         null=True, blank=True, max_length=255,
21         storage=bofh_storage)
22     image = models.ImageField(upload_to="catalogue/section/image", null=True, blank=True)
23
24     pic = models.ImageField(upload_to="catalogue/section/pic", null=True, blank=True)
25     pic_attribution = models.CharField(max_length=255, null=True, blank=True)
26     pic_src = models.URLField(null=True, blank=True)
27     
28     summary = models.TextField(blank=True, null=True)
29
30     class Meta:
31         ordering = ['order']
32
33     class IncompleteError(BaseException):
34         pass
35
36     def __unicode__(self):
37         return self.title
38
39     def get_absolute_url(self):
40         return "%s#gimnazjum_%s" % (reverse("catalogue_lessons"), self.slug)
41
42     @classmethod
43     def publish(cls, infile, ignore_incomplete=False):
44         from librarian.parser import WLDocument
45         from django.core.files.base import ContentFile
46         xml = infile.get_string()
47         wldoc = WLDocument.from_string(xml)
48
49         lessons = []
50         for part in wldoc.book_info.parts:
51             try:
52                 lessons.append(Lesson.objects.get(slug=part.slug))
53             except Lesson.DoesNotExist, e:
54                 if not ignore_incomplete:
55                     raise cls.IncompleteError(part.slug)
56
57         slug = wldoc.book_info.url.slug
58         try:
59             section = cls.objects.get(slug=slug)
60         except cls.DoesNotExist:
61             section = cls(slug=slug, order=0)
62
63         # Save XML file
64         section.xml_file.save('%s.xml' % slug, ContentFile(xml), save=False)
65         section.title = wldoc.book_info.title
66         section.save()
67
68         section.lesson_set.all().update(section=None)
69         for i, lesson in enumerate(lessons):
70             lesson.section = section
71             lesson.order = i
72             lesson.save()
73
74         return section
75
76     def syntetic_lesson(self, level):
77         try:
78             return self.lesson_set.filter(type='synthetic', level=level)[0]
79         except IndexError:
80             return None
81
82
83 class Lesson(models.Model):
84     section = models.ForeignKey(Section, null=True, blank=True)
85     level = models.ForeignKey(Level)
86     title = models.CharField(max_length=255)
87     slug = models.SlugField(max_length=255, unique=True)
88     type = models.CharField(max_length=15, db_index=True)
89     order = models.IntegerField(db_index=True)
90     dc = JSONField(default='{}')
91     curriculum_courses = models.ManyToManyField(CurriculumCourse, blank=True)
92     description = models.TextField(null=True, blank=True)
93
94     xml_file = models.FileField(
95         upload_to="catalogue/lesson/xml",
96         null=True, blank=True, max_length=255, storage=bofh_storage)
97     html_file = models.FileField(
98         upload_to="catalogue/lesson/html",
99         null=True, blank=True, max_length=255, storage=bofh_storage)
100     package = models.FileField(
101         upload_to="catalogue/lesson/pack",
102         null=True, blank=True, max_length=255, storage=bofh_storage)
103     student_package = models.FileField(
104         upload_to="catalogue/lesson/student_pack",
105         null=True, blank=True, max_length=255, storage=bofh_storage)
106     pdf = models.FileField(
107         upload_to="catalogue/lesson/pdf",
108         null=True, blank=True, max_length=255, storage=bofh_storage)
109     student_pdf = models.FileField(
110         upload_to="catalogue/lesson/student_pdf",
111         null=True, blank=True, max_length=255, storage=bofh_storage)
112
113     class Meta:
114         ordering = ['section', 'level', 'order']
115
116     def __unicode__(self):
117         return self.title
118
119     @models.permalink
120     def get_absolute_url(self):
121         return 'catalogue_lesson', [self.slug]
122
123     @classmethod
124     def publish(cls, infile, ignore_incomplete=False):
125         from librarian.parser import WLDocument
126         from django.core.files.base import ContentFile
127         wldoc = WLDocument(infile)
128
129         # Check if not section metadata block.
130         if wldoc.book_info.parts:
131             return Section.publish(infile, ignore_incomplete=ignore_incomplete)
132
133         slug = wldoc.book_info.url.slug
134         try:
135             lesson = cls.objects.get(slug=slug)
136             lesson.attachment_set.all().delete()
137         except cls.DoesNotExist:
138             lesson = cls(slug=slug, order=0)
139
140         # Save XML file
141         lesson.xml_file.save('%s.xml' % slug, ContentFile(infile.get_string()), save=False)
142         lesson.title = wldoc.book_info.title
143
144         lesson.level = Level.objects.get(meta_name=wldoc.book_info.audience)
145         lesson.populate_dc()
146         lesson.populate_description(wldoc=wldoc)
147         lesson.build_html(infile=infile)
148         lesson.build_pdf()
149         lesson.build_package()
150         if lesson.type != 'project':
151             lesson.build_pdf(student=True)
152             lesson.build_package(student=True)
153         return lesson
154
155     def republish(self, repackage_level=True, attachments=None):
156         from librarian import IOFile
157         import os.path
158         from django.conf import settings
159         if attachments is None:
160             attachments = {}
161             for attachment in self.attachment_set.all():
162                 full_name = os.path.join(settings.MEDIA_ROOT, '%s.%s' % (attachment.file.name, attachment.ext))
163                 f = IOFile.from_filename(full_name)
164                 attachments[attachment.slug] = f
165         infile = IOFile.from_filename(self.xml_file.path, attachments=attachments)
166         Lesson.publish(infile)
167         if repackage_level:
168             self.level.build_packages()
169
170     def populate_dc(self):
171         from librarian.parser import WLDocument
172         wldoc = WLDocument.from_file(self.xml_file.path)
173         self.dc = wldoc.book_info.to_dict()
174         self.type = self.dc["type"]
175         assert self.type in ('appendix', 'course', 'synthetic', 'project', 'added', 'added-var'), \
176             u"Unknown lesson type: %s" % self.type
177         self.save()
178
179         courses = set()
180         for identifier in wldoc.book_info.curriculum:
181             identifier = (identifier or "").replace(' ', '')
182             if not identifier:
183                 continue
184             try:
185                 curr = Curriculum.objects.get(identifier__iexact=identifier)
186             except Curriculum.DoesNotExist:
187                 logging.warn('Unknown curriculum course %s in lesson %s' % (identifier, self.slug))
188                 pass
189             else:
190                 courses.add(curr.course)
191         self.curriculum_courses = courses
192
193     def populate_description(self, wldoc=None, infile=None):
194         if wldoc is None:
195             wldoc = self.wldocument(infile)
196         if self.type == 'project':
197             lookup = u'Zadanie'
198         else:
199             lookup = u'Pomysł na lekcję'
200         for header in wldoc.edoc.findall('.//naglowek_rozdzial'):
201             if (header.text or '').strip() == lookup:
202                 from lxml import etree
203                 self.description = etree.tostring(
204                     header.getnext(), method='text', encoding='unicode').strip()
205                 self.save()
206                 return
207
208     def wldocument(self, infile=None):
209         from librarian import IOFile
210         from librarian.parser import WLDocument
211         from .publish import OrmDocProvider
212
213         if infile is None:
214             infile = IOFile.from_filename(self.xml_file.path)
215             for att in self.attachment_set.all():
216                 infile.attachments["%s.%s" % (att.slug, att.ext)] = \
217                     IOFile.from_filename(att.file.path)
218         return WLDocument(infile, provider=OrmDocProvider())
219
220     def build_html(self, infile=None):
221         from .publish import HtmlFormat
222         wldoc = self.wldocument(infile)
223         html = HtmlFormat(wldoc).build()
224         self.html_file.save("%s.html" % self.slug, File(open(html.get_filename())))
225
226     def build_pdf(self, student=False):
227         from .publish import PdfFormat
228         # PDF uses document with attachments already saved as media,
229         # otherwise sorl.thumbnail complains about SuspiciousOperations.
230         wldoc = self.wldocument()
231         if student:
232             pdf = PdfFormat(wldoc).build()
233             self.student_pdf.save("%s.pdf" % self.slug, File(open(pdf.get_filename())))
234         else:
235             pdf = PdfFormat(wldoc, teacher=True).build()
236             self.pdf.save("%s.pdf" % self.slug, File(open(pdf.get_filename())))
237
238     def add_to_zip(self, zipf, student=False, prefix=''):
239         pdf = self.student_pdf if student else self.pdf
240         if pdf:
241             zipf.write(pdf.path, "%s%s%s.pdf" % (prefix, self.slug, "_student" if student else ""))
242             for attachment in self.attachment_set.all():
243                 zipf.write(attachment.file.path, u"%smaterialy/%s.%s" % (prefix, attachment.slug, attachment.ext))
244             zipf.write(self.xml_file.path, "%spliki-zrodlowe/%s.xml" % (prefix, self.slug))
245
246     def build_package(self, student=False):
247         from StringIO import StringIO
248         import zipfile
249         from django.core.files.base import ContentFile
250         buff = StringIO()
251         zipf = zipfile.ZipFile(buff, 'w', zipfile.ZIP_STORED)
252         self.add_to_zip(zipf, student)
253         zipf.close()
254         fieldname = "student_package" if student else "package"
255         getattr(self, fieldname).save(
256             "%s%s.zip" % (self.slug, "_student" if student else ""),
257             ContentFile(buff.getvalue()))
258
259     def get_syntetic(self):
260         if self.section is None:
261             return None
262         return self.section.syntetic_lesson(self.level)
263
264     def get_other_level(self):
265         if self.section is None:
266             return None
267         other_levels = self.section.lesson_set.exclude(level=self.level)
268         if other_levels.exists():
269             return other_levels[0].level
270
271     def get_previous(self):
272         if self.section is None:
273             return None
274         try:
275             return self.section.lesson_set.filter(
276                 type=self.type, level=self.level,
277                 order__lt=self.order).order_by('-order')[0]
278         except IndexError:
279             return None
280
281     def get_next(self):
282         if self.section is None:
283             return None
284         try:
285             return self.section.lesson_set.filter(
286                 type=self.type, level=self.level,
287                 order__gt=self.order).order_by('order')[0]
288         except IndexError:
289             return None
290
291     def requires_internet(self):
292         return 'internet' in self.dc.get('requires', [])
293
294
295 class Attachment(models.Model):
296     slug = models.CharField(max_length=255)
297     ext = models.CharField(max_length=15)
298     lesson = models.ForeignKey(Lesson)
299     file = models.FileField(upload_to="catalogue/attachment", storage=bofh_storage)
300
301     class Meta:
302         ordering = ['slug', 'ext']
303         unique_together = ['lesson', 'slug', 'ext']
304
305     def __unicode__(self):
306         return "%s.%s" % (self.slug, self.ext)
307
308
309 class Part(models.Model):
310     lesson = models.ForeignKey(Lesson)
311     pdf = models.FileField(upload_to="catalogue/part/pdf", null=True, blank=True)
312     student_pdf = models.FileField(upload_to="catalogue/part/student_pdf", null=True, blank=True)
313
314
315 class LessonStub(models.Model):
316     section = models.ForeignKey(Section, null=True, blank=True)
317     level = models.ForeignKey(Level)
318     title = models.CharField(max_length=255)
319     type = models.CharField(max_length=15, db_index=True)
320     order = models.IntegerField(db_index=True)
321
322     class Meta:
323         ordering = ['section', 'level', 'order']
324
325     def __unicode__(self):
326         return self.title
327
328     @property
329     def slug(self):
330         return ''
331
332     def add_to_zip(self, *args, **kwargs):
333         pass