+ return attachment_download(attachment)
+
+
+@login_required
+def assignment_list(request):
+ assignments = request.user.stage2_assignments.all()
+ if not assignments:
+ return HttpResponseForbidden('Not allowed')
+ for assignment in assignments:
+ assignment.marked_count = Mark.objects.filter(expert=request.user, answer__assignment=assignment).count()
+ assignment.to_mark_count = assignment.available_answers(request.user).count()
+ non_empty_assignments = [ass for ass in assignments if ass.marked_count > 0 or ass.to_mark_count > 0]
+ if len(non_empty_assignments) == 1 and non_empty_assignments[0].to_mark_count > 0:
+ return HttpResponseRedirect(reverse('stage2_answer_list', args=[non_empty_assignments[0].id]))
+ return render(request, 'stage2/assignment_list.html', {'assignments': assignments})
+
+
+def available_answers(assignment, expert, answer_with_errors=None, form_with_errors=None, marked=False):
+ if marked:
+ answers = Answer.objects.filter(mark__expert=expert, assignment=assignment)
+ else:
+ answers = assignment.available_answers(expert)
+ answers = answers.order_by('participant__last_name').prefetch_related('attachment_set')
+ for answer in answers:
+ attachments = answer.attachment_set.all()
+ attachments_by_file_no = {attachment.file_no: attachment for attachment in attachments}
+ answer.attachments = [
+ (desc, attachments_by_file_no.get(i))
+ for (i, (desc, ext)) in enumerate(assignment.file_descriptions, 1)]
+ if answer == answer_with_errors:
+ answer.form = form_with_errors
+ else:
+ answer.form = MarkForm(
+ answer=answer, instance=answer.mark_set.filter(expert=expert).first(), prefix='ans%s' % answer.id)
+ return answers
+
+
+@login_required
+def answer_list(request, assignment_id):
+ assignment = get_object_or_404(Assignment, id=assignment_id)
+ if request.user not in assignment.experts.all():
+ return HttpResponseForbidden('Not allowed')
+ return render(request, 'stage2/answer_list.html',
+ {'answers': available_answers(assignment, request.user), 'assignment': assignment})
+
+
+@login_required
+def marked_answer_list(request, assignment_id):
+ assignment = get_object_or_404(Assignment, id=assignment_id)
+ if request.user not in assignment.experts.all():
+ return HttpResponseForbidden('Not allowed')
+ return render(request, 'stage2/answer_list.html', {
+ 'answers': available_answers(assignment, request.user, marked=True),
+ 'assignment': assignment,
+ 'marked': True,
+ })
+
+
+@login_required
+def expert_download(request, attachment_id):
+ attachment = get_object_or_404(Attachment, id=attachment_id)
+ return attachment_download(attachment)
+
+
+@require_POST
+@login_required
+def mark_answer(request, answer_id):
+ answer = get_object_or_404(Answer, id=answer_id)
+ if request.user not in answer.assignment.experts.all():
+ return HttpResponseForbidden('Not allowed')
+ if answer.assignment.is_active():
+ return HttpResponseForbidden('Not allowed')
+ mark, created = Mark.objects.get_or_create(answer=answer, expert=request.user, defaults={'points': 0})
+ form = MarkForm(data=request.POST, answer=answer, instance=mark, prefix='ans%s' % answer.id)
+ if form.is_valid():
+ form.save()
+ elif created:
+ mark.delete()
+
+ return HttpResponseRedirect(reverse(
+ 'stage2_answer_list' if created else 'stage2_marked_answers', args=[answer.assignment_id]))