From: Jan Szejko Date: Thu, 16 Mar 2017 13:43:34 +0000 (+0100) Subject: csv results for stage 2 X-Git-Url: https://git.mdrn.pl/edumed.git/commitdiff_plain/2102ac172d69c795868236a69951b303f4330375?ds=inline csv results for stage 2 --- diff --git a/stage2/models.py b/stage2/models.py index 972376a..924b153 100644 --- a/stage2/models.py +++ b/stage2/models.py @@ -121,7 +121,7 @@ class Answer(models.Model): marks = self.mark_set.all() if len(marks) < 2: return None - return self.mark_set.aggregate(models.Avg('points')) + return self.mark_set.aggregate(avg=models.Avg('points'))['avg'] def attachment_path(instance, filename): diff --git a/stage2/urls.py b/stage2/urls.py index 081c2b0..98fe2e1 100644 --- a/stage2/urls.py +++ b/stage2/urls.py @@ -14,4 +14,5 @@ urlpatterns = ( url(r'^zadania/(?P[0-9]*)/ocenione/$', views.marked_answer_list, name='stage2_marked_answers'), url(r'^plik/(?P[0-9]*)/$', views.expert_download, name='stage2_expert_download'), url(r'^mark/(?P[0-9]*)/$', views.mark_answer, name='stage2_mark_answer'), + url(r'^csv-results/', views.csv_results, name='stage2_csv_results'), ) diff --git a/stage2/views.py b/stage2/views.py index 3d6a72d..58bdf97 100644 --- a/stage2/views.py +++ b/stage2/views.py @@ -160,3 +160,41 @@ def mark_answer(request, answer_id): return HttpResponseRedirect(reverse( 'stage2_answer_list' if created else 'stage2_marked_answers', args=[answer.assignment_id])) + + +@login_required +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'] + 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') + writer.writerow([unicode(item).encode('utf-8') for item in headers]) + for participant in participants: + row = [ + participant.first_name, + participant.last_name, + participant.contact.body['school'], + ] + for assignment, expert in assignments_experts: + try: + row.append( + Mark.objects.get( + expert=expert, answer__assignment=assignment, answer__participant=participant).points) + except Mark.DoesNotExist: + row.append('') + 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]) + response['Content-Disposition'] = 'attachment; filename="wyniki.csv"' + return response