apropriate glossary in level package
[edumed.git] / curriculum / models.py
index a3e2280..4153f1b 100644 (file)
@@ -2,9 +2,13 @@
 import re
 from django.core.urlresolvers import reverse
 from django.db import models
-from django.utils.translation import ugettext_lazy as _
+from django.utils.translation import ugettext_lazy as _, get_language
+from fnpdjango.storage import BofhFileSystemStorage
 from fnpdjango.utils.models.translation import add_translatable
 
+bofh_storage = BofhFileSystemStorage()
+
+
 class Section(models.Model):
     slug = models.SlugField(_('slug'))
     order = models.IntegerField(_('order'))
@@ -22,9 +26,9 @@ class Section(models.Model):
 
     def url_for_level(self, level):
         return "%s?s=%d&level=%s&d=1" % (reverse("curriculum"), self.pk, level.slug)
-        
+
 add_translatable(Section, {
-    'name': models.CharField(_('name'), max_length=255, default = '')
+    'name': models.CharField(_('name'), max_length=255, default='')
 })
 
 
@@ -54,19 +58,27 @@ class Competence(models.Model):
     def from_text(cls, text):
         """Tries to return a Competence or a Section."""
         parts = re.split(ur'[-\u2013]', text, 1)
+        lookup_field_name = 'name_%s__iexact' % get_language()
         if len(parts) == 1:
-            return Section.objects.get(name__iexact=text.strip())
+            return Section.objects.get(**{lookup_field_name: text.strip()})
         else:
-            return cls.objects.get(name__iexact=parts[1].strip())
+            return cls.objects.get(**{lookup_field_name: parts[1].strip()})
 
 add_translatable(Competence, {
-    'name': models.CharField(_('name'), max_length=255, default = '')
+    'name': models.CharField(_('name'), max_length=255, default='')
 })
 
 
 class Level(models.Model):
-    slug = models.CharField(_('slug'), max_length=255)
+    slug = models.CharField(_('slug'), max_length=255, unique=True)
+    meta_name = models.CharField(_('meta name'), max_length=255, unique=True)
     order = models.IntegerField(_('order'))
+    package = models.FileField(
+        upload_to=lambda i, f: "curriculum/pack/edukacjamedialna_%s.zip" % i.slug,
+        null=True, blank=True, max_length=255, storage=bofh_storage)
+    student_package = models.FileField(
+        upload_to=lambda i, f: "curriculum/pack/edukacjamedialna_%s_uczen.zip" % i.slug,
+        null=True, blank=True, max_length=255, storage=bofh_storage)
 
     class Meta:
         ordering = ['order']
@@ -76,9 +88,58 @@ class Level(models.Model):
     def __unicode__(self):
         return self.name
 
+    def length_course(self):
+        return self.lesson_set.filter(type='course').count()
+
+    def length_synthetic(self):
+        return self.lesson_set.filter(type='synthetic').count()
+
+    def build_package(self, student):
+        from StringIO import StringIO
+        import zipfile
+        from django.core.files.base import ContentFile
+        from catalogue.templatetags.catalogue_tags import level_box
+        from catalogue.models import Lesson
+
+        buff = StringIO()
+        zipf = zipfile.ZipFile(buff, 'w', zipfile.ZIP_STORED)
+
+        lessons = level_box(self)['lessons']
+        for i, lesson in enumerate(lessons['synthetic']):
+            prefix = 'Skrocony kurs/%d %s/' % (i, lesson.slug)
+            lesson.add_to_zip(zipf, student, prefix)
+        for c, (section, clessons) in enumerate(lessons['course'].items()):
+            assert section, clessons
+            for i, lesson in enumerate(clessons):
+                prefix = 'Pelny kurs/%d %s/%d %s/' % (c, section.slug, i, lesson.slug)
+                lesson.add_to_zip(zipf, student, prefix)
+        for i, lesson in enumerate(lessons['project']):
+            prefix = 'Projekty/%d %s/' % (i, lesson.slug)
+            lesson.add_to_zip(zipf, student, prefix)
+        # Add all appendix lessons, from all levels.
+        for lesson in Lesson.objects.filter(type='appendix'):
+            # ugly fix
+            if self.slug in ('przedszkole', 'sp1-3', 'sp4-6'):
+                if lesson.slug == 'slowniczek':
+                    continue
+            else:
+                if lesson.slug == 'slowniczek-sp':
+                    continue
+            prefix = '%s/' % lesson.slug
+            lesson.add_to_zip(zipf, student, prefix)
+        zipf.close()
+
+        fieldname = "student_package" if student else "package"
+        getattr(self, fieldname).save(None, ContentFile(buff.getvalue()))
+
+    def build_packages(self):
+        self.build_package(False)
+        self.build_package(True)
+
+
 add_translatable(Level, {
-    'name': models.CharField(_('name'), max_length=255, default = ''),
-    'group': models.CharField(_('group'), max_length=255, default = '')
+    'name': models.CharField(_('name'), max_length=255, default=''),
+    'group': models.CharField(_('group'), max_length=255, default='')
 })
 
 
@@ -98,7 +159,7 @@ class CompetenceLevel(models.Model):
         return "%s?c=%d&level=%s&d=1" % (reverse("curriculum"), self.competence.pk, self.level.slug)
 
 add_translatable(CompetenceLevel, {
-    'description': models.TextField(_('description'), default = '')
+    'description': models.TextField(_('description'), default='')
 })
 
 
@@ -147,8 +208,7 @@ class Curriculum(models.Model):
     @classmethod
     def from_text(cls, identifier, title):
         m = re.match(r"^\d+/(?P<level>[^/]+)/(?P<course>[^/]+)/"
-                     "(?P<type>(?:%s))[^/]+(?P<roz>/roz)?" %
-                        "|".join(cls.TYPES), identifier)
+                     r"(?P<type>(?:%s))[^/]+(?P<roz>/roz)?" % "|".join(cls.TYPES), identifier)
         assert m is not None, "Curriculum identifier doesn't match template."
         level, created = CurriculumLevel.objects.get_or_create(
                                        title=m.group('level'))
@@ -173,4 +233,3 @@ class Curriculum(models.Model):
         curr.type = type_
         curr.save()
         return curr
-