-def mark_answer(request, answer_id):
- answer = get_object_or_404(Answer, id=answer_id)
- if request.user not in answer.assignment.experts.all():
- return HttpResponseForbidden()
- if answer.assignment.is_active():
- return HttpResponseForbidden()
- mark, created = Mark.objects.get_or_create(answer=answer, expert=request.user, defaults={'points': 0})
- form = MarkForm(data=request.POST, answer=answer, instance=mark, prefix='ans%s' % answer.id)
- if form.is_valid():
- form.save()
- elif created:
- mark.delete()
-
- return HttpResponseRedirect(reverse(
- 'stage2_answer_list' if created else 'stage2_marked_answers', args=[answer.assignment_id]))
+def csv_results(request):
+ import csv
+ response = HttpResponse(content_type='text/csv')
+ writer = csv.writer(response)
+ assignments = Assignment.objects.all()
+ participants = Participant.objects.filter(complete_set=True)
+ headers = [u'imię', u'nazwisko', u'szkoła', u'adres szkoły']
+ assignments_experts = []
+ for assignment in assignments:
+ for expert in assignment.experts.filter(mark__answer__assignment=assignment).distinct():
+ assignments_experts.append((assignment, expert))
+ headers.append(u'%s %s' % (assignment.title, expert.last_name))
+ for assignment in assignments:
+ headers.append(u'%s - średnia' % assignment.title.encode('utf-8'))
+ headers.append(u'ostateczny wynik')
+ write_row(headers, writer)
+ for participant in participants:
+ row = [
+ participant.first_name,
+ participant.last_name,
+ participant.contact.body['school'],
+ participant.contact.body['school_address'],
+ ]
+ for assignment, expert in assignments_experts:
+ marks = expert.mark_set.filter(answer__assignment=assignment, answer__participant=participant)
+ if marks:
+ row.append(sum(mark.points for mark in marks))
+ else:
+ row.append('')
+ for assignment in assignments:
+ row.append('%.2f' % participant.answer_set.get(assignment=assignment).score())
+ row.append('%.2f' % participant.score())
+ 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