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
9 from unidecode import unidecode
11 from stage2.forms import AttachmentForm, MarkForm
12 from stage2.models import Participant, Assignment, Answer, Attachment, Mark
15 def all_assignments(participant):
16 assignments = Assignment.objects.all()
17 for assignment in assignments:
18 assignment.answer = assignment.answer_set.filter(participant=participant).first()
20 (AttachmentForm(assignment=assignment, file_no=i, label=label, extensions=ext),
21 assignment.answer.attachment_set.filter(file_no=i).first() if assignment.answer else None)
22 for i, (label, ext) in enumerate(assignment.file_descriptions, 1)]
26 def participant_view(request, participant_id, key):
27 participant = get_object_or_404(Participant, id=participant_id)
28 if not participant.check(key):
30 return render(request, 'stage2/participant.html', {
31 'participant': participant,
32 'assignments': all_assignments(participant)})
36 def upload(request, assignment_id, participant_id, key):
37 participant = get_object_or_404(Participant, id=participant_id)
38 if not participant.check(key):
40 assignment = get_object_or_404(Assignment, id=assignment_id)
42 if assignment.deadline < now:
43 raise Http404 # TODO za późno
44 for i, (label, ext) in enumerate(assignment.file_descriptions, 1):
45 answer, created = Answer.objects.get_or_create(participant=participant, assignment=assignment)
46 attachment, created = Attachment.objects.get_or_create(answer=answer, file_no=i)
47 form = AttachmentForm(
48 data=request.POST, files=request.FILES,
49 assignment=assignment, file_no=i, label=label, instance=attachment, extensions=ext)
52 return HttpResponseRedirect(reverse('stage2_participant', args=(participant_id, key)))
55 def attachment_download(attachment):
56 response = HttpResponse(content_type='application/force-download')
57 response.write(attachment.file.read())
58 # workaround to this: https://code.djangoproject.com/ticket/20889
59 response['Content-Disposition'] = 'attachment; filename="%s"' % unidecode(attachment.filename().replace('\n', ' '))
60 response['Content-Length'] = response.tell()
64 def get_file(request, assignment_id, file_no, participant_id, key):
65 """We want to serve submitted files back to participants, but also validate their keys,
66 so static files are not good"""
67 participant = get_object_or_404(Participant, id=participant_id)
68 if not participant.check(key):
70 assignment = get_object_or_404(Assignment, id=assignment_id)
71 answer = get_object_or_404(Answer, participant=participant, assignment=assignment)
72 attachment = get_object_or_404(Attachment, answer=answer, file_no=file_no)
73 return attachment_download(attachment)
77 def assignment_list(request):
78 assignments = request.user.stage2_assignments.all()
80 return HttpResponseForbidden('Not allowed')
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 non_empty_assignments = [ass for ass in assignments if ass.marked_count > 0 or ass.to_mark_count > 0]
85 if len(non_empty_assignments) == 1 and non_empty_assignments[0].to_mark_count > 0:
86 return HttpResponseRedirect(reverse('stage2_answer_list', args=[non_empty_assignments[0].id]))
87 return render(request, 'stage2/assignment_list.html', {'assignments': assignments})
90 def available_answers(assignment, expert, answer_with_errors=None, form_with_errors=None, marked=False):
92 answers = Answer.objects.filter(mark__expert=expert, assignment=assignment)
94 answers = assignment.available_answers(expert)
95 answers = answers.order_by('participant__last_name').prefetch_related('attachment_set')
96 for answer in answers:
97 attachments = answer.attachment_set.all()
98 attachments_by_file_no = {attachment.file_no: attachment for attachment in attachments}
99 answer.attachments = [
100 (desc, attachments_by_file_no.get(i))
101 for (i, (desc, ext)) in enumerate(assignment.file_descriptions, 1)]
102 if answer == answer_with_errors:
103 answer.form = form_with_errors
105 answer.form = MarkForm(
106 answer=answer, instance=answer.mark_set.filter(expert=expert).first(), prefix='ans%s' % answer.id)
111 def answer_list(request, assignment_id):
112 assignment = get_object_or_404(Assignment, id=assignment_id)
113 if request.user not in assignment.experts.all():
114 return HttpResponseForbidden('Not allowed')
115 return render(request, 'stage2/answer_list.html',
116 {'answers': available_answers(assignment, request.user), 'assignment': assignment})
120 def marked_answer_list(request, assignment_id):
121 assignment = get_object_or_404(Assignment, id=assignment_id)
122 if request.user not in assignment.experts.all():
123 return HttpResponseForbidden('Not allowed')
124 return render(request, 'stage2/answer_list.html', {
125 'answers': available_answers(assignment, request.user, marked=True),
126 'assignment': assignment,
132 def expert_download(request, attachment_id):
133 attachment = get_object_or_404(Attachment, id=attachment_id)
134 return attachment_download(attachment)
139 def mark_answer(request, answer_id):
140 answer = get_object_or_404(Answer, id=answer_id)
141 if request.user not in answer.assignment.experts.all():
142 return HttpResponseForbidden('Not allowed')
143 if answer.assignment.is_active():
144 return HttpResponseForbidden('Not allowed')
145 mark, created = Mark.objects.get_or_create(answer=answer, expert=request.user, defaults={'points': 0})
146 form = MarkForm(data=request.POST, answer=answer, instance=mark, prefix='ans%s' % answer.id)
152 return HttpResponseRedirect(reverse(
153 'stage2_answer_list' if created else 'stage2_marked_answers', args=[answer.assignment_id]))