forked from apluslms/a-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
191 lines (152 loc) · 6.78 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
import datetime
import logging
import icalendar
from django.conf import settings
from django.contrib import messages
from django.core.exceptions import PermissionDenied
from django.http import Http404
from django.http.response import HttpResponse
from django.shortcuts import get_object_or_404
from django.utils import html
from django.utils import timezone
from django.utils.http import is_safe_url
from django.utils.translation import ugettext_lazy as _
from authorization.permissions import ACCESS
from exercise.cache.hierarchy import NoSuchContent
from exercise.models import LearningObject
from lib.helpers import settings_text
from lib.viewbase import BaseTemplateView, BaseRedirectView, BaseFormView, BaseView
from userprofile.viewbase import UserProfileView
from .forms import GroupsForm, GroupSelectForm
from .models import CourseInstance, Enrollment
from .renders import group_info_context
from .viewbase import CourseModuleBaseView, CourseInstanceMixin, EnrollableViewMixin
logger = logging.getLogger("course.views")
class HomeView(UserProfileView):
access_mode = ACCESS.ANONYMOUS
template_name = "course/index.html"
def get_common_objects(self):
super().get_common_objects()
self.welcome_text = settings_text('WELCOME_TEXT')
self.internal_user_label = settings_text('INTERNAL_USER_LABEL')
self.external_user_label = settings_text('EXTERNAL_USER_LABEL')
self.instances = []
prio2 = []
treshold = timezone.now() - datetime.timedelta(days=10)
for instance in CourseInstance.objects.get_visible(self.request.user)\
.filter(ending_time__gte=timezone.now()):
if instance.starting_time > treshold:
self.instances += [instance]
else:
prio2 += [instance]
self.instances += prio2
self.note("welcome_text", "internal_user_label", "external_user_label", "instances")
class ArchiveView(UserProfileView):
access_mode = ACCESS.ANONYMOUS
template_name = "course/archive.html"
def get_common_objects(self):
super().get_common_objects()
self.instances = CourseInstance.objects.get_visible(self.request.user)
self.note("instances")
class InstanceView(EnrollableViewMixin, BaseTemplateView):
template_name = "course/course.html"
def get(self, request, *args, **kwargs):
# external LTI Tool Providers may return the user to the course instance view
# with a message given in GET query parameters
lti_error_msg = request.GET.get('lti_errormsg')
lti_msg = request.GET.get('lti_msg')
# message HTML is not escaped in the templates so escape it here
if lti_error_msg:
messages.error(request, html.escape(lti_error_msg))
elif lti_msg:
messages.info(request, html.escape(lti_msg))
return super().get(request, *args, **kwargs)
class Enroll(EnrollableViewMixin, BaseRedirectView):
def post(self, request, *args, **kwargs):
if self.is_student or not self.enrollable:
messages.error(self.request, _("You cannot enroll, or have already enrolled, in this course."))
raise PermissionDenied()
if not self.instance.is_enrollment_open():
messages.error(self.request, _("The enrollment is not open."))
raise PermissionDenied()
# Support enrollment questionnaires.
from exercise.models import LearningObject
exercise = LearningObject.objects.find_enrollment_exercise(
self.instance, self.profile)
if exercise:
return self.redirect(exercise.get_absolute_url())
self.instance.enroll_student(self.request.user)
return self.redirect(self.instance.get_absolute_url())
class ModuleView(CourseModuleBaseView):
template_name = "course/module.html"
def get_common_objects(self):
super().get_common_objects()
self.now = timezone.now()
try:
self.children = self.content.flat_module(self.module)
cur, tree, prev, nex = self.content.find(self.module)
self.previous = prev
self.current = cur
self.next = nex
except NoSuchContent:
raise Http404
self.note('now', 'children', 'previous', 'current', 'next')
class CalendarExport(CourseInstanceMixin, BaseView):
def get(self, request, *args, **kwargs):
cal = icalendar.Calendar()
cal.add('prodid', '-// {} calendar //'.format(settings.BRAND_NAME))
cal.add('version', '2.0')
for module in self.instance.course_modules.all():
event = icalendar.Event()
event.add('summary', module.name)
event.add('dtstart',
module.closing_time - datetime.timedelta(hours=1))
event.add('dtend', module.closing_time)
event.add('dtstamp', module.closing_time)
event['uid'] = "module/" + str(module.id) + "/A+"
cal.add_component(event)
return HttpResponse(cal.to_ical(),
content_type="text/calendar; charset=utf-8")
class GroupsView(CourseInstanceMixin, BaseFormView):
access_mode = ACCESS.ENROLLED
template_name = "course/groups.html"
form_class = GroupsForm
def get_common_objects(self):
super().get_common_objects()
self.enrollment = self.instance.get_enrollment_for(self.request.user)
self.groups = list(self.profile.groups.filter(course_instance=self.instance))
self.note('enrollment','groups')
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs["profile"] = self.profile
kwargs["instance"] = self.instance
kwargs["content"] = self.content
return kwargs
def get_success_url(self):
return self.instance.get_url('groups')
def form_valid(self, form):
form.save()
messages.success(self.request, _("A new student group was created."))
return super().form_valid(form)
class GroupSelect(CourseInstanceMixin, BaseFormView):
access_mode = ACCESS.ENROLLED
form_class = GroupSelectForm
template_name = "course/_group_info.html"
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs["profile"] = self.profile
kwargs["instance"] = self.instance
return kwargs
def get_success_url(self):
return self.instance.get_absolute_url()
def get(self, request, *args, **kwargs):
return self.http_method_not_allowed(request, *args, **kwargs)
def form_invalid(self, form):
return HttpResponse('Invalid group selection')
def form_valid(self, form):
enrollment = form.save()
if self.request.is_ajax():
return self.response(
**group_info_context(enrollment.selected_group, self.profile)
)
return super().form_valid(form)