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
8 class Section(models.Model):
9 title = models.CharField(max_length=255, unique=True)
10 slug = models.SlugField(unique=True)
11 order = models.IntegerField()
12 xml_file = models.FileField(upload_to="catalogue/section/xml",
13 null=True, blank=True)
18 class IncompleteError(BaseException):
21 def __unicode__(self):
24 def get_absolute_url(self):
25 return "%s#%s" % (reverse("catalogue_lessons"), self.slug)
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)
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)
40 slug = wldoc.book_info.url.slug
42 section = cls.objects.get(slug=slug)
43 except cls.DoesNotExist:
44 section = cls(slug=slug, order=0)
47 section.xml_file.save('%s.xml' % slug, ContentFile(xml), save=False)
48 section.title = wldoc.book_info.title
51 section.lesson_set.all().update(section=None)
52 for i, lesson in enumerate(lessons):
53 lesson.section = section
59 def syntetic_lesson(self, level):
61 return self.lesson_set.filter(type='synthetic', level=level)[0]
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(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)
76 xml_file = models.FileField(upload_to="catalogue/lesson/xml",
77 null=True, blank=True) # FIXME: slug in paths
78 html_file = models.FileField(upload_to="catalogue/lesson/html",
79 null=True, blank=True)
80 package = models.FileField(upload_to="catalogue/lesson/pack",
81 null=True, blank=True)
82 student_package = models.FileField(upload_to="catalogue/lesson/student_pack",
83 null=True, blank=True)
84 pdf = models.FileField(upload_to="catalogue/lesson/pdf",
85 null=True, blank=True)
86 student_pdf = models.FileField(upload_to="catalogue/lesson/student_pdf",
87 null=True, blank=True)
90 ordering = ['section', 'level', 'order']
92 def __unicode__(self):
96 def get_absolute_url(self):
97 return ('catalogue_lesson', [self.slug])
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)
106 # Check if not section metadata block.
107 if wldoc.book_info.parts:
108 return Section.publish(infile)
110 slug = wldoc.book_info.url.slug
112 lesson = cls.objects.get(slug=slug)
113 lesson.attachment_set.all().delete()
114 except cls.DoesNotExist:
115 lesson = cls(slug=slug, order=0)
118 lesson.xml_file.save('%s.xml' % slug, ContentFile(xml), save=False)
119 lesson.title = wldoc.book_info.title
121 lesson.level = Level.objects.get(slug=wldoc.book_info.audience)
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)
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"]
138 for identifier in wldoc.book_info.curriculum:
140 curr = Curriculum.objects.get(identifier=identifier)
141 except Curriculum.DoesNotExist:
144 courses.add(curr.course)
145 self.curriculum_courses = courses
147 def build_html(self, infile=None):
148 from librarian.parser import WLDocument
149 from .publish import HtmlFormat, OrmDocProvider
152 wldoc = WLDocument.from_file(self.xml_file.path, provider=OrmDocProvider)
154 wldoc = WLDocument(infile, provider=OrmDocProvider())
155 html = HtmlFormat(wldoc).build()
156 self.html_file.save("%s.html" % self.slug,
157 File(open(html.get_filename())))
159 def build_pdf(self, student=False, infile=None):
160 from librarian.parser import WLDocument
161 from .publish import PdfFormat, OrmDocProvider
164 wldoc = WLDocument.from_file(self.xml_file.path, provider=OrmDocProvider)
166 wldoc = WLDocument(infile, provider=OrmDocProvider())
168 pdf = PdfFormat(wldoc).build()
169 self.student_pdf.save("%s.pdf" % self.slug,
170 File(open(pdf.get_filename())))
172 pdf = PdfFormat(wldoc, teacher=True).build()
173 self.pdf.save("%s.pdf" % self.slug,
174 File(open(pdf.get_filename())))
176 def add_to_zip(self, zipf, student=False, prefix=''):
177 zipf.write(self.xml_file.path,
178 "%spliki-zrodlowe/%s.xml" % (prefix, self.slug))
179 pdf = self.student_pdf if student else self.pdf
182 "%s%s%s.pdf" % (prefix, self.slug, "_student" if student else ""))
183 for attachment in self.attachment_set.all():
184 zipf.write(attachment.file.path,
185 u"%smaterialy/%s.%s" % (prefix, attachment.slug, attachment.ext))
189 def build_package(self, student=False):
190 from StringIO import StringIO
192 from django.core.files.base import ContentFile
194 zipf = zipfile.ZipFile(buff, 'w', zipfile.ZIP_STORED)
195 self.add_to_zip(zipf, student)
197 fieldname = "student_package" if student else "package"
198 getattr(self, fieldname).save(
199 "%s%s.zip" % (self.slug, "_student" if student else ""),
200 ContentFile(buff.getvalue()))
202 def get_syntetic(self):
203 return self.section.syntetic_lesson(self.level)
205 def get_previous(self):
206 if self.section is None: return None
208 return self.section.lesson_set.filter(
209 type=self.type, level=self.level,
210 order__lt=self.order).order_by('-order')[0]
215 if self.section is None: return None
217 return self.section.lesson_set.filter(
218 type=self.type, level=self.level,
219 order__gt=self.order).order_by('order')[0]
224 class Attachment(models.Model):
225 slug = models.CharField(max_length=255)
226 ext = models.CharField(max_length=15)
227 lesson = models.ForeignKey(Lesson)
228 file = models.FileField(upload_to="catalogue/attachment")
231 ordering = ['slug', 'ext']
232 unique_together = ['lesson', 'slug', 'ext']
234 def __unicode__(self):
235 return "%s.%s" % (self.slug, self.ext)
238 class Part(models.Model):
239 lesson = models.ForeignKey(Lesson)
240 pdf = models.FileField(upload_to="catalogue/part/pdf",
241 null=True, blank=True)
242 student_pdf = models.FileField(upload_to="catalogue/part/student_pdf",
243 null=True, blank=True)