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
8 from fnpdjango.utils.text.slughifi import slughifi as slugify
10 bofh_storage = BofhFileSystemStorage()
13 class Section(models.Model):
14 slug = models.SlugField(_('slug'))
15 order = models.IntegerField(_('order'))
19 verbose_name = _('section')
20 verbose_name_plural = _('sections')
22 def __unicode__(self):
25 def get_absolute_url(self):
26 return "%s?s=%d" % (reverse("curriculum"), self.pk)
28 def url_for_level(self, level):
29 return "%s?s=%d&level=%s&d=1" % (reverse("curriculum"), self.pk, level.slug)
31 add_translatable(Section, {
32 'name': models.CharField(_('name'), max_length=255, default='')
36 class Competence(models.Model):
37 section = models.ForeignKey(Section)
38 slug = models.SlugField(_('slug'))
39 order = models.IntegerField(_('order'))
42 ordering = ['section', 'order']
43 verbose_name = _('competence')
44 verbose_name_plural = _('competences')
46 def __unicode__(self):
49 def get_absolute_url(self):
50 return "%s?c=%d" % (reverse("curriculum"), self.pk)
52 def for_level(self, level):
53 return self.competencelevel_set.get(level=level)
55 def url_for_level(self, level):
56 return self.for_level(level).get_absolute_url()
59 def from_text(cls, text):
60 """Tries to return a Competence or a Section."""
61 parts = re.split(ur'[-\u2013]', text, 1)
62 lookup_field_name = 'name_%s__iexact' % get_language()
64 return Section.objects.get(**{lookup_field_name: text.strip()})
66 return cls.objects.get(**{lookup_field_name: parts[1].strip()})
68 add_translatable(Competence, {
69 'name': models.CharField(_('name'), max_length=255, default='')
73 class Level(models.Model):
74 slug = models.CharField(_('slug'), max_length=255, unique=True)
75 meta_name = models.CharField(_('meta name'), max_length=255, unique=True)
76 order = models.IntegerField(_('order'))
77 package = models.FileField(
78 upload_to=lambda i, f: "curriculum/pack/edukacjamedialna_%s.zip" % i.slug,
79 null=True, blank=True, max_length=255, storage=bofh_storage)
80 student_package = models.FileField(
81 upload_to=lambda i, f: "curriculum/pack/edukacjamedialna_%s_uczen.zip" % i.slug,
82 null=True, blank=True, max_length=255, storage=bofh_storage)
86 verbose_name = _('educational level')
87 verbose_name_plural = _('educational levels')
89 def __unicode__(self):
92 def length_course(self):
93 return self.lesson_set.filter(type='course').count()
95 def length_synthetic(self):
96 return self.lesson_set.filter(type='synthetic').count()
98 def build_package(self, student):
99 from StringIO import StringIO
101 from django.core.files.base import ContentFile
102 from catalogue.templatetags.catalogue_tags import level_box
103 from catalogue.models import Lesson
106 zipf = zipfile.ZipFile(buff, 'w', zipfile.ZIP_STORED)
108 lessons = level_box(self)['lessons']
109 for i, lesson in enumerate(lessons['synthetic']):
110 prefix = 'Skrocony kurs/%d %s/' % (i, lesson.slug)
111 lesson.add_to_zip(zipf, student, prefix)
112 for c, (section, clessons) in enumerate(lessons['course'].items()):
113 assert section, clessons
114 for i, lesson in enumerate(clessons):
115 prefix = 'Pelny kurs/%d %s/%d %s/' % (c, section.slug, i, lesson.slug)
116 lesson.add_to_zip(zipf, student, prefix)
117 for i, lesson in enumerate(lessons['project']):
118 prefix = 'Projekty/%d %s/' % (i, lesson.slug)
119 lesson.add_to_zip(zipf, student, prefix)
120 # Add all appendix lessons, from all levels.
121 for lesson in Lesson.objects.filter(type='appendix'):
123 if self.slug in ('przedszkole', 'sp1-3', 'sp4-6'):
124 if lesson.slug == 'slowniczek':
127 if lesson.slug == 'slowniczek-sp':
129 prefix = '%s/' % lesson.slug
130 lesson.add_to_zip(zipf, student, prefix)
133 fieldname = "student_package" if student else "package"
134 getattr(self, fieldname).save(None, ContentFile(buff.getvalue()))
136 def build_packages(self):
137 self.build_package(False)
138 self.build_package(True)
141 add_translatable(Level, {
142 'name': models.CharField(_('name'), max_length=255, default=''),
143 'group': models.CharField(_('group'), max_length=255, default='')
147 class CompetenceLevel(models.Model):
148 competence = models.ForeignKey(Competence)
149 level = models.ForeignKey(Level)
152 ordering = ['competence', 'level']
153 verbose_name = _('competence on level')
154 verbose_name_plural = _('competences on levels')
156 def __unicode__(self):
157 return u"%s/%s" % (self.competence, self.level)
159 def get_absolute_url(self):
160 return "%s?c=%d&level=%s&d=1" % (reverse("curriculum"), self.competence.pk, self.level.slug)
162 add_translatable(CompetenceLevel, {
163 'description': models.TextField(_('description'), default='')
167 class CurriculumLevel(models.Model):
168 title = models.CharField(max_length=16, db_index=True)
169 verbose = models.CharField(max_length=32)
172 verbose_name = _("curriculum level")
173 verbose_name_plural = _("curriculum levels")
175 def __unicode__(self):
179 class CurriculumCourse(models.Model):
180 title = models.CharField(max_length=255)
181 accusative = models.CharField(max_length=255)
182 slug = models.CharField(max_length=255, db_index=True)
185 verbose_name = _("curriculum course")
186 verbose_name_plural = _("curriculum courses")
189 def __unicode__(self):
193 class Curriculum(models.Model):
194 """Official curriculum."""
195 TYPES = {'c': u'Cele kształcenia', 't': u'Treści nauczania', 'o': u'Osiągnięcia'}
197 identifier = models.CharField(max_length=255, db_index=True, unique=True)
198 title = models.CharField(max_length=1024)
199 course = models.ForeignKey(CurriculumCourse)
200 level = models.ForeignKey(CurriculumLevel)
201 type = models.CharField(max_length=16, choices=TYPES.items())
204 ordering = ['identifier']
205 verbose_name = _("curriculum item")
206 verbose_name_plural = _("curriculum items")
208 def __unicode__(self):
209 return self.identifier
212 def from_text(cls, identifier, title):
213 m = re.match(r"^\d+/(?P<level>[^/]+)/(?P<course>[^/]+)/"
214 r"(?P<type>(?:%s))[^/]+(?P<roz>/roz)?" % "|".join(cls.TYPES), identifier)
215 assert m is not None, "Curriculum identifier doesn't match template."
216 level, created = CurriculumLevel.objects.get_or_create(
217 title=m.group('level'))
219 print 'created level:', m.group('level')
220 def_title = m.group('course').capitalize()
221 course, created = CurriculumCourse.objects.get_or_create(
222 slug=slugify(m.group('course')),
225 'accusative': def_title,
228 print 'created course:', slugify(m.group('course')), def_title
229 type_ = m.group('type')
231 title += " (zakres rozszerzony)"
234 curr = cls.objects.get(identifier=identifier)
235 except cls.DoesNotExist:
236 curr = cls(identifier=identifier)