show required extensions
[edumed.git] / stage2 / views.py
index 2015295..3b05eb0 100644 (file)
@@ -23,15 +23,16 @@ def all_assignments(participant, sent_forms):
         assignment.answer, created = Answer.objects.get_or_create(participant=participant, assignment=assignment)
         if assignment == sent_assignment:
             assignment.field_forms = field_forms
-            assignment.attachment_forms = attachment_forms
         else:
             assignment.field_forms = [
                 AssignmentFieldForm(label=label, field_no=i, options=options, answer=assignment.answer)
                 for i, (label, options) in enumerate(assignment.field_descriptions, 1)]
-            assignment.attachment_forms = [
-                (AttachmentForm(assignment=assignment, file_no=i, label=label, extensions=ext),
-                 assignment.answer.attachment_set.filter(file_no=i).first() if assignment.answer else None)
-                for i, (label, ext) in enumerate(assignment.file_descriptions, 1)]
+        # in theory, if assignment == sent_assignment, it should be copied like field_forms,
+        # but somehow it doesn't work as expected
+        assignment.attachment_forms = [
+            (AttachmentForm(assignment=assignment, file_no=i, label=label, options=options),
+             assignment.answer.attachment_set.filter(file_no=i).first() if assignment.answer else None)
+            for i, (label, options) in enumerate(assignment.file_descriptions, 1)]
     return assignments
 
 
@@ -54,7 +55,11 @@ def participant_view(request, participant_id, key):
             return HttpResponseForbidden('Not Allowed')
         attachments_valid, attachment_forms = get_attachment_forms(assignment, participant, request)
         fields_valid, field_forms = get_field_forms(assignment, participant, request)
-        if attachments_valid and fields_valid:
+        # tutaj w zasadzie powinno być też sprawdzenie attachments_valid, ale to powoduje,
+        # że jeśli jakiś plik nie został wysłany, to traktujemy to jako błąd, a tego nie chcemy.
+        # trzeba by znaleźć sensowny sposób odrózniania błędnego pliku od braku pliku.
+        # na szczęście pliki walidujemy też javascriptem, więc jakoś ujdzie
+        if fields_valid:
             return HttpResponseRedirect(reverse('stage2_participant', args=(participant_id, key)))
         else:
             sent_forms = (assignment, field_forms, attachment_forms)
@@ -71,12 +76,12 @@ def participant_view(request, participant_id, key):
 def get_attachment_forms(assignment, participant, request):
     all_valid = True
     attachment_forms = []
-    for i, (label, ext) in enumerate(assignment.file_descriptions, 1):
+    for i, (label, options) in enumerate(assignment.file_descriptions, 1):
         answer, created = Answer.objects.get_or_create(participant=participant, assignment=assignment)
         attachment, created = Attachment.objects.get_or_create(answer=answer, file_no=i)
         form = AttachmentForm(
             data=request.POST, files=request.FILES,
-            assignment=assignment, file_no=i, label=label, instance=attachment, extensions=ext)
+            assignment=assignment, file_no=i, label=label, instance=attachment, options=options)
         if form.is_valid():
             form.save()
         else:
@@ -234,6 +239,10 @@ def expert_download(request, attachment_id):
     return attachment_download(attachment)
 
 
+def write_row(row, writer):
+    writer.writerow([unicode(item).encode('utf-8') for item in row])
+
+
 @login_required
 def csv_results(request):
     import csv
@@ -250,7 +259,7 @@ def csv_results(request):
     for assignment in assignments:
         headers.append(u'%s - średnia' % assignment.title.encode('utf-8'))
     headers.append(u'ostateczny wynik')
-    writer.writerow([unicode(item).encode('utf-8') for item in headers])
+    write_row(headers, writer)
     for participant in participants:
         row = [
             participant.first_name,
@@ -267,6 +276,34 @@ def csv_results(request):
         for assignment in assignments:
             row.append('%.2f' % participant.answer_set.get(assignment=assignment).score())
         row.append('%.2f' % participant.score())
-        writer.writerow([unicode(item).encode('utf-8') for item in row])
+        write_row(row, writer)
     response['Content-Disposition'] = 'attachment; filename="wyniki.csv"'
     return response
+
+
+@login_required
+def csv_details(request, assignment_id):
+    import csv
+    response = HttpResponse(content_type='text/csv')
+    writer = csv.writer(response)
+    assignment = get_object_or_404(Assignment, id=assignment_id)
+    criteria = tuple(assignment.markcriterion_set.all())
+    expert_headers = (u"ekspert",) + criteria
+    headers = (u"imię", u"nazwisko", u"numer", u"wynik") + expert_headers * 2
+    write_row(headers, writer)
+
+    for a in assignment.answer_set.filter(participant__complete_set=True).order_by('participant__last_name'):
+        row = (
+            a.participant.first_name,
+            a.participant.last_name,
+            str(a.participant.id),
+            '%.2f' % a.score(),
+        )
+        experts = a.mark_set.values_list('expert__username', flat=True).order_by().distinct()
+        for expert in experts:
+            row += (expert,)
+            marks = a.mark_set.filter(expert__username=expert).order_by('criterion__order')
+            row += tuple(marks.values_list('points', flat=True))
+        write_row(row, writer)
+    response['Content-Disposition'] = 'attachment; filename="%s.csv"' % assignment.title
+    return response