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)
171 verbose_name = _("curriculum level")
172 verbose_name_plural = _("curriculum levels")
174 def __unicode__(self):
178 class CurriculumCourse(models.Model):
179 title = models.CharField(max_length=255)
180 accusative = models.CharField(max_length=255)
181 slug = models.CharField(max_length=255, db_index=True)
184 verbose_name = _("curriculum course")
185 verbose_name_plural = _("curriculum courses")
188 def __unicode__(self):
192 class Curriculum(models.Model):
193 """Official curriculum."""
194 TYPES = {'c': u'Cele kształcenia', 't': u'Treści nauczania', 'o': u'Osiągnięcia'}
196 identifier = models.CharField(max_length=255, db_index=True, unique=True)
197 title = models.CharField(max_length=1024)
198 course = models.ForeignKey(CurriculumCourse)
199 level = models.ForeignKey(CurriculumLevel)
200 type = models.CharField(max_length=16, choices=TYPES.items())
203 ordering = ['identifier']
204 verbose_name = _("curriculum item")
205 verbose_name_plural = _("curriculum items")
207 def __unicode__(self):
208 return self.identifier
211 def from_text(cls, identifier, title):
212 m = re.match(r"^\d+/(?P<level>[^/]+)/(?P<course>[^/]+)/"
213 r"(?P<type>(?:%s))[^/]+(?P<roz>/roz)?" % "|".join(cls.TYPES), identifier)
214 assert m is not None, "Curriculum identifier doesn't match template."
215 level, created = CurriculumLevel.objects.get_or_create(
216 title=m.group('level'))
217 def_title = m.group('course').capitalize()
218 course, created = CurriculumCourse.objects.get_or_create(
219 slug=slugify(m.group('course')),
222 'accusative': def_title,
224 type_ = m.group('type')
226 title += " (zakres rozszerzony)"
229 curr = cls.objects.get(identifier=identifier)
230 except cls.DoesNotExist:
231 curr = cls(identifier=identifier)