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(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)
20 class IncompleteError(BaseException):
23 def __unicode__(self):
26 def get_absolute_url(self):
27 return "%s#%s" % (reverse("catalogue_lessons"), self.slug)
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)
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)
42 slug = wldoc.book_info.url.slug
44 section = cls.objects.get(slug=slug)
45 except cls.DoesNotExist:
46 section = cls(slug=slug, order=0)
49 section.xml_file.save('%s.xml' % slug, ContentFile(xml), save=False)
50 section.title = wldoc.book_info.title
53 section.lesson_set.all().update(section=None)
54 for i, lesson in enumerate(lessons):
55 lesson.section = section
61 def syntetic_lesson(self, level):
63 return self.lesson_set.filter(type='synthetic', level=level)[0]
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)
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)
92 ordering = ['section', 'level', 'order']
94 def __unicode__(self):
98 def get_absolute_url(self):
99 return ('catalogue_lesson', [self.slug])
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)
108 # Check if not section metadata block.
109 if wldoc.book_info.parts:
110 return Section.publish(infile)
112 slug = wldoc.book_info.url.slug
114 lesson = cls.objects.get(slug=slug)
115 lesson.attachment_set.all().delete()
116 except cls.DoesNotExist:
117 lesson = cls(slug=slug, order=0)
120 lesson.xml_file.save('%s.xml' % slug, ContentFile(xml), save=False)
121 lesson.title = wldoc.book_info.title
123 lesson.level = Level.objects.get(slug=wldoc.book_info.audience)
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)
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"]
141 for identifier in wldoc.book_info.curriculum:
143 curr = Curriculum.objects.get(identifier=identifier)
144 except Curriculum.DoesNotExist:
147 courses.add(curr.course)
148 self.curriculum_courses = courses
150 def wldocument(self, infile=None):
151 from librarian import IOFile
152 from librarian.parser import WLDocument
153 from .publish import OrmDocProvider
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())
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())))
169 def build_pdf(self, student=False, infile=None):
170 from .publish import PdfFormat
171 wldoc = self.wldocument(infile)
173 pdf = PdfFormat(wldoc).build()
174 self.student_pdf.save("%s.pdf" % self.slug,
175 File(open(pdf.get_filename())))
177 pdf = PdfFormat(wldoc, teacher=True).build()
178 self.pdf.save("%s.pdf" % self.slug,
179 File(open(pdf.get_filename())))
181 def add_to_zip(self, zipf, student=False, prefix=''):
182 pdf = self.student_pdf if student else self.pdf
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))
192 def build_package(self, student=False):
193 from StringIO import StringIO
195 from django.core.files.base import ContentFile
197 zipf = zipfile.ZipFile(buff, 'w', zipfile.ZIP_STORED)
198 self.add_to_zip(zipf, student)
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()))
205 def get_syntetic(self):
206 if self.section is None: return None
207 return self.section.syntetic_lesson(self.level)
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
215 def get_previous(self):
216 if self.section is None: return None
218 return self.section.lesson_set.filter(
219 type=self.type, level=self.level,
220 order__lt=self.order).order_by('-order')[0]
225 if self.section is None: return None
227 return self.section.lesson_set.filter(
228 type=self.type, level=self.level,
229 order__gt=self.order).order_by('order')[0]
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")
241 ordering = ['slug', 'ext']
242 unique_together = ['lesson', 'slug', 'ext']
244 def __unicode__(self):
245 return "%s.%s" % (self.slug, self.ext)
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)
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)
264 ordering = ['section', 'level', 'order']
266 def __unicode__(self):