fix
[edumed.git] / curriculum / models.py
1 # -*- coding: utf-8
2 import re
3 from django.core.urlresolvers import reverse
4 from django.db import models
5 from django.utils.translation import ugettext_lazy as _
6
7 class Section(models.Model):
8     name = models.CharField(_('name'), max_length=255)
9     slug = models.SlugField(_('slug'))
10     order = models.IntegerField(_('order'))
11
12     class Meta:
13         ordering = ['order']
14         verbose_name = _('section')
15         verbose_name_plural = _('sections')
16
17     def __unicode__(self):
18         return self.name
19
20 class Competence(models.Model):
21     section = models.ForeignKey(Section)
22     name = models.CharField(_('name'), max_length=255)
23     slug = models.SlugField(_('slug'))
24     order = models.IntegerField(_('order'))
25
26     class Meta:
27         ordering = ['order']
28         verbose_name = _('competence')
29         verbose_name_plural = _('competences')
30
31     def __unicode__(self):
32         return self.name
33
34     def get_absolute_url(self):
35         return "%s?c=%d" % (reverse("curriculum"), self.pk)
36
37     def for_level(self, level):
38         return self.competencelevel_set.get(level=level)
39
40     @classmethod
41     def from_text(cls, text):
42         name = text.rsplit(u'\u2013', 1)[-1].strip()
43         return cls.objects.get(name__iexact=name)
44
45 class Level(models.Model):
46     group = models.CharField(_('group'), max_length=255)
47     name = models.CharField(_('name'), max_length=255)
48     slug = models.CharField(_('slug'), max_length=255)
49     order = models.IntegerField(_('order'))
50
51     class Meta:
52         ordering = ['order']
53         verbose_name = _('educational level')
54         verbose_name_plural = _('educational levels')
55
56     def __unicode__(self):
57         return self.name
58
59 class CompetenceLevel(models.Model):
60     competence = models.ForeignKey(Competence)
61     level = models.ForeignKey(Level)
62     description = models.TextField(_('description'))
63
64     class Meta:
65         ordering = ['competence', 'level']
66         verbose_name = _('competence on level')
67         verbose_name_plural = _('competences on levels')
68
69     def __unicode__(self):
70         return u"%s/%s" % (self.competence, self.level)
71
72     def get_absolute_url(self):
73         return "%s?c=%d&level=%s&d=1" % (reverse("curriculum"), self.competence.pk, self.level.slug)
74
75
76
77 class CurriculumLevel(models.Model):
78     title = models.CharField(max_length=16, db_index=True)
79
80     class Meta:
81         verbose_name = _("curriculum level")
82         verbose_name_plural = _("curriculum levels")
83
84     def __unicode__(self):
85         return self.title
86
87
88 class CurriculumCourse(models.Model):
89     title = models.CharField(max_length=255)
90     accusative = models.CharField(max_length=255)
91     slug = models.CharField(max_length=255, db_index=True)
92
93     class Meta:
94         verbose_name = _("curriculum course")
95         verbose_name_plural = _("curriculum courses")
96         ordering = ['slug']
97
98     def __unicode__(self):
99         return self.title
100
101
102 class Curriculum(models.Model):
103     """Official curriculum."""
104     TYPES = {'c': u'Cele kształcenia', 't': u'Treści nauczania'}
105
106     identifier = models.CharField(max_length=255, db_index=True)
107     title = models.CharField(max_length=255)
108     course = models.ForeignKey(CurriculumCourse)
109     level = models.ForeignKey(CurriculumLevel)
110     type = models.CharField(max_length=16, choices=TYPES.items())
111
112     class Meta:
113         verbose_name = _("curriculum item")
114         verbose_name_plural = _("curriculum items")
115
116     def __unicode__(self):
117         return self.identifier
118
119     @classmethod
120     def from_text(cls, identifier, title):
121         m = re.match(r"^\d+/(?P<level>[^/]+)/(?P<course>[^/]+)/"
122                      "(?P<type>(?:%s))[^/]+(?P<roz>/roz)?" %
123                         "|".join(cls.TYPES), identifier)
124         assert m is not None, "Curriculum identifier doesn't match template."
125         level, created = CurriculumLevel.objects.get_or_create(
126                                        title=m.group('level'))
127         def_title = m.group('course').title()
128         course, created = CurriculumCourse.objects.get_or_create(
129                                         slug=m.group('course').lower(),
130                                         defaults={
131                                             'title': def_title,
132                                             'accusative': def_title,
133                                         })
134         type_ = m.group('type')
135         if m.group('roz'):
136             title += " (zakres rozszerzony)"
137
138         try:
139             curr = cls.objects.get(identifier=identifier)
140         except cls.DoesNotExist:
141             curr = cls(identifier=identifier)
142         curr.title = title
143         curr.course = course
144         curr.level = level
145         curr.type = type_
146         curr.save()
147         return curr
148