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