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
8 class Section(models.Model):
9 title = models.CharField(max_length=255, unique=True)
10 slug = models.SlugField(unique=True)
11 order = models.IntegerField()
16 def __unicode__(self):
19 def get_absolute_url(self):
20 return "%s#%s" % (reverse("catalogue_lessons"), self.slug)
22 def syntetic_lesson(self):
24 return self.lesson_set.filter(depth=0)[0]
29 class Lesson(models.Model):
30 section = models.ForeignKey(Section)
31 level = models.ForeignKey(Level)
32 title = models.CharField(max_length=255)
33 slug = models.SlugField(unique=True)
34 depth = models.IntegerField()
35 order = models.IntegerField()
36 dc = JSONField(default='{}')
38 xml_file = models.FileField(upload_to="catalogue/lesson/xml",
39 null=True, blank=True) # FIXME: slug in paths
40 html_file = models.FileField(upload_to="catalogue/lesson/html",
41 null=True, blank=True)
42 package = models.FileField(upload_to="catalogue/lesson/pack",
43 null=True, blank=True)
44 student_package = models.FileField(upload_to="catalogue/lesson/student_pack",
45 null=True, blank=True)
46 pdf = models.FileField(upload_to="catalogue/lesson/pdf",
47 null=True, blank=True)
48 student_pdf = models.FileField(upload_to="catalogue/lesson/student_pdf",
49 null=True, blank=True)
52 ordering = ['section', 'level', 'depth', 'order']
54 def __unicode__(self):
58 def get_absolute_url(self):
59 return ('catalogue_lesson', [self.slug])
62 def publish(cls, infile):
63 from librarian.parser import WLDocument
64 from django.core.files.base import ContentFile
65 # infile should be IOFile, now it's a regular file
67 wldoc = WLDocument.from_string(xml)
68 slug = wldoc.book_info.url.slug
71 lesson = cls.objects.get(slug=slug)
72 except cls.DoesNotExist:
73 lesson = cls(slug=slug)
76 lesson.xml_file.save('%s.xml' % slug, ContentFile(xml), save=False)
77 lesson.title = wldoc.book_info.title
78 #book.extra_info = wldoc.book_info.to_dict()
81 #lesson.level = Level.objects.get(slug=wldoc.book_info.audience)
82 lesson.level = Level.objects.get(name=wldoc.book_info.audience)
84 lesson.section = Section.objects.all()[0]
90 lesson.build_package()
91 lesson.build_package(student=True)
94 def populate_dc(self):
95 from librarian.parser import WLDocument
96 wldoc = WLDocument.from_file(self.xml_file.path)
97 self.dc = wldoc.book_info.to_dict()
100 def build_html(self):
101 from librarian.parser import WLDocument
102 wldoc = WLDocument.from_file(self.xml_file.path)
103 html = wldoc.as_html()
104 self.html_file.save("%s.html" % self.slug,
105 File(open(html.get_filename())))
107 def build_package(self, student=False):
108 from StringIO import StringIO
110 from django.core.files.base import ContentFile
112 zipf = zipfile.ZipFile(buff, 'w', zipfile.ZIP_STORED)
113 zipf.write(self.xml_file.path, "pliki-zrodlowe/%s.xml" % self.slug)
114 pdf = self.student_pdf if student else self.pdf
116 zipf.write(self.xml_file.path,
117 "%s%s.pdf" % (self.slug, "_student" if student else ""))
119 fieldname = "student_package" if student else "package"
120 getattr(self, fieldname).save(
121 "%s%s.zip" % (self.slug, "_student" if student else ""),
122 ContentFile(buff.getvalue()))
125 class Attachment(models.Model):
126 lesson = models.ForeignKey(Lesson)
127 file = models.FileField(upload_to="catalogue/attachment")
130 class Part(models.Model):
131 lesson = models.ForeignKey(Lesson)
132 pdf = models.FileField(upload_to="catalogue/part/pdf",
133 null=True, blank=True)
134 student_pdf = models.FileField(upload_to="catalogue/part/student_pdf",
135 null=True, blank=True)