From: Aleksander Ɓukasz Date: Wed, 13 Nov 2013 11:12:56 +0000 (+0100) Subject: Export results to csv X-Git-Url: https://git.mdrn.pl/edumed.git/commitdiff_plain/6612e6c0026cee6bbeb7845a5c5749420b409b3b Export results to csv --- diff --git a/wtem/admin.py b/wtem/admin.py index 08cff0d..15430cc 100644 --- a/wtem/admin.py +++ b/wtem/admin.py @@ -7,7 +7,9 @@ from django import forms from django.utils import simplejson from django.utils.safestring import mark_safe from django.core.urlresolvers import reverse - +from django.conf.urls import url, patterns +from django.shortcuts import render +from django.contrib.auth.models import User from .models import Submission, Assignment, Attachment, exercises from .middleware import get_current_request @@ -167,6 +169,32 @@ class SubmissionAdmin(admin.ModelAdmin): qs = qs.filter(examiners = request.user) return qs + def get_urls(self): + urls = super(SubmissionAdmin, self).get_urls() + return patterns('', + url(r'^report/$', self.admin_site.admin_view(report_view), name='wtem_admin_report') + ) + super(SubmissionAdmin, self).get_urls() + + +class SubmissionsSet: + def __init__(self, submissions): + self.submissions = submissions + self.examiners_by_exercise = dict() + for submission in submissions: + for user_id, marks in submission.marks.items(): + user = User.objects.get(pk=user_id) + for exercise_id in marks.keys(): + examiners = self.examiners_by_exercise.setdefault(exercise_id, []) + if not user in examiners: + examiners.append(user) + +def report_view(request): + submissions = Submission.objects.all() + submissions = sorted(submissions, key = lambda s: -s.final_result) + return render(request, 'wtem/admin_report.csv', dict( + submissionsSet = SubmissionsSet(submissions), + exercise_ids = map(str, range(1,len(exercises)+1)) + )) admin.site.register(Submission, SubmissionAdmin) admin.site.register(Assignment) \ No newline at end of file diff --git a/wtem/models.py b/wtem/models.py index 8e9d505..5325a58 100644 --- a/wtem/models.py +++ b/wtem/models.py @@ -70,6 +70,35 @@ class Submission(models.Model): del self.marks[user_id][exercise_id] + def get_exercise_marks_by_examiner(self, exercise_id): + marks = dict() + for examiner_id, examiner_marks in self.marks.items(): + mark = examiner_marks.get(exercise_id, None) + if mark is not None: + marks[examiner_id] = mark + return marks + + def get_final_exercise_mark(self, exercise_id): + exercise = exercises[int(exercise_id)-1] + if exercise_checked_manually(exercise): + marks_by_examiner = self.get_exercise_marks_by_examiner(exercise_id) + if len(marks_by_examiner): + return max(marks_by_examiner.values()) + else: + return None + else: + return 1 # TODO: actually calculate the result + + @property + def final_result(self): + final = 0 + for exercise_id in map(str,range(1, len(exercises) + 1)): + mark = self.get_final_exercise_mark(exercise_id) + if mark is not None: + final += int(mark) + return final + + class Attachment(models.Model): submission = models.ForeignKey(Submission) exercise_id = models.IntegerField() @@ -88,4 +117,8 @@ class Assignment(models.Model): raise ValidationError(_('Invalid exercise id: %s' % exercise)) def __unicode__(self): - return self.user.username + ': ' + ','.join(map(str,self.exercises)) \ No newline at end of file + return self.user.username + ': ' + ','.join(map(str,self.exercises)) + + +def exercise_checked_manually(exercise): + return (exercise['type'] in ('open', 'file_upload')) or 'open_part' in exercise \ No newline at end of file diff --git a/wtem/templates/admin/wtem/submission/change_list.html b/wtem/templates/admin/wtem/submission/change_list.html index 8f31489..e73e9bc 100644 --- a/wtem/templates/admin/wtem/submission/change_list.html +++ b/wtem/templates/admin/wtem/submission/change_list.html @@ -3,6 +3,15 @@ {% load admin_urls %} +{% block object-tools-items %} + {{block.super}} +
  • + + Wyniki w CSV + +
  • +{% endblock %} + {% block result_list %} {% if examiners %}

    diff --git a/wtem/templates/wtem/admin_report.csv b/wtem/templates/wtem/admin_report.csv new file mode 100644 index 0000000..5680d44 --- /dev/null +++ b/wtem/templates/wtem/admin_report.csv @@ -0,0 +1,4 @@ +{% load wtem_csv %} +{% spaceless %} +email,nazwisko,imie,suma{% for exercise_id in exercise_ids %}{% csv_header exercise_id submissionsSet %}{% endfor %}{% for submission in submissionsSet.submissions %} +{{submission.email}},{{submission.last_name}},{{submission.first_name}},{{submission.final_result}}{% for exercise_id in exercise_numbers %}{% csv_row_fragment exercise_id submission submissionsSet %}{% endfor %}{% endfor %}{% endspaceless %} \ No newline at end of file diff --git a/wtem/templatetags/__init__.py b/wtem/templatetags/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/wtem/templatetags/wtem_csv.py b/wtem/templatetags/wtem_csv.py new file mode 100644 index 0000000..9d444e7 --- /dev/null +++ b/wtem/templatetags/wtem_csv.py @@ -0,0 +1,27 @@ +from django import template + +register = template.Library() + + +@register.simple_tag +def csv_header(exercise_id, submissionSet): + examiners = submissionSet.examiners_by_exercise.get(exercise_id, []) + examiners_string = ','.join(['zad %s - %s' % (exercise_id, user.username) for user in examiners]) + toret = ',zad %s' % exercise_id + if examiners_string: + toret += ',' + examiners_string + return toret + +@register.simple_tag +def csv_row_fragment(exercise_id, submission, submissionSet): + toret = '%s' % submission.final_result + examiners = submissionSet.examiners_by_exercise[exercise_id] + marks_by_examiner = submission.get_exercise_marks_by_examiner(exercise_id) + for examiner in examiners: + mark = marks_by_examiner[examiner.id] + toret += ',' + if mark is None: + toret += '-' + else: + toret += str(mark) + return toret