models.py 4.64 KB
Newer Older
Jan's avatar
Jan committed
1
2
3
4
5
from django.conf import settings
from django.db import models
from django.urls import reverse
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _
6

Jan's avatar
Jan committed
7
from utils.snippets import datetime_to_lectureyear
8
from utils.translation import ModelTranslateMeta, MultilingualField
Jan's avatar
Jan committed
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
61


class Category(models.Model, metaclass=ModelTranslateMeta):
    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):
    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
    )

    shorthand = MultilingualField(
        models.CharField,
        max_length=10
    )

    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
62
        blank=True, null=True
Jan's avatar
Jan committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
    )

    period = models.CharField(
        max_length=64,
        verbose_name=_('period'),
    )

    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')


81
class Exam(models.Model, metaclass=ModelTranslateMeta):
Jan's avatar
Jan committed
82
83
84
85
86
    EXAM_TYPES = (
        ('document', _('Document')),
        ('exam', _('Exam')),
        ('partial', _('Partial Exam')),
        ('resit', _('Resit')),
87
88
        ('practice', _('Practice Exam')),
        ('answers', _('Exam Answers')))
Jan's avatar
Jan committed
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104

    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(
        settings.AUTH_USER_MODEL,
        verbose_name=_('uploader'),
Wietse Kuipers's avatar
Wietse Kuipers committed
105
        on_delete=models.CASCADE,
Jan's avatar
Jan committed
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
    )

    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(
        upload_to="education/files/exams/"
    )

    course = models.ForeignKey(
        Course,
        verbose_name=_("course"),
Wietse Kuipers's avatar
Wietse Kuipers committed
128
        on_delete=models.CASCADE,
Jan's avatar
Jan committed
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
    )

    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')


150
class Summary(models.Model, metaclass=ModelTranslateMeta):
Jan's avatar
Jan committed
151
152
153
154
155
156
157
158
    name = models.CharField(
        max_length=255,
        verbose_name=_('summary name'),
    )

    uploader = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        verbose_name=_('uploader'),
Wietse Kuipers's avatar
Wietse Kuipers committed
159
        on_delete=models.CASCADE,
Jan's avatar
Jan committed
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
    )

    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
176
        on_delete=models.CASCADE,
Jan's avatar
Jan committed
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
    )

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

    file = models.FileField(
        upload_to="education/files/summary/"
    )

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

    def get_absolute_url(self):
193
        return reverse('education:summary', args=[str(self.pk)])
Jan's avatar
Jan committed
194
195
196
197

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