move to librarian 1.5 IOFile, add some curriculum links
[edumed.git] / catalogue / models.py
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
6
7
8 class Section(models.Model):
9     title = models.CharField(max_length=255, unique=True)
10     slug = models.SlugField(unique=True)
11     order = models.IntegerField()
12
13     class Meta:
14         ordering = ['order']
15
16     def __unicode__(self):
17         return self.title
18
19     def get_absolute_url(self):
20         return "%s#%s" % (reverse("catalogue_lessons"), self.slug)
21
22     def syntetic_lesson(self):
23         try:
24             return self.lesson_set.filter(depth=0)[0]
25         except IndexError:
26             return None
27
28
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='{}')
37
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)
50
51     class Meta:
52         ordering = ['section', 'level', 'depth', 'order']
53
54     def __unicode__(self):
55         return self.title
56
57     @models.permalink
58     def get_absolute_url(self):
59         return ('catalogue_lesson', [self.slug])
60
61     @classmethod
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
68
69         try:
70             lesson = cls.objects.get(slug=slug)
71         except cls.DoesNotExist:
72             lesson = cls(slug=slug)
73
74         lesson.attachment_set.all().delete()
75         for att_name, att_file in infile.attachments.items():
76             try:
77                 slug, ext = att_name.rsplit('.', 1)
78             except ValueError:
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()))
82
83         # Save XML file
84         lesson.xml_file.save('%s.xml' % slug, ContentFile(xml), save=False)
85         lesson.title = wldoc.book_info.title
86
87         lesson.level = Level.objects.get(slug=wldoc.book_info.audience)
88         # TODO: no xml data?
89         lesson.section = Section.objects.all()[0]
90         lesson.order = 1
91         lesson.depth = 1
92         lesson.populate_dc()
93         lesson.save()
94         lesson.build_html()
95         lesson.build_package()
96         lesson.build_package(student=True)
97         return lesson
98
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()
103         self.save()
104
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())))
111
112     def build_package(self, student=False):
113         from StringIO import StringIO
114         import zipfile
115         from django.core.files.base import ContentFile
116         buff = StringIO()
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
120         if pdf:
121             zipf.write(self.xml_file.path, 
122                 "%s%s.pdf" % (self.slug, "_student" if student else ""))
123         zipf.close()
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()))
128
129
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")
135
136     class Meta:
137         ordering = ['slug', 'ext']
138         unique_together = ['lesson', 'slug', 'ext']
139
140     def __unicode__(self):
141         return "%s.%s" % (self.slug, self.ext)
142
143
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)