Overdue fixes.
[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     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
27 class Competence(models.Model):
28     section = models.ForeignKey(Section)
29     name = models.CharField(_('name'), max_length=255)
30     slug = models.SlugField(_('slug'))
31     order = models.IntegerField(_('order'))
32
33     class Meta:
34         ordering = ['section', 'order']
35         verbose_name = _('competence')
36         verbose_name_plural = _('competences')
37
38     def __unicode__(self):
39         return self.name
40
41     def get_absolute_url(self):
42         return "%s?c=%d" % (reverse("curriculum"), self.pk)
43
44     def for_level(self, level):
45         return self.competencelevel_set.get(level=level)
46
47     def url_for_level(self, level):
48         return self.for_level(level).get_absolute_url()
49
50     @classmethod
51     def from_text(cls, text):
52         """Tries to return a Competence or a Section."""
53         parts = re.split(ur'[-\u2013]', text, 1)
54         if len(parts) == 1:
55             return Section.objects.get(name__iexact=text.strip())
56         else:
57             return cls.objects.get(name__iexact=parts[1].strip())
58
59 class Level(models.Model):
60     group = models.CharField(_('group'), max_length=255)
61     name = models.CharField(_('name'), max_length=255)
62     slug = models.CharField(_('slug'), max_length=255)
63     order = models.IntegerField(_('order'))
64
65     class Meta:
66         ordering = ['order']
67         verbose_name = _('educational level')
68         verbose_name_plural = _('educational levels')
69
70     def __unicode__(self):
71         return self.name
72
73 class CompetenceLevel(models.Model):
74     competence = models.ForeignKey(Competence)
75     level = models.ForeignKey(Level)
76     description = models.TextField(_('description'))
77
78     class Meta:
79         ordering = ['competence', 'level']
80         verbose_name = _('competence on level')
81         verbose_name_plural = _('competences on levels')
82
83     def __unicode__(self):
84         return u"%s/%s" % (self.competence, self.level)
85
86     def get_absolute_url(self):
87         return "%s?c=%d&level=%s&d=1" % (reverse("curriculum"), self.competence.pk, self.level.slug)
88
89
90
91 class CurriculumLevel(models.Model):
92     title = models.CharField(max_length=16, db_index=True)
93
94     class Meta:
95         verbose_name = _("curriculum level")
96         verbose_name_plural = _("curriculum levels")
97
98     def __unicode__(self):
99         return self.title
100
101
102 class CurriculumCourse(models.Model):
103     title = models.CharField(max_length=255)
104     accusative = models.CharField(max_length=255)
105     slug = models.CharField(max_length=255, db_index=True)
106
107     class Meta:
108         verbose_name = _("curriculum course")
109         verbose_name_plural = _("curriculum courses")
110         ordering = ['slug']
111
112     def __unicode__(self):
113         return self.title
114
115
116 class Curriculum(models.Model):
117     """Official curriculum."""
118     TYPES = {'c': u'Cele kształcenia', 't': u'Treści nauczania'}
119
120     identifier = models.CharField(max_length=255, db_index=True)
121     title = models.CharField(max_length=255)
122     course = models.ForeignKey(CurriculumCourse)
123     level = models.ForeignKey(CurriculumLevel)
124     type = models.CharField(max_length=16, choices=TYPES.items())
125
126     class Meta:
127         verbose_name = _("curriculum item")
128         verbose_name_plural = _("curriculum items")
129
130     def __unicode__(self):
131         return self.identifier
132
133     @classmethod
134     def from_text(cls, identifier, title):
135         m = re.match(r"^\d+/(?P<level>[^/]+)/(?P<course>[^/]+)/"
136                      "(?P<type>(?:%s))[^/]+(?P<roz>/roz)?" %
137                         "|".join(cls.TYPES), identifier)
138         assert m is not None, "Curriculum identifier doesn't match template."
139         level, created = CurriculumLevel.objects.get_or_create(
140                                        title=m.group('level'))
141         def_title = m.group('course').title()
142         course, created = CurriculumCourse.objects.get_or_create(
143                                         slug=m.group('course').lower(),
144                                         defaults={
145                                             'title': def_title,
146                                             'accusative': def_title,
147                                         })
148         type_ = m.group('type')
149         if m.group('roz'):
150             title += " (zakres rozszerzony)"
151
152         try:
153             curr = cls.objects.get(identifier=identifier)
154         except cls.DoesNotExist:
155             curr = cls(identifier=identifier)
156         curr.title = title
157         curr.course = course
158         curr.level = level
159         curr.type = type_
160         curr.save()
161         return curr
162