views.py
8.82 KB
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
from django.shortcuts import render, get_object_or_404, redirect
from django.views import generic
from django.contrib.auth.decorators import login_required
from django.core.paginator import Paginator, EmptyPage
from django.contrib.auth.mixins import LoginRequiredMixin
from rolepermissions.mixins import HasRoleMixin
from django.core.urlresolvers import reverse_lazy
from django.utils.translation import ugettext_lazy as _
from rolepermissions.verifications import has_role
from rolepermissions.verifications import has_object_permission
from django.db.models import Q
# from django.views.generic.edit import FormMixin
from .forms import ExamForm
from .models import Exam, Answer, AnswersStudent
from core.mixins import NotificationMixin
from users.models import User
from courses.models import Course, Topic
class ViewExam(LoginRequiredMixin,generic.DetailView):
model = Exam
context_object_name = 'exam'
template_name = 'exam/view.html'
def get_object(self, queryset=None):
return get_object_or_404(Topic, slug = self.kwargs.get('slug'))
def get_context_data(self, **kwargs):
context = super(ViewExam, self).get_context_data(**kwargs)
exam = self.object
context["topic"] = exam.topic
context['course'] = exam.topic.subject.course
context['subject'] = exam.topic.subject
context['subjects'] = exam.topic.subject.course.subjects.all()
answered = AnswersStudent.objects.filter(exam = exam, student=self.request.user)
print (answered)
if answered.count()<1:
context['status'] = False
else:
context['status'] = answered[0].status
return context
class CreateExam(LoginRequiredMixin,HasRoleMixin, NotificationMixin,generic.CreateView):
allowed_roles = ['professor', 'system_admin']
login_url = reverse_lazy("core:home")
redirect_field_name = 'next'
model = Exam
form_class = ExamForm
context_object_name = 'exam'
template_name = 'exam/create.html'
def form_invalid(self, form,**kwargs):
context = super(CreateExam, self).form_invalid(form)
answers = {}
for key in self.request.POST:
if(key != 'csrfmiddlewaretoken' and key != 'name' and key != 'begin_date' and key != 'limit_date' and key != 'all_students' and key != 'students'):
answers[key] = self.request.POST[key]
keys = sorted(answers)
context.context_data['answers'] = answers
context.context_data['keys'] = keys
context.context_data['form'] = form
context.status_code = 400
return context
def form_valid(self, form):
self.object = form.save(commit = False)
topic = get_object_or_404(Topic, slug = self.kwargs.get('slug'))
self.object.topic = topic
self.object.name = str(self.object)
self.object.save()
super(CreateExam, self).createNotification(message="created an Exam "+ self.object.name, actor=self.request.user,
resource_name=self.object.name, resource_link= reverse('course:exam:view_exam', args=[self.object.slug]),
users=self.object.topic.subject.students.all())
for key in self.request.POST:
if(key != 'csrfmiddlewaretoken' and key != 'name' and key != 'begin_date' and key != 'limit_date' and key != 'all_students' and key != 'students'):
answer = Answer(answer=self.request.POST[key],order=key,exam=self.object)
answer.save()
return self.render_to_response(self.get_context_data(form = form), status = 200)
def get_context_data(self, **kwargs):
context = super(CreateExam, self).get_context_data(**kwargs)
topic = get_object_or_404(Topic, slug = self.kwargs.get('slug'))
context['course'] = topic.subject.course
context['subject'] = topic.subject
context['subjects'] = topic.subject.course.subjects.all()
return context
class UpdateExam(LoginRequiredMixin,HasRoleMixin,generic.UpdateView):
allowed_roles = ['professor', 'system_admin']
login_url = reverse_lazy("core:home")
redirect_field_name = 'next'
model = Exam
form_class = ExamForm
context_object_name = 'exam'
template_name = 'exam/update.html'
success_url = reverse_lazy('core:home')
def dispatch(self, *args, **kwargs):
exam = get_object_or_404(Exam, slug = self.kwargs.get('slug'))
if(not has_object_permission('edit_exam', self.request.user, exam)):
return self.handle_no_permission()
return super(UpdateExam, self).dispatch(*args, **kwargs)
def get_object(self, queryset=None):
return get_object_or_404(Exam, slug = self.kwargs.get('slug'))
def form_invalid(self, form,**kwargs):
context = super(UpdateExam, self).form_invalid(form)
answers = {}
for key in self.request.POST:
if(key != 'csrfmiddlewaretoken' and key != 'name' and key != 'begin_date' and key != 'limit_date' and key!= 'exibe' and key != 'all_students' and key != 'students'):
answers[key] = self.request.POST[key]
keys = sorted(answers)
context.context_data['answers'] = answers
context.context_data['keys'] = keys
context.context_data['form'] = form
context.status_code = 400
return context
def form_valid(self, form):
exam = self.object
exam = form.save(commit = False)
exam.answers.all().delete()
exam.save()
for key in self.request.POST:
if(key != 'csrfmiddlewaretoken' and key != 'name' and key != 'begin_date' and key != 'limit_date' and key!= 'exibe' and key != 'all_students' and key != 'students'):
answer = Answer(answer=self.request.POST[key],order=key,exam=exam)
answer.save()
return super(UpdateExam, self).form_valid(form)
def get_context_data(self, **kwargs):
context = super(UpdateExam, self).get_context_data(**kwargs)
exam = self.object
context['course'] = exam.topic.subject.course
context['subject'] = exam.topic.subject
context['subjects'] = exam.topic.subject.course.subjects.all()
answers = {}
for answer in exam.answers.all():
answers[answer.order] = answer.answer
keys = sorted(answers)
context['answers'] = answers
context['keys'] = keys
return context
class DeleteExam(LoginRequiredMixin, HasRoleMixin, generic.DeleteView):
allowed_roles = ['professor', 'system_admin']
login_url = reverse_lazy("core:home")
redirect_field_name = 'next'
model = Exam
template_name = 'exam/remove.html'
def dispatch(self, *args, **kwargs):
exam = get_object_or_404(Exam, slug = self.kwargs.get('slug'))
if(not has_object_permission('delete_exam', self.request.user, exam)):
return self.handle_no_permission()
return super(DeleteExam, self).dispatch(*args, **kwargs)
def get_context_data(self, **kwargs):
context = super(DeleteExam, self).get_context_data(**kwargs)
context['course'] = self.object.topic.subject.course
context['subject'] = self.object.topic.subject
context['exam'] = self.object
context['subjects'] = self.object.topic.subject.course.subjects.filter(Q(visible=True) | Q(professors__in=[self.request.user]))
if (has_role(self.request.user,'system_admin')):
context['subjects'] = self.object.topic.subject.course.subjects.all()
return context
def get_success_url(self):
return reverse_lazy('course:view_topic', kwargs={'slug' : self.object.topic.slug})
class AnswerExam(generic.TemplateView):
template_name = 'exam/answer.html'
class AnswerStudentExam(LoginRequiredMixin,generic.CreateView):
model = AnswersStudent
fields = ['status']
context_object_name = 'answer'
template_name = 'exam/answer_student.html'
def form_valid(self, form):
exam = get_object_or_404(Exam, slug = self.kwargs.get('slug'))
answers = AnswersStudent(
status = True,
exam = exam,
student = self.request.user,
)
answers.save()
for key in self.request.POST:
if(key != 'csrfmiddlewaretoken'):
answers.answer.add(exam.answers.all().filter(order=key)[0])
return self.render_to_response(self.get_context_data(form = form), status = 200)
def get_context_data(self, **kwargs):
context = super(AnswerStudentExam, self).get_context_data(**kwargs)
print (self.kwargs.get('slug'))
exam = get_object_or_404(Exam, slug = self.kwargs.get('slug'))
context['exam'] = exam
context['topic'] = exam.topic
context['course'] = exam.topic.subject.course
context['subject'] = exam.topic.subject
context['subjects'] = exam.topic.subject.course.subjects.all()
print (self.request.method)
answers = {}
for answer in exam.answers.all():
answers[answer.order] = answer.answer
keys = sorted(answers)
context['answers'] = answers
context['keys'] = keys
return context
class MultipleChoiceQuestion(generic.TemplateView):
template_name = 'exam/multiple_choice_question.html'
class MultipleChoiceAnswer(generic.TemplateView):
template_name = 'exam/multiple_choice_answer.html'
class DiscursiveQuestion(generic.TemplateView):
template_name = 'exam/discursive_question.html'
class TrueOrFalseQuestion(generic.TemplateView):
template_name = 'exam/true_or_false_question.html'
class TrueOrFalseAnswer(generic.TemplateView):
template_name = 'exam/true_or_false_answer.html'
class GapFillingQuestion(generic.TemplateView):
template_name = 'exam/gap_filling_question.html'
class GapFillingAnswer(generic.TemplateView):
template_name = 'exam/gap_filling_answer.html'