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
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
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()
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
{% load admin_urls %}
+{% block object-tools-items %}
+ {{block.super}}
+ <li>
+ <a href="{% url 'admin:wtem_admin_report' %}">
+ Wyniki w CSV
+ </a>
+ </li>
+{% endblock %}
+
{% block result_list %}
{% if examiners %}
<p>
--- /dev/null
+{% 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
--- /dev/null
+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