X-Git-Url: https://git.mdrn.pl/edumed.git/blobdiff_plain/39844fcccd43fc77f6beb350f5792bb0859135df..2b5915c86273728f6f1936f2c4e704edc6d19222:/catalogue/models.py diff --git a/catalogue/models.py b/catalogue/models.py index 8ff6be4..03606c0 100644 --- a/catalogue/models.py +++ b/catalogue/models.py @@ -1,7 +1,10 @@ from django.core.files import File +from django.core.urlresolvers import reverse from django.db import models +from jsonfield import JSONField from curriculum.models import Level + class Section(models.Model): title = models.CharField(max_length=255, unique=True) slug = models.SlugField(unique=True) @@ -13,6 +16,16 @@ class Section(models.Model): def __unicode__(self): return self.title + def get_absolute_url(self): + return "%s#%s" % (reverse("catalogue_lessons"), self.slug) + + def syntetic_lesson(self): + try: + return self.lesson_set.filter(depth=0)[0] + except IndexError: + return None + + class Lesson(models.Model): section = models.ForeignKey(Section) level = models.ForeignKey(Level) @@ -20,6 +33,7 @@ class Lesson(models.Model): slug = models.SlugField(unique=True) depth = models.IntegerField() order = models.IntegerField() + dc = JSONField(default='{}') xml_file = models.FileField(upload_to="catalogue/lesson/xml", null=True, blank=True) # FIXME: slug in paths @@ -70,14 +84,42 @@ class Lesson(models.Model): lesson.section = Section.objects.all()[0] lesson.order = 1 lesson.depth = 1 - - # Build HTML. - html = wldoc.as_html() - lesson.html_file.save("%s.html" % slug, - File(open(html.get_filename())), save=False) + 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() + with zipfile.ZipFile(buff, 'w', zipfile.ZIP_STORED) as zipf: + 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 "")) + 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)