models.py 5.6 KB
Newer Older
1
"""The models defined by the education package"""
Jan's avatar
Jan committed
2
3
4
5
from django.db import models
from django.urls import reverse
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _
6

7
from members.models import Member
8
from thaliawebsite.settings import settings
Jan's avatar
Jan committed
9
from utils.snippets import datetime_to_lectureyear
10
from utils.translation import ModelTranslateMeta, MultilingualField
Jan's avatar
Jan committed
11
12
13


class Category(models.Model, metaclass=ModelTranslateMeta):
14
    """Describes a course category"""
Jan's avatar
Jan committed
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
    name = MultilingualField(
        models.CharField,
        max_length=64,
    )

    def __str__(self):
        return self.name

    def get_absolute_url(self):
        return reverse('education:category', args=[str(self.pk)])

    class Meta:
        verbose_name = _('category')
        verbose_name_plural = _('categories')


class Course(models.Model, metaclass=ModelTranslateMeta):
32
    """Describes a course"""
Jan's avatar
Jan committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
    name = MultilingualField(
        models.CharField,
        max_length=255
    )

    categories = models.ManyToManyField(
        Category,
        verbose_name=_("categories"),
        blank=True
    )

    old_courses = models.ManyToManyField(
        'self',
        symmetrical=False,
        verbose_name=_("old courses"),
        blank=True
    )

    course_code = models.CharField(
        max_length=16
    )

    ec = models.IntegerField(
        verbose_name=_('EC')
    )

    since = models.IntegerField()
    until = models.IntegerField(
Luuk Scholten's avatar
Luuk Scholten committed
61
        blank=True, null=True
Jan's avatar
Jan committed
62
63
64
65
66
67
68
69
70
71
72
73
74
    )

    def __str__(self):
        return "{} ({})".format(self.name, self.course_code)

    def get_absolute_url(self):
        return reverse('education:course', args=[str(self.pk)])

    class Meta:
        verbose_name = _('course')
        verbose_name_plural = _('courses')


75
class Exam(models.Model, metaclass=ModelTranslateMeta):
76
77
    """Describes an exam"""

Jan's avatar
Jan committed
78
79
80
81
82
    EXAM_TYPES = (
        ('document', _('Document')),
        ('exam', _('Exam')),
        ('partial', _('Partial Exam')),
        ('resit', _('Resit')),
83
        ('practice', _('Practice Exam')),
84
85
86
87
        ('exam_answers', _('Exam Answers')),
        ('partial_answers', _('Partial Exam Answers')),
        ('resit_answers', _('Resit Answers')),
        ('practice_answers', _('Practice Exam Answers')))
Jan's avatar
Jan committed
88
89
90
91
92
93
94
95
96
97
98
99
100
101

    type = models.CharField(
        max_length=40,
        choices=EXAM_TYPES,
        verbose_name=_('exam type'),
    )

    name = models.CharField(
        max_length=255,
        verbose_name=_('exam name'),
        blank=True
    )

    uploader = models.ForeignKey(
102
        Member,
Jan's avatar
Jan committed
103
        verbose_name=_('uploader'),
104
105
        on_delete=models.SET_NULL,
        null=True,
Jan's avatar
Jan committed
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
    )

    uploader_date = models.DateField(
        default=timezone.now,
    )

    accepted = models.BooleanField(
        verbose_name=_('accepted'),
        default=False,
    )

    exam_date = models.DateField(
        verbose_name=_('exam date'),
    )

    file = models.FileField(
122
123
124
        upload_to="education/files/exams/",
        help_text=_('Use the \'View on site\' button to download '
                    'the file for inspection.')
Jan's avatar
Jan committed
125
126
127
128
129
    )

    course = models.ForeignKey(
        Course,
        verbose_name=_("course"),
Wietse Kuipers's avatar
Wietse Kuipers committed
130
        on_delete=models.CASCADE,
Jan's avatar
Jan committed
131
132
    )

133
134
135
136
137
138
139
    language = models.CharField(
        max_length=2,
        choices=settings.LANGUAGES,
        blank=False,
        null=True
    )

140
141
142
143
144
145
    download_count = models.IntegerField(
        verbose_name=_('amount of downloads'),
        default=0,
        blank=False,
    )

Jan's avatar
Jan committed
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
    def __str__(self):
        return "{} {} ({}, {}, {})".format(self.name.capitalize(),
                                           self.type.capitalize(),
                                           self.course.name,
                                           self.course.course_code,
                                           self.exam_date)

    def get_absolute_url(self):
        return reverse('education:exam', args=[str(self.pk)])

    @property
    def year(self):
        return datetime_to_lectureyear(self.exam_date)

    class Meta:
        verbose_name = _('exam')
        verbose_name_plural = _('exams')


165
class Summary(models.Model, metaclass=ModelTranslateMeta):
166
    """Describes a summary"""
Jan's avatar
Jan committed
167
168
169
170
171
172
    name = models.CharField(
        max_length=255,
        verbose_name=_('summary name'),
    )

    uploader = models.ForeignKey(
173
        Member,
Jan's avatar
Jan committed
174
        verbose_name=_('uploader'),
175
176
        on_delete=models.SET_NULL,
        null=True,
Jan's avatar
Jan committed
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
    )

    uploader_date = models.DateField(
        default=timezone.now,
    )

    year = models.IntegerField()

    author = models.CharField(
        max_length=64,
        verbose_name=_("author"),
    )

    course = models.ForeignKey(
        Course,
        verbose_name=_("course"),
Wietse Kuipers's avatar
Wietse Kuipers committed
193
        on_delete=models.CASCADE,
Jan's avatar
Jan committed
194
195
196
197
198
199
200
201
    )

    accepted = models.BooleanField(
        verbose_name=_('accepted'),
        default=False,
    )

    file = models.FileField(
202
203
204
        upload_to="education/files/summary/",
        help_text=_('Use the \'View on site\' button to download '
                    'the file for inspection.')
Jan's avatar
Jan committed
205
206
    )

207
208
209
210
    language = models.CharField(
        max_length=2,
        choices=settings.LANGUAGES,
        blank=False,
211
        null=True
212
213
    )

214
215
216
217
218
219
    download_count = models.IntegerField(
        verbose_name=_('amount of downloads'),
        default=0,
        blank=False,
    )

Jan's avatar
Jan committed
220
221
222
223
224
    def __str__(self):
        return "{} ({}, {}, {})".format(self.name, self.course.name,
                                        self.course.course_code, self.year)

    def get_absolute_url(self):
225
        return reverse('education:summary', args=[str(self.pk)])
Jan's avatar
Jan committed
226
227
228
229

    class Meta:
        verbose_name = _('summary')
        verbose_name_plural = _('summaries')