forked from apluslms/a-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
staff_views.py
92 lines (74 loc) · 3.07 KB
/
staff_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
import json
from django.contrib import messages
from django.shortcuts import get_object_or_404
from django.utils.translation import ugettext_lazy as _
from authorization.permissions import ACCESS
from lib.helpers import settings_text
from lib.viewbase import BaseFormView, BaseTemplateView, BaseRedirectMixin
from .cache.students import CachedStudents
from .forms import GroupEditForm
from .models import StudentGroup
from .viewbase import CourseInstanceBaseView, CourseInstanceMixin
class ParticipantsView(CourseInstanceBaseView):
access_mode = ACCESS.ASSISTANT
template_name = "course/staff/participants.html"
def get_common_objects(self):
super().get_common_objects()
self.participants = json.dumps(CachedStudents(self.instance).students())
self.tags = list(self.instance.usertags.all())
self.course_id = self.instance.id
self.internal_user_label = settings_text('INTERNAL_USER_LABEL')
self.external_user_label = settings_text('EXTERNAL_USER_LABEL')
self.note(
'participants', 'tags', 'course_id',
'internal_user_label', 'external_user_label',
)
class GroupsView(CourseInstanceBaseView):
access_mode = ACCESS.ASSISTANT
template_name = "course/staff/groups.html"
def get_common_objects(self):
super().get_common_objects()
self.groups = list(self.instance.groups.all())
self.note('groups')
class GroupsEditView(CourseInstanceMixin, BaseFormView):
access_mode = ACCESS.ASSISTANT
form_class = GroupEditForm
group_kw = "group_id"
template_name = "course/staff/group_edit.html"
def get_resource_objects(self):
super().get_resource_objects()
gid = self._get_kwarg(self.group_kw, default=None)
if gid:
self.group = get_object_or_404(StudentGroup,
course_instance=self.instance,
id=gid,
)
else:
self.group = StudentGroup(course_instance=self.instance)
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs["instance"] = self.group
return kwargs
def get_success_url(self):
return self.instance.get_url('groups-list')
def form_valid(self, form):
form.save()
messages.success(self.request, _("Changes were saved succesfully."))
return super().form_valid(form)
def form_invalid(self, form):
messages.error(self.request, _("Failed to save changes."))
return super().form_invalid(form)
class GroupsDeleteView(CourseInstanceMixin, BaseRedirectMixin, BaseTemplateView):
access_mode = ACCESS.ASSISTANT
group_kw = "group_id"
template_name = "course/staff/group_delete.html"
def get_resource_objects(self):
super().get_resource_objects()
self.group = get_object_or_404(StudentGroup,
course_instance=self.instance,
id=self._get_kwarg(self.group_kw),
)
self.note('group')
def post(self, request, *args, **kwargs):
self.group.delete()
return self.redirect(self.instance.get_url('groups-list'))