Fixing contact form to work with katalog subdomain
[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 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         if len(parts) == 1:
58             return Section.objects.get(name__iexact=text.strip())
59         else:
60             return cls.objects.get(name__iexact=parts[1].strip())
61
62 add_translatable(Competence, {
63     'name': models.CharField(_('name'), max_length=255, default = '')
64 })
65
66
67 class Level(models.Model):
68     slug = models.CharField(_('slug'), max_length=255)
69     order = models.IntegerField(_('order'))
70
71     class Meta:
72         ordering = ['order']
73         verbose_name = _('educational level')
74         verbose_name_plural = _('educational levels')
75
76     def __unicode__(self):
77         return self.name
78
79 add_translatable(Level, {
80     'name': models.CharField(_('name'), max_length=255, default = ''),
81     'group': models.CharField(_('group'), max_length=255, default = '')
82 })
83
84
85 class CompetenceLevel(models.Model):
86     competence = models.ForeignKey(Competence)
87     level = models.ForeignKey(Level)
88
89     class Meta:
90         ordering = ['competence', 'level']
91         verbose_name = _('competence on level')
92         verbose_name_plural = _('competences on levels')
93
94     def __unicode__(self):
95         return u"%s/%s" % (self.competence, self.level)
96
97     def get_absolute_url(self):
98         return "%s?c=%d&level=%s&d=1" % (reverse("curriculum"), self.competence.pk, self.level.slug)
99
100 add_translatable(CompetenceLevel, {
101     'description': models.TextField(_('description'), default = '')
102 })
103
104
105 class CurriculumLevel(models.Model):
106     title = models.CharField(max_length=16, db_index=True)
107
108     class Meta:
109         verbose_name = _("curriculum level")
110         verbose_name_plural = _("curriculum levels")
111
112     def __unicode__(self):
113         return self.title
114
115
116 class CurriculumCourse(models.Model):
117     title = models.CharField(max_length=255)
118     accusative = models.CharField(max_length=255)
119     slug = models.CharField(max_length=255, db_index=True)
120
121     class Meta:
122         verbose_name = _("curriculum course")
123         verbose_name_plural = _("curriculum courses")
124         ordering = ['slug']
125
126     def __unicode__(self):
127         return self.title
128
129
130 class Curriculum(models.Model):
131     """Official curriculum."""
132     TYPES = {'c': u'Cele kształcenia', 't': u'Treści nauczania'}
133
134     identifier = models.CharField(max_length=255, db_index=True)
135     title = models.CharField(max_length=255)
136     course = models.ForeignKey(CurriculumCourse)
137     level = models.ForeignKey(CurriculumLevel)
138     type = models.CharField(max_length=16, choices=TYPES.items())
139
140     class Meta:
141         verbose_name = _("curriculum item")
142         verbose_name_plural = _("curriculum items")
143
144     def __unicode__(self):
145         return self.identifier
146
147     @classmethod
148     def from_text(cls, identifier, title):
149         m = re.match(r"^\d+/(?P<level>[^/]+)/(?P<course>[^/]+)/"
150                      "(?P<type>(?:%s))[^/]+(?P<roz>/roz)?" %
151                         "|".join(cls.TYPES), identifier)
152         assert m is not None, "Curriculum identifier doesn't match template."
153         level, created = CurriculumLevel.objects.get_or_create(
154                                        title=m.group('level'))
155         def_title = m.group('course').title()
156         course, created = CurriculumCourse.objects.get_or_create(
157                                         slug=m.group('course').lower(),
158                                         defaults={
159                                             'title': def_title,
160                                             'accusative': def_title,
161                                         })
162         type_ = m.group('type')
163         if m.group('roz'):
164             title += " (zakres rozszerzony)"
165
166         try:
167             curr = cls.objects.get(identifier=identifier)
168         except cls.DoesNotExist:
169             curr = cls(identifier=identifier)
170         curr.title = title
171         curr.course = course
172         curr.level = level
173         curr.type = type_
174         curr.save()
175         return curr
176