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 xml = infile.get_string()
66 wldoc = WLDocument.from_string(xml)
67 slug = wldoc.book_info.url.slug
70 lesson = cls.objects.get(slug=slug)
71 except cls.DoesNotExist:
72 lesson = cls(slug=slug)
74 lesson.attachment_set.all().delete()
75 for att_name, att_file in infile.attachments.items():
77 slug, ext = att_name.rsplit('.', 1)
79 slug, ext = att_name, ''
80 attachment = lesson.attachment_set.create(slug=slug, ext=ext)
81 attachment.file.save(att_name, ContentFile(att_file.get_string()))
84 lesson.xml_file.save('%s.xml' % slug, ContentFile(xml), save=False)
85 lesson.title = wldoc.book_info.title
87 lesson.level = Level.objects.get(slug=wldoc.book_info.audience)
89 lesson.section = Section.objects.all()[0]
95 lesson.build_package()
96 lesson.build_package(student=True)
99 def populate_dc(self):
100 from librarian.parser import WLDocument
101 wldoc = WLDocument.from_file(self.xml_file.path)
102 self.dc = wldoc.book_info.to_dict()
105 def build_html(self):
106 from librarian.parser import WLDocument
107 wldoc = WLDocument.from_file(self.xml_file.path)
108 html = wldoc.as_html()
109 self.html_file.save("%s.html" % self.slug,
110 File(open(html.get_filename())))
112 def build_package(self, student=False):
113 from StringIO import StringIO
115 from django.core.files.base import ContentFile
117 zipf = zipfile.ZipFile(buff, 'w', zipfile.ZIP_STORED)
118 zipf.write(self.xml_file.path, "pliki-zrodlowe/%s.xml" % self.slug)
119 pdf = self.student_pdf if student else self.pdf
121 zipf.write(self.xml_file.path,
122 "%s%s.pdf" % (self.slug, "_student" if student else ""))
124 fieldname = "student_package" if student else "package"
125 getattr(self, fieldname).save(
126 "%s%s.zip" % (self.slug, "_student" if student else ""),
127 ContentFile(buff.getvalue()))
130 class Attachment(models.Model):
131 slug = models.CharField(max_length=255)
132 ext = models.CharField(max_length=15)
133 lesson = models.ForeignKey(Lesson)
134 file = models.FileField(upload_to="catalogue/attachment")
137 ordering = ['slug', 'ext']
138 unique_together = ['lesson', 'slug', 'ext']
140 def __unicode__(self):
141 return "%s.%s" % (self.slug, self.ext)
144 class Part(models.Model):
145 lesson = models.ForeignKey(Lesson)
146 pdf = models.FileField(upload_to="catalogue/part/pdf",
147 null=True, blank=True)
148 student_pdf = models.FileField(upload_to="catalogue/part/student_pdf",
149 null=True, blank=True)