forked from apluslms/a-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
viewbase.py
152 lines (120 loc) · 4.85 KB
/
viewbase.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
from django.contrib import messages
from django.core.exceptions import PermissionDenied
from django.http import Http404
from django.shortcuts import get_object_or_404
from django.utils import translation
from django.utils.translation import ugettext_lazy as _
from django.utils.translation import get_language
from authorization.permissions import ACCESS
from exercise.cache.content import CachedContent
from lib.viewbase import BaseTemplateView
from userprofile.viewbase import UserProfileMixin
from .cache.students import CachedStudent
from .permissions import (
CourseVisiblePermission,
CourseModulePermission,
)
from .models import Course, CourseInstance, CourseModule, UserTagging
class CourseMixin(UserProfileMixin):
course_kw = "course_slug"
def get_resource_objects(self):
super().get_resource_objects()
self.course = get_object_or_404(
Course,
url=self._get_kwarg(self.course_kw)
)
self.is_teacher = self.course.is_teacher(self.request.user)
self.note("course", "is_teacher")
class CourseBaseView(CourseMixin, BaseTemplateView):
pass
class CourseInstanceBaseMixin(object):
course_kw = CourseMixin.course_kw
instance_kw = "instance_slug"
course_permission_classes = (
CourseVisiblePermission,
)
def get_permissions(self):
perms = super().get_permissions()
perms.extend((Perm() for Perm in self.course_permission_classes))
return perms
# get_course_instance_object
def get_resource_objects(self):
super().get_resource_objects()
user = self.request.user
instance = self.get_course_instance_object()
if instance is not None:
self.instance = instance
self.course = self.instance.course
self.content = CachedContent(self.instance)
self.is_student = self.instance.is_student(user)
self.is_assistant = self.instance.is_assistant(user)
self.is_teacher = self.course.is_teacher(user)
self.is_course_staff = self.is_teacher or self.is_assistant
self.get_taggings = lambda: CachedStudent(instance, user.id).data['tag_slugs']
self.note(
"course", "instance", "content", "is_student", "is_assistant",
"is_teacher", "is_course_staff", "get_taggings",
)
# Apply course instance language.
if self.instance.language:
lang = self.instance.language
if lang.startswith("|"):
active = get_language()
if "|" + active + "|" in lang:
translation.activate(active)
else:
fallback = lang[1:lang.find("|", 1)]
translation.activate(fallback)
else:
translation.activate(lang)
def get_access_mode(self):
access_mode = super().get_access_mode()
if hasattr(self, 'instance'):
# Loosen the access mode if instance is public
show_for = self.instance.view_content_to
is_public = show_for == CourseInstance.VIEW_ACCESS.PUBLIC
access_mode_student = access_mode in (ACCESS.STUDENT, ACCESS.ENROLL)
if is_public and access_mode_student:
access_mode = ACCESS.ANONYMOUS
return access_mode
class CourseInstanceMixin(CourseInstanceBaseMixin, UserProfileMixin):
def get_course_instance_object(self):
return get_object_or_404(
CourseInstance,
url=self.kwargs[self.instance_kw],
course__url=self.kwargs[self.course_kw],
)
class CourseInstanceBaseView(CourseInstanceMixin, BaseTemplateView):
pass
class EnrollableViewMixin(CourseInstanceMixin):
access_mode = ACCESS.ENROLL
def get_common_objects(self):
self.enrolled = self.is_student
self.enrollable = (
self.profile
and self.instance.is_enrollable(self.profile.user)
)
self.note('enrolled', 'enrollable')
class CourseModuleBaseMixin(object):
module_kw = "module_slug"
module_permissions_classes = (
CourseModulePermission,
)
def get_permissions(self):
perms = super().get_permissions()
perms.extend((Perm() for Perm in self.module_permissions_classes))
return perms
# get_course_module_object
def get_resource_objects(self):
super().get_resource_objects()
self.module = self.get_course_module_object()
self.note("module")
class CourseModuleMixin(CourseModuleBaseMixin, CourseInstanceMixin):
def get_course_module_object(self):
return get_object_or_404(
CourseModule,
url=self.kwargs[self.module_kw],
course_instance=self.instance
)
class CourseModuleBaseView(CourseModuleMixin, BaseTemplateView):
pass