03606c0c42387a8c3f47f68f578442978a3561da
[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         # infile should be IOFile, now it's a regular file
66         xml = infile.read()
67         wldoc = WLDocument.from_string(xml)
68         slug = wldoc.book_info.url.slug
69
70         try:
71             lesson = cls.objects.get(slug=slug)
72         except cls.DoesNotExist:
73             lesson = cls(slug=slug)
74
75         # Save XML file
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()
79
80         # FIXME: 
81         #lesson.level = Level.objects.get(slug=wldoc.book_info.audience)
82         lesson.level = Level.objects.get(name=wldoc.book_info.audience)
83         # TODO: no xml data?
84         lesson.section = Section.objects.all()[0]
85         lesson.order = 1
86         lesson.depth = 1
87         lesson.populate_dc()
88         lesson.save()
89         lesson.build_html()
90         lesson.build_package()
91         lesson.build_package(student=True)
92         return lesson
93
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()
98         self.save()
99
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())))
106
107     def build_package(self, student=False):
108         from StringIO import StringIO
109         import zipfile
110         from django.core.files.base import ContentFile
111         buff = StringIO()
112         with zipfile.ZipFile(buff, 'w', zipfile.ZIP_STORED) as zipf:
113             zipf.write(self.xml_file.path, "pliki-zrodlowe/%s.xml" % self.slug)
114             pdf = self.student_pdf if student else self.pdf
115             if pdf:
116                 zipf.write(self.xml_file.path, 
117                     "%s%s.pdf" % (self.slug, "_student" if student else ""))
118         fieldname = "student_package" if student else "package"
119         getattr(self, fieldname).save(
120             "%s%s.zip" % (self.slug, "_student" if student else ""),
121             ContentFile(buff.getvalue()))
122
123
124 class Attachment(models.Model):
125     lesson = models.ForeignKey(Lesson)
126     file = models.FileField(upload_to="catalogue/attachment")
127
128
129 class Part(models.Model):
130     lesson = models.ForeignKey(Lesson)
131     pdf = models.FileField(upload_to="catalogue/part/pdf",
132         null=True, blank=True)
133     student_pdf = models.FileField(upload_to="catalogue/part/student_pdf",
134         null=True, blank=True)