-
Notifications
You must be signed in to change notification settings - Fork 73
/
models.py
352 lines (311 loc) · 11.2 KB
/
models.py
1
2
3
4
5
6
7
8
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
from django.core.exceptions import ValidationError
from django.urls import reverse
from django.db import models
from django.utils.functional import cached_property
from django.utils.translation import gettext_lazy as _
from urllib.parse import urljoin, urlsplit
from course.models import CourseInstance
from inheritance.models import ModelWithInheritance
from lib.fields import DefaultForeignKey
from lib.helpers import Enum
from lib.models import UrlMixin
class LinkService(ModelWithInheritance):
'''
A link to an external service.
'''
DESTINATION_REGION = Enum([
('INTERNAL', 0, _('DESTINATION_INTERNAL_PRIVACY_NOTICE')),
('ORGANIZATION', 1, _('DESTINATION_ORGANIZATION_PRIVACY_NOTICE')),
('EEA', 3, _('DESTINATION_EEA_PRIVACY_NOTICE')),
('PRIVACYSHIELD', 5, _('DESTINATION_PRIVACYSHIELD_PRIVACY_NOTICE')),
('GLOBAL', 6, _('DESTINATION_GLOBAL_PRIVACY_NOTICE')),
])
url = models.CharField(
verbose_name=_('LABEL_URL'),
max_length=256,
help_text=_('SERVICE_URL')
)
destination_region = models.PositiveSmallIntegerField(
verbose_name=_('LABEL_DESTINATION_REGION'),
choices=DESTINATION_REGION.choices,
default=DESTINATION_REGION.GLOBAL,
help_text=_('SERVICE_DESTINATION_REGION_HELPTEXT'),
)
privacy_notice_url = models.CharField(
verbose_name=_('LABEL_PRIVACY_NOTICE_URL'),
max_length=512,
blank=True,
help_text=_('SERVICE_PRIVACY_NOTICE_URL_HELPTEXT'))
menu_label = models.CharField(
verbose_name=_('LABEL_MENU_LABEL'),
max_length=255,
help_text=_('SERVICE_MENU_LABEL_HELPTEXT')
)
menu_icon_class = models.CharField(
verbose_name=_('LABEL_MENU_ICON_CLASS'),
max_length=32,
default="globe",
help_text=_('SERVICE_MENU_ICON_HELPTEXT')
)
enabled = models.BooleanField(
verbose_name=_('LABEL_ENABLED'),
default=True,
help_text=_('SERVICE_ENABLED_HELPTEXT')
)
class Meta:
verbose_name = _('MODEL_NAME_LINK_SERVICE')
verbose_name_plural = _('MODEL_NAME_LINK_SERVICE_PLURAL')
ordering = ["menu_label"]
def __str__(self):
out = "{}: {}".format(self.menu_label, self.url)
if not self.enabled:
return "[Disabled] " + out
return out
def clean(self):
errors = {}
if self.destination_region > self.DESTINATION_REGION.ORGANIZATION and not self.privacy_notice_url:
errors['privacy_notice_url'] = ValidationError(_('SERVICE_ERROR_PRIVACY_NOTICE_URL_MANDATORY'))
if errors:
raise ValidationError(errors)
@property
def url_parts(self):
return urlsplit(self.url)
@property
def method(self):
return 'GET'
@property
def sends_user_info(self):
return False
def get_url(self, replace=None, kwargs={}): # pylint: disable=dangerous-default-value
'''Return the URL to the launch page of this service.'''
if self.destination_region > self.DESTINATION_REGION.INTERNAL:
return reverse('external-service-link', kwargs=kwargs)
return self.get_final_url(replace)
def get_final_url(self, replace=None):
'''Return the launch URL for this service.
The optional replace parameter may be a relative URL that is joined to
the URL path of this service. The relative URL must not include a domain.
'''
url = self.url
if replace:
assert '://' not in replace and not replace.startswith('//'), "Replace can't include domain"
url = urljoin(url, replace)
return url
class LTIService(LinkService):
'''
A provider of an LTI service.
'''
LTI_ACCESS = Enum([
('ANON_API_NO', 0, _('LTI_SERVICE_ANONYMOUS_NO_API')),
('PUBLIC_API_NO', 5, _('LTI_SERVICE_PUBLIC_NO_API')),
('PUBLIC_API_YES', 10, _('LTI_SERVICE_PUBLIC_YES_API')),
])
access_settings = models.IntegerField(
verbose_name=_('LABEL_ACCESS_SETTINGS'),
choices=LTI_ACCESS.choices,
default=LTI_ACCESS.ANON_API_NO,
help_text=_('LTI_SERVICE_ACCESS_SETTINGS_HELPTEXT')
)
consumer_key = models.CharField(
verbose_name=_('LABEL_CONSUMER_KEY'),
max_length=128,
help_text=_('LTI_SERVICE_CONSUMER_KEY_HELPTEXT')
)
consumer_secret = models.CharField(
verbose_name=_('LABEL_CONSUMER_SECRET'),
max_length=128,
help_text=_('LTI_SERVICE_CONSUMER_SECRET_HELPTEXT')
)
class Meta:
verbose_name = _('MODEL_NAME_LTI_SERVICE')
verbose_name_plural = _('MODEL_NAME_LTI_SERVICE_PLURAL')
def __str__(self):
out = "(LTI) {}: {}".format(self.menu_label, self.url)
if not self.enabled:
return "[Disabled] " + out
return out
@property
def method(self):
return 'POST'
@property
def sends_user_info(self):
return True
@property
def is_anonymous(self):
return self.access_settings == self.LTI_ACCESS.ANON_API_NO
@property
def api_access(self):
return self.access_settings == self.LTI_ACCESS.PUBLIC_API_YES
def get_url(self, replace=None, kwargs={}): # pylint: disable=dangerous-default-value
return reverse('lti-login', kwargs=kwargs)
class LTI1p3Service(LinkService):
'''
Settings for a remote LTI v1.3 Tool.
'''
# Verbose names are intentionally non-translated, because they would not make sense in Finnish.
login_url = models.CharField(
verbose_name="Login initiation URL",
max_length=256,
)
client_id = models.CharField(
verbose_name="Client ID",
unique=True,
max_length=128,
)
deployment_id = models.CharField(
verbose_name="Deployment ID",
max_length=128,
)
jwks_url = models.CharField(
verbose_name="JWKS URL for tool's public key",
max_length=256,
)
# Note that setting share_name to False is not the same as LTI's "anonymous mode",
# because the A+ user ID will still be provided to the tool.
# Otherwise we would not be able to connect incoming scores with the correct user.
share_name = models.BooleanField(
verbose_name="Share launcher's name, email and student ID with the tool",
default=False,
)
class Meta:
verbose_name = _('MODEL_NAME_LTI1P3_SERVICE')
verbose_name_plural = _('MODEL_NAME_LTI1P3_SERVICE_PLURAL')
def get_url(self, replace=None, kwargs=None) -> str:
return reverse('lti-login', kwargs=kwargs)
@property
def method(self) -> str:
return 'POST'
class MenuItemManager(models.Manager):
def get_queryset(self):
return super().get_queryset().select_related(
'course_instance', 'course_instance__course')
class MenuItem(UrlMixin, models.Model):
'''
Attaches link to course menu.
'''
ACCESS = Enum([
('STUDENT', 0, _('MENU_ITEM_ACCESS_ALL')),
('ASSISTANT', 5, _('MENU_ITEM_ACCESS_ASSISTANTS_AND_TEACHERS')),
('TEACHER', 10, _('MENU_ITEM_ACCESS_TEACHERS')),
])
course_instance = models.ForeignKey(
CourseInstance,
verbose_name=_('LABEL_COURSE_INSTANCE'),
on_delete=models.CASCADE,
related_name="ext_services",
help_text=_('MENU_ITEM_COURSE_INSTANCE_HELPTEXT')
)
access = models.IntegerField(
verbose_name=_('LABEL_ACCESS'),
choices=ACCESS.choices,
default=ACCESS.STUDENT,
)
service = DefaultForeignKey(
LinkService,
verbose_name=_('LABEL_SERVICE'),
on_delete=models.CASCADE,
blank=True,
null=True,
help_text=_('MENU_ITEM_SERVICE_HELPTEXT')
)
menu_url = models.CharField(
verbose_name=_('LABEL_MENU_URL'),
max_length=256,
blank=True,
null=True,
help_text=_('MENU_ITEM_MENU_URL_HELPTEXT'),
)
menu_group_label = models.CharField(
verbose_name=_('LABEL_MENU_GROUP_LABEL'),
max_length=255,
blank=True,
null=True,
help_text=_('MENU_ITEM_MENU_GROUP_LABEL_HELPTEXT'),
)
menu_label = models.CharField(
verbose_name=_('LABEL_MENU_LABEL'),
max_length=255,
blank=True,
null=True,
help_text=_('MENU_ITEM_MENU_LINK_LABEL_HELPTEXT'),
)
menu_icon_class = models.CharField(
verbose_name=_('LABEL_MENU_ICON_CLASS'),
max_length=32,
null=True,
blank=True,
help_text=_('MENU_ITEM_MENU_ICON_CLASS_HELPTEXT'),
)
menu_weight = models.IntegerField(
verbose_name=_('LABEL_MENU_WEIGHT'),
default=0,
help_text=_('MENU_ITEM_MENU_WEIGHT_HELPTEXT'),
)
enabled = models.BooleanField(
verbose_name=_('LABEL_ENABLED'),
default=True,
)
class Meta:
verbose_name = _('MODEL_NAME_MENU_ITEM')
verbose_name_plural = _('MODEL_NAME_MENU_ITEM_PLURAL')
ordering = ["course_instance", "menu_weight", "menu_label"]
def __str__(self):
out = self.label
if not self.is_enabled:
return "[Disabled] " + out
return out
def clean(self):
errors = {}
if not self.service:
if not self.menu_url:
errors['menu_url'] = ValidationError(_(
'MENU_ITEM_ERROR_MENU_URL_REQUIRED_WHEN_NO_PRECONFIGURED_SERVICE_SELECTED'
))
if not self.menu_label:
errors['menu_label'] = ValidationError(_(
'MENU_ITEM_ERROR_MENU_LABEL_REQUIRED_WHEN_NO_PRECONFIGURED_SERVICE_SELECTED'
))
if self.service and self.menu_url and '://' in self.menu_url:
errors['menu_url'] = ValidationError(_(
'MENU_ITEM_ERROR_SCHEME_IN_MENU_URL_AND_PRECONFIGURED_SERVICE_SELECTED'
))
if errors:
raise ValidationError(errors)
@cached_property
def is_enabled(self):
if self.service:
return self.service.enabled and self.enabled
return self.enabled
@cached_property
def label(self):
if self.menu_label:
return self.menu_label
if self.service:
return self.service.menu_label
return ""
@cached_property
def icon_class(self):
if self.menu_icon_class:
return self.menu_icon_class
if self.service:
return self.service.menu_icon_class
return ""
@cached_property
def url(self):
if self.service:
kwargs = {
"course_slug": self.course_instance.course.url,
"instance_slug": self.course_instance.url,
"menu_id": self.id,
}
return self.service.as_leaf_class().get_url(replace=self.menu_url, kwargs=kwargs)
if '://' in self.menu_url:
return self.menu_url
return urljoin(self.course_instance.get_absolute_url(), self.menu_url)
@cached_property
def final_url(self):
if self.service:
return self.service.as_leaf_class().get_final_url(self.menu_url)
return urljoin(self.course_instance.get_absolute_url(), self.menu_url)
def get_url_kwargs(self):
return dict(menu_id=self.id, **self.course_instance.get_url_kwargs()) # pylint: disable=use-dict-literal