1 # -*- coding: utf-8 -*-
2 from django.contrib.auth.decorators import login_required
3 from django.core.urlresolvers import reverse
4 from django.http import Http404
5 from django.http.response import HttpResponseRedirect, HttpResponse, HttpResponseForbidden
6 from django.shortcuts import get_object_or_404, render
7 from django.utils import timezone
8 from django.views.decorators.http import require_POST
10 from stage2.forms import AttachmentForm, MarkForm
11 from stage2.models import Participant, Assignment, Answer, Attachment, Mark
14 def all_assignments(participant):
15 assignments = Assignment.objects.all()
16 for assignment in assignments:
17 assignment.answer = assignment.answer_set.filter(participant=participant).first()
19 (AttachmentForm(assignment=assignment, file_no=i, label=label),
20 assignment.answer.attachment_set.filter(file_no=i).first() if assignment.answer else None)
21 for i, label in enumerate(assignment.file_descriptions, 1)]
25 def participant_view(request, participant_id, key):
26 participant = get_object_or_404(Participant, id=participant_id)
27 if not participant.check(key):
29 return render(request, 'stage2/participant.html', {
30 'participant': participant,
31 'assignments': all_assignments(participant)})
35 def upload(request, assignment_id, participant_id, key):
36 participant = get_object_or_404(Participant, id=participant_id)
37 if not participant.check(key):
39 assignment = get_object_or_404(Assignment, id=assignment_id)
41 if assignment.deadline < now:
42 raise Http404 # TODO za późno
43 for i, label in enumerate(assignment.file_descriptions, 1):
44 answer, created = Answer.objects.get_or_create(participant=participant, assignment=assignment)
45 attachment, created = Attachment.objects.get_or_create(answer=answer, file_no=i)
46 form = AttachmentForm(
47 data=request.POST, files=request.FILES,
48 assignment=assignment, file_no=i, label=label, instance=attachment)
51 return HttpResponseRedirect(reverse('stage2_participant', args=(participant_id, key)))
54 def attachment_download(attachment):
55 response = HttpResponse(content_type='application/force-download')
56 response.write(attachment.file.read())
57 response['Content-Disposition'] = 'attachment; filename="%s"' % attachment.filename()
58 response['Content-Length'] = response.tell()
62 def get_file(request, assignment_id, file_no, participant_id, key):
63 """We want to serve submitted files back to participants, but also validate their keys,
64 so static files are not good"""
65 participant = get_object_or_404(Participant, id=participant_id)
66 if not participant.check(key):
68 assignment = get_object_or_404(Assignment, id=assignment_id)
69 answer = get_object_or_404(Answer, participant=participant, assignment=assignment)
70 attachment = get_object_or_404(Attachment, answer=answer, file_no=file_no)
71 return attachment_download(attachment)
75 def assignment_list(request):
76 assignments = request.user.stage2_assignments.all()
78 return HttpResponseForbidden()
79 if len(assignments) == 1:
80 return HttpResponseRedirect(reverse('stage2_answer_list', args=[assignments.get().id]))
81 for assignment in assignments:
82 assignment.marked_count = Mark.objects.filter(expert=request.user, answer__assignment=assignment).count()
83 assignment.to_mark_count = assignment.available_answers(request.user).count()
84 return render(request, 'stage2/assignment_list.html', {'assignments': assignments})
87 def available_answers(assignment, expert, answer_with_errors=None, form_with_errors=None, marked=False):
89 answers = Answer.objects.filter(mark__expert=expert, assignment=assignment)
91 answers = assignment.available_answers(expert)
92 answers = answers.order_by('participant__last_name').prefetch_related('attachment_set')
93 for answer in answers:
94 attachments = answer.attachment_set.all()
95 attachments_by_file_no = {attachment.file_no: attachment for attachment in attachments}
96 answer.attachments = [
97 (desc, attachments_by_file_no.get(i))
98 for (i, desc) in enumerate(assignment.file_descriptions, 1)]
99 if answer == answer_with_errors:
100 answer.form = form_with_errors
102 answer.form = MarkForm(
103 answer=answer, instance=answer.mark_set.filter(expert=expert).first(), prefix='ans%s' % answer.id)
108 def answer_list(request, assignment_id):
109 assignment = get_object_or_404(Assignment, id=assignment_id)
110 if request.user not in assignment.experts.all():
111 return HttpResponseForbidden()
112 return render(request, 'stage2/answer_list.html',
113 {'answers': available_answers(assignment, request.user), 'assignment': assignment})
117 def marked_answer_list(request, assignment_id):
118 assignment = get_object_or_404(Assignment, id=assignment_id)
119 if request.user not in assignment.experts.all():
120 return HttpResponseForbidden()
121 return render(request, 'stage2/answer_list.html', {
122 'answers': available_answers(assignment, request.user, marked=True),
123 'assignment': assignment,
129 def expert_download(request, attachment_id):
130 attachment = get_object_or_404(Attachment, id=attachment_id)
131 return attachment_download(attachment)
136 def mark_answer(request, answer_id):
137 answer = get_object_or_404(Answer, id=answer_id)
138 if request.user not in answer.assignment.experts.all():
139 return HttpResponseForbidden()
140 if answer.assignment.is_active():
141 return HttpResponseForbidden()
142 mark, created = Mark.objects.get_or_create(answer=answer, expert=request.user, defaults={'points': 0})
143 form = MarkForm(data=request.POST, answer=answer, instance=mark, prefix='ans%s' % answer.id)
149 return HttpResponseRedirect(reverse(
150 'stage2_answer_list' if created else 'stage2_marked_answers', args=[answer.assignment_id]))