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
8 from curriculum.models import Level, Curriculum, CurriculumCourse
11 bofh_storage = BofhFileSystemStorage()
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,
22 image = models.ImageField(upload_to="catalogue/section/image", null=True, blank=True)
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)
28 summary = models.TextField(blank=True, null=True)
33 class IncompleteError(BaseException):
36 def __unicode__(self):
39 def get_absolute_url(self):
40 return "%s#gimnazjum_%s" % (reverse("catalogue_lessons"), self.slug)
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)
50 for part in wldoc.book_info.parts:
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)
57 slug = wldoc.book_info.url.slug
59 section = cls.objects.get(slug=slug)
60 except cls.DoesNotExist:
61 section = cls(slug=slug, order=0)
64 section.xml_file.save('%s.xml' % slug, ContentFile(xml), save=False)
65 section.title = wldoc.book_info.title
68 section.lesson_set.all().update(section=None)
69 for i, lesson in enumerate(lessons):
70 lesson.section = section
76 def syntetic_lesson(self, level):
78 return self.lesson_set.filter(type='synthetic', level=level)[0]
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)
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)
114 ordering = ['section', 'level', 'order']
116 def __unicode__(self):
120 def get_absolute_url(self):
121 return 'catalogue_lesson', [self.slug]
124 def publish(cls, infile, ignore_incomplete=False, repackage_level=False):
125 from librarian.parser import WLDocument
126 from django.core.files.base import ContentFile
127 wldoc = WLDocument(infile)
129 # Check if not section metadata block.
130 if wldoc.book_info.parts:
131 return Section.publish(infile, ignore_incomplete=ignore_incomplete)
133 slug = wldoc.book_info.url.slug
135 lesson = cls.objects.get(slug=slug)
136 lesson.attachment_set.all().delete()
137 except cls.DoesNotExist:
138 lesson = cls(slug=slug, order=0)
141 lesson.xml_file.save('%s.xml' % slug, ContentFile(infile.get_string()), save=False)
142 lesson.title = wldoc.book_info.title
144 lesson.level = Level.objects.get(meta_name=wldoc.book_info.audience)
146 lesson.populate_description(wldoc=wldoc)
147 lesson.build_html(infile=infile)
149 lesson.build_package()
150 if lesson.type != 'project':
151 lesson.build_pdf(student=True)
152 lesson.build_package(student=True)
154 lesson.level.build_packages()
157 def republish(self, repackage_level=True, attachments=None):
158 from librarian import IOFile
160 from django.conf import settings
161 if attachments is None:
163 for attachment in self.attachment_set.all():
164 full_name = os.path.join(settings.MEDIA_ROOT, attachment.file.name)
165 f = IOFile.from_filename(full_name)
166 attachments['%s.%s' % (attachment.slug, attachment.ext)] = f
167 infile = IOFile.from_filename(self.xml_file.path, attachments=attachments)
168 Lesson.publish(infile, repackage_level=repackage_level)
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
180 for identifier in wldoc.book_info.curriculum:
181 identifier = (identifier or "").replace(' ', '')
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))
190 courses.add(curr.course)
191 self.curriculum_courses = courses
193 def populate_description(self, wldoc=None, infile=None):
195 wldoc = self.wldocument(infile)
196 if self.type == 'project':
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()
208 def wldocument(self, infile=None):
209 from librarian import IOFile
210 from librarian.parser import WLDocument
211 from .publish import OrmDocProvider
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())
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())))
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()
232 pdf = PdfFormat(wldoc).build()
233 self.student_pdf.save("%s.pdf" % self.slug, File(open(pdf.get_filename())))
235 pdf = PdfFormat(wldoc, teacher=True).build()
236 self.pdf.save("%s.pdf" % self.slug, File(open(pdf.get_filename())))
238 def add_to_zip(self, zipf, student=False, prefix=''):
239 pdf = self.student_pdf if student else self.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))
246 def build_package(self, student=False):
247 from StringIO import StringIO
249 from django.core.files.base import ContentFile
251 zipf = zipfile.ZipFile(buff, 'w', zipfile.ZIP_STORED)
252 self.add_to_zip(zipf, student)
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()))
259 def get_syntetic(self):
260 if self.section is None:
262 return self.section.syntetic_lesson(self.level)
264 def get_other_level(self):
265 if self.section is None:
267 other_levels = self.section.lesson_set.exclude(level=self.level)
268 if other_levels.exists():
269 return other_levels[0].level
271 def get_previous(self):
272 if self.section is None:
275 return self.section.lesson_set.filter(
276 type=self.type, level=self.level,
277 order__lt=self.order).order_by('-order')[0]
282 if self.section is None:
285 return self.section.lesson_set.filter(
286 type=self.type, level=self.level,
287 order__gt=self.order).order_by('order')[0]
291 def requires_internet(self):
292 return any(requirement in self.dc.get('requires', []) for requirement in ('internet', 'Internet'))
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, max_length=255)
302 ordering = ['slug', 'ext']
303 unique_together = ['lesson', 'slug', 'ext']
305 def __unicode__(self):
306 return "%s.%s" % (self.slug, self.ext)
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)
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)
323 ordering = ['section', 'level', 'order']
325 def __unicode__(self):
332 def add_to_zip(self, *args, **kwargs):