1 # -*- coding: utf-8 -*-
 
   2 from django.core.urlresolvers import reverse
 
   3 from django.http import Http404
 
   4 from django.http.response import HttpResponseRedirect, HttpResponse
 
   5 from django.shortcuts import get_object_or_404, render
 
   6 from django.utils import timezone
 
   7 from django.views.decorators.http import require_POST
 
   9 from stage2.forms import AttachmentForm
 
  10 from stage2.models import Participant, Assignment, Answer, Attachment
 
  13 def participant_view(request, participant_id, key):
 
  14     participant = get_object_or_404(Participant, id=participant_id)
 
  15     if not participant.check(key):
 
  18     assignments = Assignment.objects.all()
 
  19     for assignment in assignments:
 
  20         assignment.active = assignment.deadline >= now
 
  21         assignment.answer = assignment.answer_set.filter(participant=participant).first()
 
  23             (AttachmentForm(assignment=assignment, file_no=i, label=label),
 
  24              assignment.answer.attachment_set.filter(file_no=i).first() if assignment.answer else None)
 
  25             for i, label in enumerate(assignment.file_descriptions, 1)]
 
  26     return render(request, 'stage2/participant.html', {
 
  27         'participant': participant,
 
  28         'assignments': assignments})
 
  32 def upload(request, assignment_id, participant_id, key):
 
  33     participant = get_object_or_404(Participant, id=participant_id)
 
  34     if not participant.check(key):
 
  36     assignment = get_object_or_404(Assignment, id=assignment_id)
 
  38     if assignment.deadline < now:
 
  39         raise Http404  # TODO za późno
 
  40     for i, label in enumerate(assignment.file_descriptions, 1):
 
  41         answer, created = Answer.objects.get_or_create(participant=participant, assignment=assignment)
 
  42         attachment, created = Attachment.objects.get_or_create(answer=answer, file_no=i)
 
  43         form = AttachmentForm(
 
  44             data=request.POST, files=request.FILES,
 
  45             assignment=assignment, file_no=i, label=label, instance=attachment)
 
  48     return HttpResponseRedirect(reverse('stage2_participant', args=(participant_id, key)))
 
  51 def get_file(request, assignment_id, file_no, participant_id, key):
 
  52     """We want to serve submitted files back to participants, but also validate their keys,
 
  53        so static files are not good"""
 
  54     participant = get_object_or_404(Participant, id=participant_id)
 
  55     if not participant.check(key):
 
  57     assignment = get_object_or_404(Assignment, id=assignment_id)
 
  58     answer = get_object_or_404(Answer, participant=participant, assignment=assignment)
 
  59     attachment = get_object_or_404(Attachment, answer=answer, file_no=file_no)
 
  60     response = HttpResponse(content_type='application/force-download')
 
  61     response.write(attachment.file.read())
 
  62     response['Content-Disposition'] = 'attachment; filename="%s"' % attachment.filename()
 
  63     response['Content-Length'] = response.tell()