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.utils.models.translation import add_translatable
8 class Section(models.Model):
9 slug = models.SlugField(_('slug'))
10 order = models.IntegerField(_('order'))
14 verbose_name = _('section')
15 verbose_name_plural = _('sections')
17 def __unicode__(self):
20 def get_absolute_url(self):
21 return "%s?s=%d" % (reverse("curriculum"), self.pk)
23 def url_for_level(self, level):
24 return "%s?s=%d&level=%s&d=1" % (reverse("curriculum"), self.pk, level.slug)
26 add_translatable(Section, {
27 'name': models.CharField(_('name'), max_length=255, default = '')
31 class Competence(models.Model):
32 section = models.ForeignKey(Section)
33 slug = models.SlugField(_('slug'))
34 order = models.IntegerField(_('order'))
37 ordering = ['section', 'order']
38 verbose_name = _('competence')
39 verbose_name_plural = _('competences')
41 def __unicode__(self):
44 def get_absolute_url(self):
45 return "%s?c=%d" % (reverse("curriculum"), self.pk)
47 def for_level(self, level):
48 return self.competencelevel_set.get(level=level)
50 def url_for_level(self, level):
51 return self.for_level(level).get_absolute_url()
54 def from_text(cls, text):
55 """Tries to return a Competence or a Section."""
56 parts = re.split(ur'[-\u2013]', text, 1)
57 lookup_field_name = 'name_%s__iexact' % get_language()
59 return Section.objects.get(**{lookup_field_name: text.strip()})
61 return cls.objects.get(**{lookup_field_name: parts[1].strip()})
63 add_translatable(Competence, {
64 'name': models.CharField(_('name'), max_length=255, default = '')
68 class Level(models.Model):
69 slug = models.CharField(_('slug'), max_length=255)
70 order = models.IntegerField(_('order'))
74 verbose_name = _('educational level')
75 verbose_name_plural = _('educational levels')
77 def __unicode__(self):
80 add_translatable(Level, {
81 'name': models.CharField(_('name'), max_length=255, default = ''),
82 'group': models.CharField(_('group'), max_length=255, default = '')
86 class CompetenceLevel(models.Model):
87 competence = models.ForeignKey(Competence)
88 level = models.ForeignKey(Level)
91 ordering = ['competence', 'level']
92 verbose_name = _('competence on level')
93 verbose_name_plural = _('competences on levels')
95 def __unicode__(self):
96 return u"%s/%s" % (self.competence, self.level)
98 def get_absolute_url(self):
99 return "%s?c=%d&level=%s&d=1" % (reverse("curriculum"), self.competence.pk, self.level.slug)
101 add_translatable(CompetenceLevel, {
102 'description': models.TextField(_('description'), default = '')
106 class CurriculumLevel(models.Model):
107 title = models.CharField(max_length=16, db_index=True)
110 verbose_name = _("curriculum level")
111 verbose_name_plural = _("curriculum levels")
113 def __unicode__(self):
117 class CurriculumCourse(models.Model):
118 title = models.CharField(max_length=255)
119 accusative = models.CharField(max_length=255)
120 slug = models.CharField(max_length=255, db_index=True)
123 verbose_name = _("curriculum course")
124 verbose_name_plural = _("curriculum courses")
127 def __unicode__(self):
131 class Curriculum(models.Model):
132 """Official curriculum."""
133 TYPES = {'c': u'Cele kształcenia', 't': u'Treści nauczania'}
135 identifier = models.CharField(max_length=255, db_index=True)
136 title = models.CharField(max_length=255)
137 course = models.ForeignKey(CurriculumCourse)
138 level = models.ForeignKey(CurriculumLevel)
139 type = models.CharField(max_length=16, choices=TYPES.items())
142 verbose_name = _("curriculum item")
143 verbose_name_plural = _("curriculum items")
145 def __unicode__(self):
146 return self.identifier
149 def from_text(cls, identifier, title):
150 m = re.match(r"^\d+/(?P<level>[^/]+)/(?P<course>[^/]+)/"
151 "(?P<type>(?:%s))[^/]+(?P<roz>/roz)?" %
152 "|".join(cls.TYPES), identifier)
153 assert m is not None, "Curriculum identifier doesn't match template."
154 level, created = CurriculumLevel.objects.get_or_create(
155 title=m.group('level'))
156 def_title = m.group('course').title()
157 course, created = CurriculumCourse.objects.get_or_create(
158 slug=m.group('course').lower(),
161 'accusative': def_title,
163 type_ = m.group('type')
165 title += " (zakres rozszerzony)"
168 curr = cls.objects.get(identifier=identifier)
169 except cls.DoesNotExist:
170 curr = cls(identifier=identifier)