Add formsets to contact forms, add WTEM form.
[edumed.git] / catalogue / models.py
1 from django.core.files import File
2 from django.core.urlresolvers import reverse
3 from django.db import models
4 from jsonfield import JSONField
5 from curriculum.models import Level, Curriculum, CurriculumCourse
6
7
8 class Section(models.Model):
9     title = models.CharField(max_length=255, unique=True)
10     slug = models.SlugField(max_length=255, unique=True)
11     order = models.IntegerField()
12     xml_file = models.FileField(upload_to="catalogue/section/xml",
13         null=True, blank=True, max_length=255)
14     image = models.ImageField(upload_to="catalogue/section/image",
15         null=True, blank=True)
16
17     class Meta:
18         ordering = ['order']
19
20     class IncompleteError(BaseException):
21         pass
22
23     def __unicode__(self):
24         return self.title
25
26     def get_absolute_url(self):
27         return "%s#%s" % (reverse("catalogue_lessons"), self.slug)
28
29     @classmethod
30     def publish(cls, infile):
31         from librarian.parser import WLDocument
32         from django.core.files.base import ContentFile
33         xml = infile.get_string()
34         wldoc = WLDocument.from_string(xml)
35
36         try:
37             lessons = [Lesson.objects.get(slug=part.slug)
38                         for part in wldoc.book_info.parts]
39         except Lesson.DoesNotExist, e:
40             raise cls.IncompleteError(part.slug)
41
42         slug = wldoc.book_info.url.slug
43         try:
44             section = cls.objects.get(slug=slug)
45         except cls.DoesNotExist:
46             section = cls(slug=slug, order=0)
47
48         # Save XML file
49         section.xml_file.save('%s.xml' % slug, ContentFile(xml), save=False)
50         section.title = wldoc.book_info.title
51         section.save()
52
53         section.lesson_set.all().update(section=None)
54         for i, lesson in enumerate(lessons):
55             lesson.section = section
56             lesson.order = i
57             lesson.save()
58
59         return section
60
61     def syntetic_lesson(self, level):
62         try:
63             return self.lesson_set.filter(type='synthetic', level=level)[0]
64         except IndexError:
65             return None
66
67
68 class Lesson(models.Model):
69     section = models.ForeignKey(Section, null=True, blank=True)
70     level = models.ForeignKey(Level)
71     title = models.CharField(max_length=255)
72     slug = models.SlugField(max_length=255, unique=True)
73     type = models.CharField(max_length=15, db_index=True)
74     order = models.IntegerField(db_index=True)
75     dc = JSONField(default='{}')
76     curriculum_courses = models.ManyToManyField(CurriculumCourse, blank=True)
77
78     xml_file = models.FileField(upload_to="catalogue/lesson/xml",
79         null=True, blank=True, max_length=255)
80     html_file = models.FileField(upload_to="catalogue/lesson/html",
81         null=True, blank=True, max_length=255)
82     package = models.FileField(upload_to="catalogue/lesson/pack",
83         null=True, blank=True, max_length=255)
84     student_package = models.FileField(upload_to="catalogue/lesson/student_pack",
85         null=True, blank=True, max_length=255)
86     pdf = models.FileField(upload_to="catalogue/lesson/pdf",
87         null=True, blank=True, max_length=255)
88     student_pdf = models.FileField(upload_to="catalogue/lesson/student_pdf",
89         null=True, blank=True, max_length=255)
90
91     class Meta:
92         ordering = ['section', 'level', 'order']
93
94     def __unicode__(self):
95         return self.title
96
97     @models.permalink
98     def get_absolute_url(self):
99         return ('catalogue_lesson', [self.slug])
100
101     @classmethod
102     def publish(cls, infile):
103         from librarian.parser import WLDocument
104         from django.core.files.base import ContentFile
105         xml = infile.get_string()
106         wldoc = WLDocument.from_string(xml)
107
108         # Check if not section metadata block.
109         if wldoc.book_info.parts:
110             return Section.publish(infile)
111         
112         slug = wldoc.book_info.url.slug
113         try:
114             lesson = cls.objects.get(slug=slug)
115             lesson.attachment_set.all().delete()
116         except cls.DoesNotExist:
117             lesson = cls(slug=slug, order=0)
118
119         # Save XML file
120         lesson.xml_file.save('%s.xml' % slug, ContentFile(xml), save=False)
121         lesson.title = wldoc.book_info.title
122
123         lesson.level = Level.objects.get(slug=wldoc.book_info.audience)
124         lesson.populate_dc()
125         lesson.build_html(infile=infile)
126         lesson.build_pdf(infile=infile)
127         lesson.build_package()
128         if lesson.type != 'project':
129             lesson.build_pdf(student=True, infile=infile)
130             lesson.build_package(student=True)
131         return lesson
132
133     def populate_dc(self):
134         from librarian.parser import WLDocument
135         wldoc = WLDocument.from_file(self.xml_file.path)
136         self.dc = wldoc.book_info.to_dict()
137         self.type = self.dc["type"]
138         self.save()
139
140         courses = set()
141         for identifier in wldoc.book_info.curriculum:
142             try:
143                 curr = Curriculum.objects.get(identifier=identifier)
144             except Curriculum.DoesNotExist:
145                 pass
146             else:
147                 courses.add(curr.course)
148         self.curriculum_courses = courses
149
150     def wldocument(self, infile=None):
151         from librarian import IOFile
152         from librarian.parser import WLDocument
153         from .publish import OrmDocProvider
154
155         if infile is None:
156             infile = IOFile.from_filename(self.xml_file.path)
157             for att in self.attachment_set.all():
158                 infile.attachments["%s.%s" % (att.slug, att.ext)] = \
159                     IOFile.from_filename(att.file.path)
160         return WLDocument(infile, provider=OrmDocProvider())
161
162     def build_html(self, infile=None):
163         from .publish import HtmlFormat
164         wldoc = self.wldocument(infile)
165         html = HtmlFormat(wldoc).build()
166         self.html_file.save("%s.html" % self.slug,
167             File(open(html.get_filename())))
168
169     def build_pdf(self, student=False, infile=None):
170         from .publish import PdfFormat
171         wldoc = self.wldocument(infile)
172         if student:
173             pdf = PdfFormat(wldoc).build()
174             self.student_pdf.save("%s.pdf" % self.slug,
175                 File(open(pdf.get_filename())))
176         else:
177             pdf = PdfFormat(wldoc, teacher=True).build()
178             self.pdf.save("%s.pdf" % self.slug,
179                 File(open(pdf.get_filename())))
180
181     def add_to_zip(self, zipf, student=False, prefix=''):
182         pdf = self.student_pdf if student else self.pdf
183         if pdf:
184             zipf.write(pdf.path, 
185                 "%s%s%s.pdf" % (prefix, self.slug, "_student" if student else ""))
186             for attachment in self.attachment_set.all():
187                 zipf.write(attachment.file.path,
188                     u"%smaterialy/%s.%s" % (prefix, attachment.slug, attachment.ext))
189             zipf.write(self.xml_file.path,
190                 "%spliki-zrodlowe/%s.xml" % (prefix, self.slug))
191
192     def build_package(self, student=False):
193         from StringIO import StringIO
194         import zipfile
195         from django.core.files.base import ContentFile
196         buff = StringIO()
197         zipf = zipfile.ZipFile(buff, 'w', zipfile.ZIP_STORED)
198         self.add_to_zip(zipf, student)
199         zipf.close()
200         fieldname = "student_package" if student else "package"
201         getattr(self, fieldname).save(
202             "%s%s.zip" % (self.slug, "_student" if student else ""),
203             ContentFile(buff.getvalue()))
204
205     def get_syntetic(self):
206         if self.section is None: return None
207         return self.section.syntetic_lesson(self.level)
208
209     def get_other_level(self):
210         if self.section is None: return None
211         other_levels = self.section.lesson_set.exclude(level=self.level)
212         if other_levels.exists():
213             return other_levels[0].level
214
215     def get_previous(self):
216         if self.section is None: return None
217         try:
218             return self.section.lesson_set.filter(
219                 type=self.type, level=self.level,
220                 order__lt=self.order).order_by('-order')[0]
221         except IndexError:
222             return None
223
224     def get_next(self):
225         if self.section is None: return None
226         try:
227             return self.section.lesson_set.filter(
228                 type=self.type, level=self.level,
229                 order__gt=self.order).order_by('order')[0]
230         except IndexError:
231             return None
232
233
234 class Attachment(models.Model):
235     slug = models.CharField(max_length=255)
236     ext = models.CharField(max_length=15)
237     lesson = models.ForeignKey(Lesson)
238     file = models.FileField(upload_to="catalogue/attachment")
239
240     class Meta:
241         ordering = ['slug', 'ext']
242         unique_together = ['lesson', 'slug', 'ext']
243
244     def __unicode__(self):
245         return "%s.%s" % (self.slug, self.ext)
246
247
248 class Part(models.Model):
249     lesson = models.ForeignKey(Lesson)
250     pdf = models.FileField(upload_to="catalogue/part/pdf",
251         null=True, blank=True)
252     student_pdf = models.FileField(upload_to="catalogue/part/student_pdf",
253         null=True, blank=True)
254
255
256 class LessonStub(models.Model):
257     section = models.ForeignKey(Section, null=True, blank=True)
258     level = models.ForeignKey(Level)
259     title = models.CharField(max_length=255)
260     type = models.CharField(max_length=15, db_index=True)
261     order = models.IntegerField(db_index=True)
262
263     class Meta:
264         ordering = ['section', 'level', 'order']
265
266     def __unicode__(self):
267         return self.title