974e375754c308b879719b654c3b23ca132cc1c6
[edumed.git] / stage2 / views.py
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
10 from stage2.forms import AttachmentForm, MarkForm
11 from stage2.models import Participant, Assignment, Answer, Attachment, Mark
12
13
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()
18         assignment.forms = [
19             (AttachmentForm(assignment=assignment, file_no=i, label=label, extensions=ext),
20              assignment.answer.attachment_set.filter(file_no=i).first() if assignment.answer else None)
21             for i, (label, ext) in enumerate(assignment.file_descriptions, 1)]
22     return assignments
23
24
25 def participant_view(request, participant_id, key):
26     participant = get_object_or_404(Participant, id=participant_id)
27     if not participant.check(key):
28         raise Http404
29     return render(request, 'stage2/participant.html', {
30         'participant': participant,
31         'assignments': all_assignments(participant)})
32
33
34 @require_POST
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):
38         raise Http404
39     assignment = get_object_or_404(Assignment, id=assignment_id)
40     now = timezone.now()
41     if assignment.deadline < now:
42         raise Http404  # TODO za późno
43     for i, (label, ext) 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, extensions=ext)
49         if form.is_valid():
50             form.save()
51     return HttpResponseRedirect(reverse('stage2_participant', args=(participant_id, key)))
52
53
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()
59     return response
60
61
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):
67         raise Http404
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)
72
73
74 @login_required
75 def assignment_list(request):
76     assignments = request.user.stage2_assignments.all()
77     if not assignments:
78         return HttpResponseForbidden()
79     for assignment in assignments:
80         assignment.marked_count = Mark.objects.filter(expert=request.user, answer__assignment=assignment).count()
81         assignment.to_mark_count = assignment.available_answers(request.user).count()
82     non_empty_assignments = [ass for ass in assignments if ass.marked_count > 0 or ass.to_mark_count > 0]
83     if len(non_empty_assignments) == 1 and non_empty_assignments[0].to_mark_count > 0:
84         return HttpResponseRedirect(reverse('stage2_answer_list', args=[non_empty_assignments[0].id]))
85     return render(request, 'stage2/assignment_list.html', {'assignments': assignments})
86
87
88 def available_answers(assignment, expert, answer_with_errors=None, form_with_errors=None, marked=False):
89     if marked:
90         answers = Answer.objects.filter(mark__expert=expert, assignment=assignment)
91     else:
92         answers = assignment.available_answers(expert)
93     answers = answers.order_by('participant__last_name').prefetch_related('attachment_set')
94     for answer in answers:
95         attachments = answer.attachment_set.all()
96         attachments_by_file_no = {attachment.file_no: attachment for attachment in attachments}
97         answer.attachments = [
98             (desc, attachments_by_file_no.get(i))
99             for (i, (desc, ext)) in enumerate(assignment.file_descriptions, 1)]
100         if answer == answer_with_errors:
101             answer.form = form_with_errors
102         else:
103             answer.form = MarkForm(
104                 answer=answer, instance=answer.mark_set.filter(expert=expert).first(), prefix='ans%s' % answer.id)
105     return answers
106
107
108 @login_required
109 def answer_list(request, assignment_id):
110     assignment = get_object_or_404(Assignment, id=assignment_id)
111     if request.user not in assignment.experts.all():
112         return HttpResponseForbidden()
113     return render(request, 'stage2/answer_list.html',
114                   {'answers': available_answers(assignment, request.user), 'assignment': assignment})
115
116
117 @login_required
118 def marked_answer_list(request, assignment_id):
119     assignment = get_object_or_404(Assignment, id=assignment_id)
120     if request.user not in assignment.experts.all():
121         return HttpResponseForbidden()
122     return render(request, 'stage2/answer_list.html', {
123         'answers': available_answers(assignment, request.user, marked=True),
124         'assignment': assignment,
125         'marked': True,
126     })
127
128
129 @login_required
130 def expert_download(request, attachment_id):
131     attachment = get_object_or_404(Attachment, id=attachment_id)
132     return attachment_download(attachment)
133
134
135 @require_POST
136 @login_required
137 def mark_answer(request, answer_id):
138     answer = get_object_or_404(Answer, id=answer_id)
139     if request.user not in answer.assignment.experts.all():
140         return HttpResponseForbidden()
141     if answer.assignment.is_active():
142         return HttpResponseForbidden()
143     mark, created = Mark.objects.get_or_create(answer=answer, expert=request.user, defaults={'points': 0})
144     form = MarkForm(data=request.POST, answer=answer, instance=mark, prefix='ans%s' % answer.id)
145     if form.is_valid():
146         form.save()
147     elif created:
148         mark.delete()
149
150     return HttpResponseRedirect(reverse(
151         'stage2_answer_list' if created else 'stage2_marked_answers', args=[answer.assignment_id]))