forked from apluslms/a-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
335 lines (283 loc) · 13.8 KB
/
views.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
from datetime import date
import logging
from typing import Any, Dict, List
from urllib.parse import unquote
from django.conf import settings
from django.contrib.auth.decorators import login_required
from django.contrib.auth.views import LoginView, LogoutView
from django.core.cache import cache
from django.core.cache.utils import make_template_fragment_key
from django.http import HttpResponse, HttpResponseRedirect, HttpRequest
from django.template.loader import TemplateDoesNotExist, get_template
from django.urls import reverse, translate_url
from django.utils.http import url_has_allowed_host_and_scheme
from django.utils.translation import (
check_for_language,
get_language
)
from django.utils.translation import gettext_lazy as _
from django.utils.translation import gettext
from django.views.decorators.http import require_POST
from authorization.permissions import ACCESS
from course.models import CourseInstance
from lib.helpers import settings_text, remove_query_param_from_url, is_ajax
from lib.viewbase import BaseView
from userprofile.models import UserProfile
from .viewbase import UserProfileView
logger = logging.getLogger('aplus.userprofile')
class AplusLoginView(LoginView):
"""This login view class extends the default Django login class and
overrides some of the default settings. Namely, the template and its context."""
template_name = "userprofile/login.html"
redirect_authenticated_user = True
# Redirecting authenticated users enables "social media fingerprinting"
# unless images are hosted on a different domain from the Django app.
extra_context = {
'shibboleth_login': 'shibboleth_login' in settings.INSTALLED_APPS,
'haka_login': getattr(settings, 'HAKA_LOGIN', False),
'mooc_login': 'social_django' in settings.INSTALLED_APPS,
}
if extra_context['haka_login'] and not extra_context['shibboleth_login']:
logger.warning("Shibboleth login not enabled, but Haka login flag set as true.")
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
# The following template context parameters can not be defined in
# the class variable extra_context because they require that Django
# translations are active. That is, there must be an HTTP request so
# that the language can be defined. There is no request in the code
# in the class level, but there is a request when this method is called
# (self.request).
brand_name = settings_text('BRAND_NAME')
context.update({
'brand_name': brand_name,
'shibboleth_title_text': settings_text('SHIBBOLETH_TITLE_TEXT'),
'shibboleth_body_text': settings_text('SHIBBOLETH_BODY_TEXT'),
'shibboleth_button_text': settings_text('SHIBBOLETH_BUTTON_TEXT'),
'haka_title_text': settings_text('HAKA_TITLE_TEXT'),
'haka_body_text': settings_text('HAKA_BODY_TEXT'),
'haka_button_text': settings_text('HAKA_BUTTON_TEXT'),
'mooc_title_text': settings_text('MOOC_TITLE_TEXT'),
'mooc_body_text': settings_text('MOOC_BODY_TEXT'),
'user_data_info': settings_text('LOGIN_USER_DATA_INFO').format(
brand_name=brand_name,
privacy_url=reverse('privacy_notice'),
),
'show_language_toggle': True,
})
return context
class AplusLogoutView(LogoutView):
template_name = "userprofile/logout.html"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["show_language_toggle"] = True
return context
def set_user_language(request):
"""Overrides set_language function from django.views.i18n."""
LANGUAGE_PARAMETER = 'language'
# pylint: disable-next=redefined-builtin
next = remove_query_param_from_url(request.POST.get('next', request.GET.get('next')), 'hl')
if ((next or not is_ajax(request)) and
not url_has_allowed_host_and_scheme(url=next,
allowed_hosts={request.get_host()},
require_https=request.is_secure())):
next = remove_query_param_from_url(request.META.get('HTTP_REFERER'), 'hl')
next = next and unquote(next) # HTTP_REFERER may be encoded.
if not url_has_allowed_host_and_scheme(url=next,
allowed_hosts={request.get_host()},
require_https=request.is_secure()):
next = '/'
response = HttpResponseRedirect(next) if next else HttpResponse(status=204)
if request.method == 'POST':
lang_code = request.POST.get(LANGUAGE_PARAMETER)
if lang_code and check_for_language(lang_code):
if next:
next_trans = translate_url(next, lang_code)
if next_trans != next:
response = HttpResponseRedirect(next_trans)
if request.user.is_authenticated:
userprofile = request.user.userprofile
userprofile.language = lang_code
userprofile.save()
else:
response.set_cookie(
settings.LANGUAGE_COOKIE_NAME, lang_code,
max_age=settings.LANGUAGE_COOKIE_AGE,
path=settings.LANGUAGE_COOKIE_PATH,
domain=settings.LANGUAGE_COOKIE_DOMAIN,
)
return response
@login_required
@require_POST
def regenerate_access_token(request):
"""Regenerates the API access token."""
request.user.userprofile.regenerate_api_token()
return HttpResponseRedirect(reverse('profile'))
def try_get_template(name):
try:
return get_template(name)
except TemplateDoesNotExist:
logger.info("Template %s not found", name)
return None
class PrivacyNoticeView(UserProfileView):
access_mode=ACCESS.ANONYMOUS
template_name="userprofile/privacy.html"
def get_common_objects(self):
super().get_common_objects()
lang = "_" + get_language().lower()
key = make_template_fragment_key('privacy_notice', [lang])
privacy_text = cache.get(key)
if not privacy_text:
template_name = "privacy_notice{}.html"
template = try_get_template(template_name.format(lang))
if not template and len(lang) > 3:
template = try_get_template(template_name.format(lang[:3]))
if not template:
logger.warning("No localized privacy notice for language %s", lang)
template = try_get_template(template_name.format(''))
if not template:
logger.error("No privacy notice at all!")
privacy_text = template.render() if template else _('NO_PRIVACY_NOTICE')
cache.set(key, privacy_text)
self.privacy_text = privacy_text
self.show_language_toggle = True
self.note("privacy_text", "show_language_toggle")
class AccessibilityStatementView(UserProfileView):
access_mode = ACCESS.ANONYMOUS
template_name="userprofile/accessibility.html"
def get_common_objects(self):
super().get_common_objects()
lang = "_" + get_language().lower()
key = make_template_fragment_key('accessibility_statement', [lang])
accessibility_statement = cache.get(key)
# TODO: refactor to a helper function
if not accessibility_statement:
local_template_name = "institution_accessibility_text{}.html"
local_template = try_get_template(local_template_name.format(lang))
if not local_template and len(lang) > 3:
local_template = try_get_template(local_template_name.format(lang[:3]))
if not local_template:
logger.warning("No localized accessibility statement for language %s", lang)
local_template = try_get_template(local_template_name.format(''))
if not local_template:
logger.error("No local accessibility content at all!")
local_accessibility_statement = local_template.render() if local_template else gettext(
'NO_LOCAL_ACCESSIBILITY_STATEMENT'
)
system_template_name = "accessibility_issues{}.html"
system_template = try_get_template(system_template_name.format(lang))
if not system_template and len(lang) > 3:
system_template = try_get_template(system_template_name.format(lang[:3]))
if not system_template:
logger.warning("No localized system accessibility content for language %s", lang)
system_template = try_get_template(system_template_name.format(''))
if not system_template:
logger.error("No system accessibility content at all!")
system_accessibility_statement = system_template.render() if system_template else gettext(
'NO_SYSTEM-WIDE_ACCESSIBILITY_STATEMENT'
)
accessibility_statement = local_accessibility_statement + system_accessibility_statement
cache.set(key, accessibility_statement)
self.accessibility_statement = accessibility_statement
self.show_language_toggle = True
self.note("accessibility_statement", "show_language_toggle")
class SupportView(UserProfileView):
access_mode = ACCESS.ANONYMOUS
template_name = "userprofile/support.html"
extra_context = {
'brand_name': settings_text('BRAND_NAME'),
'brand_name_long': settings_text('BRAND_NAME_LONG'),
'brand_institution_name': settings_text('BRAND_INSTITUTION_NAME'),
}
def get_common_objects(self):
super().get_common_objects()
lang = "_" + get_language().lower()
key = make_template_fragment_key('support_channels', [lang])
support_channels = cache.get(key)
if not support_channels:
template_name = "support_channels{}.html"
template = try_get_template(template_name.format(lang))
if not template and len(lang) > 3:
template = try_get_template(template_name.format(lang[:3]))
if not template:
template = try_get_template(template_name.format(''))
if not template:
logger.error("The support page is missing")
support_channels = template.render() if template else _('NO_SUPPORT_PAGE')
cache.set(key, support_channels)
self.support_channels = support_channels
self.show_language_toggle = True
self.note("support_channels", "show_language_toggle")
class ProfileView(UserProfileView):
template_name = "userprofile/profile.html"
extra_context = {
'brand_name': settings_text('BRAND_NAME'),
}
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['is_google'] = (
self.request.user.social_auth.filter(provider="google-oauth2").exists()
if settings.SOCIAL_AUTH
else False
)
return context
class TeacherListView(UserProfileView):
access_mode = ACCESS.SUPERUSER
template_name = "userprofile/teachers.html"
def entry(self, teacher: UserProfile, instance: CourseInstance) -> List[str]:
return [
teacher.user.first_name + " " + teacher.user.last_name,
teacher.user.email,
str(instance),
str(instance.starting_time),
str(instance.ending_time),
]
def entries(self, request: HttpRequest) -> List[List[str]]:
"""Returns a list of entries generated by entry_generator based on
instances from the date range in the request"""
start_date = request.GET.get('start_date', "")
try:
start_date = date.fromisoformat(start_date)
except ValueError:
start_date = date.today()
end_date = request.GET.get('end_date', "")
try:
end_date = date.fromisoformat(end_date)
except ValueError:
end_date = date.today()
# get all course instances with any overlap with the date range
instances = CourseInstance.objects.filter(
starting_time__date__lte = end_date,
ending_time__date__gte = start_date
)
entries: List[List[str]] = []
for instance in instances:
for teacher in instance.teachers.all():
entries.append(self.entry(teacher, instance))
if request.GET.get('with_assistants', False) == "true":
for assistant in instance.assistants.all():
entries.append(self.entry(assistant, instance))
return entries
def post(self, request: HttpRequest) -> HttpResponse:
"""Returns the teacher list as csv or the emails as a comma separated list"""
if request.POST.get('emails_only', None) is not None:
# export only the emails
entries = self.entries(request)
csv = ",".join([f'{e[1]}' for e in entries]) + "\n"
else:
# export the whole table
entries = self.entries(request)
csv = "teacher,email,instance,starting time,ending time\n"
for entry in entries:
csv += ",".join([f'"{e}"' for e in entry]) + "\n"
response = HttpResponse(csv, content_type="text/plain")
response['Content-Disposition'] = "attachment; filename=teachers.txt"
return response
def get_context_data(self, **kwargs: Any) -> Dict[str, Any]:
context = super().get_context_data(**kwargs)
context['entries'] = self.entries(context['request'])
return context
class PseudonymizeView(BaseView):
def get(self, request: HttpRequest) -> HttpResponse:
pseudonymize = request.session.get("pseudonymize", False)
request.session["pseudonymize"] = not pseudonymize
return HttpResponseRedirect(request.META.get("HTTP_REFERER", "/"))