3 from django.core.urlresolvers import reverse
4 from django.db import models
5 from django.utils.translation import ugettext_lazy as _, get_language
6 from fnpdjango.storage import BofhFileSystemStorage
7 from fnpdjango.utils.models.translation import add_translatable
9 bofh_storage = BofhFileSystemStorage()
12 class Section(models.Model):
13 slug = models.SlugField(_('slug'))
14 order = models.IntegerField(_('order'))
18 verbose_name = _('section')
19 verbose_name_plural = _('sections')
21 def __unicode__(self):
24 def get_absolute_url(self):
25 return "%s?s=%d" % (reverse("curriculum"), self.pk)
27 def url_for_level(self, level):
28 return "%s?s=%d&level=%s&d=1" % (reverse("curriculum"), self.pk, level.slug)
30 add_translatable(Section, {
31 'name': models.CharField(_('name'), max_length=255, default='')
35 class Competence(models.Model):
36 section = models.ForeignKey(Section)
37 slug = models.SlugField(_('slug'))
38 order = models.IntegerField(_('order'))
41 ordering = ['section', 'order']
42 verbose_name = _('competence')
43 verbose_name_plural = _('competences')
45 def __unicode__(self):
48 def get_absolute_url(self):
49 return "%s?c=%d" % (reverse("curriculum"), self.pk)
51 def for_level(self, level):
52 return self.competencelevel_set.get(level=level)
54 def url_for_level(self, level):
55 return self.for_level(level).get_absolute_url()
58 def from_text(cls, text):
59 """Tries to return a Competence or a Section."""
60 parts = re.split(ur'[-\u2013]', text, 1)
61 lookup_field_name = 'name_%s__iexact' % get_language()
63 return Section.objects.get(**{lookup_field_name: text.strip()})
65 return cls.objects.get(**{lookup_field_name: parts[1].strip()})
67 add_translatable(Competence, {
68 'name': models.CharField(_('name'), max_length=255, default='')
72 class Level(models.Model):
73 slug = models.CharField(_('slug'), max_length=255, unique=True)
74 meta_name = models.CharField(_('meta name'), max_length=255, unique=True)
75 order = models.IntegerField(_('order'))
76 package = models.FileField(
77 upload_to=lambda i, f: "curriculum/pack/edukacjamedialna_%s.zip" % i.slug,
78 null=True, blank=True, max_length=255, storage=bofh_storage)
79 student_package = models.FileField(
80 upload_to=lambda i, f: "curriculum/pack/edukacjamedialna_%s_uczen.zip" % i.slug,
81 null=True, blank=True, max_length=255, storage=bofh_storage)
85 verbose_name = _('educational level')
86 verbose_name_plural = _('educational levels')
88 def __unicode__(self):
91 def length_course(self):
92 return self.lesson_set.filter(type='course').count()
94 def length_synthetic(self):
95 return self.lesson_set.filter(type='synthetic').count()
97 def build_package(self, student):
98 from StringIO import StringIO
100 from django.core.files.base import ContentFile
101 from catalogue.templatetags.catalogue_tags import level_box
102 from catalogue.models import Lesson
105 zipf = zipfile.ZipFile(buff, 'w', zipfile.ZIP_STORED)
107 lessons = level_box(self)['lessons']
108 for i, lesson in enumerate(lessons['synthetic']):
109 prefix = 'Skrocony kurs/%d %s/' % (i, lesson.slug)
110 lesson.add_to_zip(zipf, student, prefix)
111 for c, (section, clessons) in enumerate(lessons['course'].items()):
112 assert section, clessons
113 for i, lesson in enumerate(clessons):
114 prefix = 'Pelny kurs/%d %s/%d %s/' % (c, section.slug, i, lesson.slug)
115 lesson.add_to_zip(zipf, student, prefix)
116 for i, lesson in enumerate(lessons['project']):
117 prefix = 'Projekty/%d %s/' % (i, lesson.slug)
118 lesson.add_to_zip(zipf, student, prefix)
119 # Add all appendix lessons, from all levels.
120 for lesson in Lesson.objects.filter(type='appendix'):
122 if self.slug in ('przedszkole', 'sp1-3', 'sp4-6'):
123 if lesson.slug == 'slowniczek':
126 if lesson.slug == 'slowniczek-sp':
128 prefix = '%s/' % lesson.slug
129 lesson.add_to_zip(zipf, student, prefix)
132 fieldname = "student_package" if student else "package"
133 getattr(self, fieldname).save(None, ContentFile(buff.getvalue()))
135 def build_packages(self):
136 self.build_package(False)
137 self.build_package(True)
140 add_translatable(Level, {
141 'name': models.CharField(_('name'), max_length=255, default=''),
142 'group': models.CharField(_('group'), max_length=255, default='')
146 class CompetenceLevel(models.Model):
147 competence = models.ForeignKey(Competence)
148 level = models.ForeignKey(Level)
151 ordering = ['competence', 'level']
152 verbose_name = _('competence on level')
153 verbose_name_plural = _('competences on levels')
155 def __unicode__(self):
156 return u"%s/%s" % (self.competence, self.level)
158 def get_absolute_url(self):
159 return "%s?c=%d&level=%s&d=1" % (reverse("curriculum"), self.competence.pk, self.level.slug)
161 add_translatable(CompetenceLevel, {
162 'description': models.TextField(_('description'), default='')
166 class CurriculumLevel(models.Model):
167 title = models.CharField(max_length=16, db_index=True)
170 verbose_name = _("curriculum level")
171 verbose_name_plural = _("curriculum levels")
173 def __unicode__(self):
177 class CurriculumCourse(models.Model):
178 title = models.CharField(max_length=255)
179 accusative = models.CharField(max_length=255)
180 slug = models.CharField(max_length=255, db_index=True)
183 verbose_name = _("curriculum course")
184 verbose_name_plural = _("curriculum courses")
187 def __unicode__(self):
191 class Curriculum(models.Model):
192 """Official curriculum."""
193 TYPES = {'c': u'Cele kształcenia', 't': u'Treści nauczania'}
195 identifier = models.CharField(max_length=255, db_index=True)
196 title = models.CharField(max_length=255)
197 course = models.ForeignKey(CurriculumCourse)
198 level = models.ForeignKey(CurriculumLevel)
199 type = models.CharField(max_length=16, choices=TYPES.items())
202 verbose_name = _("curriculum item")
203 verbose_name_plural = _("curriculum items")
205 def __unicode__(self):
206 return self.identifier
209 def from_text(cls, identifier, title):
210 m = re.match(r"^\d+/(?P<level>[^/]+)/(?P<course>[^/]+)/"
211 r"(?P<type>(?:%s))[^/]+(?P<roz>/roz)?" % "|".join(cls.TYPES), identifier)
212 assert m is not None, "Curriculum identifier doesn't match template."
213 level, created = CurriculumLevel.objects.get_or_create(
214 title=m.group('level'))
215 def_title = m.group('course').title()
216 course, created = CurriculumCourse.objects.get_or_create(
217 slug=m.group('course').lower(),
220 'accusative': def_title,
222 type_ = m.group('type')
224 title += " (zakres rozszerzony)"
227 curr = cls.objects.get(identifier=identifier)
228 except cls.DoesNotExist:
229 curr = cls(identifier=identifier)