+
+ @classmethod
+ def publish(cls, infile):
+ from librarian.parser import WLDocument
+ from django.core.files.base import ContentFile
+ # infile should be IOFile, now it's a regular file
+ xml = infile.read()
+ wldoc = WLDocument.from_string(xml)
+ slug = wldoc.book_info.url.slug
+
+ try:
+ lesson = cls.objects.get(slug=slug)
+ except cls.DoesNotExist:
+ lesson = cls(slug=slug)
+
+ # Save XML file
+ lesson.xml_file.save('%s.xml' % slug, ContentFile(xml), save=False)
+ lesson.title = wldoc.book_info.title
+ #book.extra_info = wldoc.book_info.to_dict()
+
+ # FIXME:
+ #lesson.level = Level.objects.get(slug=wldoc.book_info.audience)
+ lesson.level = Level.objects.get(name=wldoc.book_info.audience)
+ # TODO: no xml data?
+ lesson.section = Section.objects.all()[0]
+ lesson.order = 1
+ lesson.depth = 1
+ lesson.populate_dc()
+ lesson.save()
+ lesson.build_html()
+ lesson.build_package()
+ lesson.build_package(student=True)
+ return lesson
+
+ def populate_dc(self):
+ from librarian.parser import WLDocument
+ wldoc = WLDocument.from_file(self.xml_file.path)
+ self.dc = wldoc.book_info.to_dict()
+ self.save()
+
+ def build_html(self):
+ from librarian.parser import WLDocument
+ wldoc = WLDocument.from_file(self.xml_file.path)
+ html = wldoc.as_html()
+ self.html_file.save("%s.html" % self.slug,
+ File(open(html.get_filename())))
+
+ def build_package(self, student=False):
+ from StringIO import StringIO
+ import zipfile
+ from django.core.files.base import ContentFile
+ buff = StringIO()
+ zipf = zipfile.ZipFile(buff, 'w', zipfile.ZIP_STORED)
+ zipf.write(self.xml_file.path, "pliki-zrodlowe/%s.xml" % self.slug)
+ pdf = self.student_pdf if student else self.pdf
+ if pdf:
+ zipf.write(self.xml_file.path,
+ "%s%s.pdf" % (self.slug, "_student" if student else ""))
+ zipf.close()
+ fieldname = "student_package" if student else "package"
+ getattr(self, fieldname).save(
+ "%s%s.zip" % (self.slug, "_student" if student else ""),
+ ContentFile(buff.getvalue()))
+
+
+class Attachment(models.Model):
+ lesson = models.ForeignKey(Lesson)
+ file = models.FileField(upload_to="catalogue/attachment")
+
+
+class Part(models.Model):
+ lesson = models.ForeignKey(Lesson)
+ pdf = models.FileField(upload_to="catalogue/part/pdf",
+ null=True, blank=True)
+ student_pdf = models.FileField(upload_to="catalogue/part/student_pdf",
+ null=True, blank=True)