add lesson footer
[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     slug = models.CharField(max_length=255, db_index=True)
91
92     class Meta:
93         verbose_name = _("curriculum course")
94         verbose_name_plural = _("curriculum courses")
95
96     def __unicode__(self):
97         return self.title
98
99
100 class Curriculum(models.Model):
101     """Official curriculum."""
102     TYPES = {'c': u'Cele kształcenia', 't': u'Treści nauczania'}
103
104     identifier = models.CharField(max_length=255, db_index=True)
105     title = models.CharField(max_length=255)
106     course = models.ForeignKey(CurriculumCourse)
107     level = models.ForeignKey(CurriculumLevel)
108     type = models.CharField(max_length=16, choices=TYPES.items())
109
110     class Meta:
111         verbose_name = _("curriculum item")
112         verbose_name_plural = _("curriculum items")
113
114     def __unicode__(self):
115         return self.identifier
116
117     @classmethod
118     def from_text(cls, identifier, title):
119         m = re.match(r"^\d+/(?P<level>[^/]+)/(?P<course>[^/]+)/"
120                      "(?P<type>(?:%s))[^/]+(?P<roz>/roz)?" %
121                         "|".join(cls.TYPES), identifier)
122         assert m is not None, "Curriculum identifier doesn't match template."
123         level, created = CurriculumLevel.objects.get_or_create(
124                                        title=m.group('level'))
125         course, created = CurriculumCourse.objects.get_or_create(
126                                         slug=m.group('course'),
127                                         defaults={'title': m.group('course').title()})
128         type_ = m.group('type')
129         if m.group('roz'):
130             title += " (zakres rozszerzony)"
131
132         try:
133             curr = cls.objects.get(identifier=identifier)
134         except cls.DoesNotExist:
135             curr = cls(identifier=identifier)
136         curr.title = title
137         curr.course = course
138         curr.level = level
139         curr.type = type_
140         curr.save()
141         return curr
142