hide banner
[edumed.git] / catalogue / models.py
index ee07651..c73c762 100644 (file)
@@ -1,9 +1,11 @@
 # -*- coding: utf-8
 # -*- coding: utf-8
+from django.conf import settings
 from django.core.files import File
 from django.core.urlresolvers import reverse
 from django.db import models
 from jsonfield import JSONField
 from fnpdjango.storage import BofhFileSystemStorage
 from django.core.files import File
 from django.core.urlresolvers import reverse
 from django.db import models
 from jsonfield import JSONField
 from fnpdjango.storage import BofhFileSystemStorage
+
 from curriculum.models import Level, Curriculum, CurriculumCourse
 import logging
 
 from curriculum.models import Level, Curriculum, CurriculumCourse
 import logging
 
@@ -108,6 +110,9 @@ class Lesson(models.Model):
     student_pdf = models.FileField(
         upload_to="catalogue/lesson/student_pdf",
         null=True, blank=True, max_length=255, storage=bofh_storage)
     student_pdf = models.FileField(
         upload_to="catalogue/lesson/student_pdf",
         null=True, blank=True, max_length=255, storage=bofh_storage)
+    weasy_pdf = models.FileField(
+        upload_to="catalogue/lesson/weasy",
+        null=True, blank=True, max_length=255, storage=bofh_storage)
 
     class Meta:
         ordering = ['section', 'level', 'order']
 
     class Meta:
         ordering = ['section', 'level', 'order']
@@ -120,11 +125,10 @@ class Lesson(models.Model):
         return 'catalogue_lesson', [self.slug]
 
     @classmethod
         return 'catalogue_lesson', [self.slug]
 
     @classmethod
-    def publish(cls, infile, ignore_incomplete=False):
+    def publish(cls, infile, ignore_incomplete=False, repackage_level=False):
         from librarian.parser import WLDocument
         from django.core.files.base import ContentFile
         from librarian.parser import WLDocument
         from django.core.files.base import ContentFile
-        xml = infile.get_string()
-        wldoc = WLDocument.from_string(xml)
+        wldoc = WLDocument(infile)
 
         # Check if not section metadata block.
         if wldoc.book_info.parts:
 
         # Check if not section metadata block.
         if wldoc.book_info.parts:
@@ -138,7 +142,7 @@ class Lesson(models.Model):
             lesson = cls(slug=slug, order=0)
 
         # Save XML file
             lesson = cls(slug=slug, order=0)
 
         # Save XML file
-        lesson.xml_file.save('%s.xml' % slug, ContentFile(xml), save=False)
+        lesson.xml_file.save('%s.xml' % slug, ContentFile(infile.get_string()), save=False)
         lesson.title = wldoc.book_info.title
 
         lesson.level = Level.objects.get(meta_name=wldoc.book_info.audience)
         lesson.title = wldoc.book_info.title
 
         lesson.level = Level.objects.get(meta_name=wldoc.book_info.audience)
@@ -150,8 +154,23 @@ class Lesson(models.Model):
         if lesson.type != 'project':
             lesson.build_pdf(student=True)
             lesson.build_package(student=True)
         if lesson.type != 'project':
             lesson.build_pdf(student=True)
             lesson.build_package(student=True)
+        if repackage_level:
+            lesson.level.build_packages()
         return lesson
 
         return lesson
 
+    def republish(self, repackage_level=True, attachments=None):
+        from librarian import IOFile
+        import os.path
+        from django.conf import settings
+        if attachments is None:
+            attachments = {}
+            for attachment in self.attachment_set.all():
+                full_name = os.path.join(settings.MEDIA_ROOT, attachment.file.name)
+                f = IOFile.from_filename(full_name)
+                attachments['%s.%s' % (attachment.slug, attachment.ext)] = f
+        infile = IOFile.from_filename(self.xml_file.path, attachments=attachments)
+        Lesson.publish(infile, repackage_level=repackage_level)
+
     def populate_dc(self):
         from librarian.parser import WLDocument
         wldoc = WLDocument.from_file(self.xml_file.path)
     def populate_dc(self):
         from librarian.parser import WLDocument
         wldoc = WLDocument.from_file(self.xml_file.path)
@@ -220,6 +239,15 @@ class Lesson(models.Model):
             pdf = PdfFormat(wldoc, teacher=True).build()
             self.pdf.save("%s.pdf" % self.slug, File(open(pdf.get_filename())))
 
             pdf = PdfFormat(wldoc, teacher=True).build()
             self.pdf.save("%s.pdf" % self.slug, File(open(pdf.get_filename())))
 
+    def build_pdf_from_html(self, **kwargs):
+        from .publish import PdfFromHtmlFormat
+        wldoc = self.wldocument()
+        pdf = PdfFromHtmlFormat(
+            wldoc, media_root=settings.MEDIA_ROOT,
+            html_to_pdf_command=settings.HTML_TO_PDF_COMMAND,
+            **kwargs).build()
+        self.weasy_pdf.save("%s.pdf" % self.slug, File(open(pdf.get_filename())))
+
     def add_to_zip(self, zipf, student=False, prefix=''):
         pdf = self.student_pdf if student else self.pdf
         if pdf:
     def add_to_zip(self, zipf, student=False, prefix=''):
         pdf = self.student_pdf if student else self.pdf
         if pdf:
@@ -274,14 +302,18 @@ class Lesson(models.Model):
             return None
 
     def requires_internet(self):
             return None
 
     def requires_internet(self):
-        return 'internet' in self.dc.get('requires', [])
+        return any(requirement in self.dc.get('requires', []) for requirement in ('internet', 'Internet'))
+
+
+def attachment_path(instance, filename):
+    return 'catalogue/attachment/%s/%s' % (instance.lesson.slug, filename)
 
 
 class Attachment(models.Model):
     slug = models.CharField(max_length=255)
     ext = models.CharField(max_length=15)
     lesson = models.ForeignKey(Lesson)
 
 
 class Attachment(models.Model):
     slug = models.CharField(max_length=255)
     ext = models.CharField(max_length=15)
     lesson = models.ForeignKey(Lesson)
-    file = models.FileField(upload_to="catalogue/attachment", storage=bofh_storage)
+    file = models.FileField(upload_to=attachment_path, storage=bofh_storage, max_length=255)
 
     class Meta:
         ordering = ['slug', 'ext']
 
     class Meta:
         ordering = ['slug', 'ext']