Additional information about contest deadline
[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 _, get_language
6 from fnpdjango.utils.models.translation import add_translatable
7
8 class Section(models.Model):
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     def get_absolute_url(self):
21         return "%s?s=%d" % (reverse("curriculum"), self.pk)
22
23     def url_for_level(self, level):
24         return "%s?s=%d&level=%s&d=1" % (reverse("curriculum"), self.pk, level.slug)
25         
26 add_translatable(Section, {
27     'name': models.CharField(_('name'), max_length=255, default = '')
28 })
29
30
31 class Competence(models.Model):
32     section = models.ForeignKey(Section)
33     slug = models.SlugField(_('slug'))
34     order = models.IntegerField(_('order'))
35
36     class Meta:
37         ordering = ['section', 'order']
38         verbose_name = _('competence')
39         verbose_name_plural = _('competences')
40
41     def __unicode__(self):
42         return self.name
43
44     def get_absolute_url(self):
45         return "%s?c=%d" % (reverse("curriculum"), self.pk)
46
47     def for_level(self, level):
48         return self.competencelevel_set.get(level=level)
49
50     def url_for_level(self, level):
51         return self.for_level(level).get_absolute_url()
52
53     @classmethod
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()
58         if len(parts) == 1:
59             return Section.objects.get(**{lookup_field_name: text.strip()})
60         else:
61             return cls.objects.get(**{lookup_field_name: parts[1].strip()})
62
63 add_translatable(Competence, {
64     'name': models.CharField(_('name'), max_length=255, default = '')
65 })
66
67
68 class Level(models.Model):
69     slug = models.CharField(_('slug'), max_length=255)
70     order = models.IntegerField(_('order'))
71
72     class Meta:
73         ordering = ['order']
74         verbose_name = _('educational level')
75         verbose_name_plural = _('educational levels')
76
77     def __unicode__(self):
78         return self.name
79
80 add_translatable(Level, {
81     'name': models.CharField(_('name'), max_length=255, default = ''),
82     'group': models.CharField(_('group'), max_length=255, default = '')
83 })
84
85
86 class CompetenceLevel(models.Model):
87     competence = models.ForeignKey(Competence)
88     level = models.ForeignKey(Level)
89
90     class Meta:
91         ordering = ['competence', 'level']
92         verbose_name = _('competence on level')
93         verbose_name_plural = _('competences on levels')
94
95     def __unicode__(self):
96         return u"%s/%s" % (self.competence, self.level)
97
98     def get_absolute_url(self):
99         return "%s?c=%d&level=%s&d=1" % (reverse("curriculum"), self.competence.pk, self.level.slug)
100
101 add_translatable(CompetenceLevel, {
102     'description': models.TextField(_('description'), default = '')
103 })
104
105
106 class CurriculumLevel(models.Model):
107     title = models.CharField(max_length=16, db_index=True)
108
109     class Meta:
110         verbose_name = _("curriculum level")
111         verbose_name_plural = _("curriculum levels")
112
113     def __unicode__(self):
114         return self.title
115
116
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)
121
122     class Meta:
123         verbose_name = _("curriculum course")
124         verbose_name_plural = _("curriculum courses")
125         ordering = ['slug']
126
127     def __unicode__(self):
128         return self.title
129
130
131 class Curriculum(models.Model):
132     """Official curriculum."""
133     TYPES = {'c': u'Cele kształcenia', 't': u'Treści nauczania'}
134
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())
140
141     class Meta:
142         verbose_name = _("curriculum item")
143         verbose_name_plural = _("curriculum items")
144
145     def __unicode__(self):
146         return self.identifier
147
148     @classmethod
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(),
159                                         defaults={
160                                             'title': def_title,
161                                             'accusative': def_title,
162                                         })
163         type_ = m.group('type')
164         if m.group('roz'):
165             title += " (zakres rozszerzony)"
166
167         try:
168             curr = cls.objects.get(identifier=identifier)
169         except cls.DoesNotExist:
170             curr = cls(identifier=identifier)
171         curr.title = title
172         curr.course = course
173         curr.level = level
174         curr.type = type_
175         curr.save()
176         return curr
177