1
|
|
|
# Grading scheme admin interface |
2
|
|
|
|
3
|
|
|
from django.contrib.admin import ModelAdmin |
4
|
|
|
from django.db.models import Q |
5
|
|
|
from django.core.urlresolvers import resolve |
6
|
|
|
from opensubmit.models import Course, Grading |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
def gradings(gradingScheme): |
10
|
|
|
''' Determine the list of gradings in this scheme as rendered string. |
11
|
|
|
TODO: Use nice little icons instead of (p) / (f) marking. |
12
|
|
|
''' |
13
|
|
|
result = [] |
14
|
|
|
for grading in gradingScheme.gradings.all(): |
15
|
|
|
if grading.means_passed: |
16
|
|
|
result.append(str(grading) + " (pass)") |
17
|
|
|
else: |
18
|
|
|
result.append(str(grading) + " (fail)") |
19
|
|
|
return ' - '.join(result) |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
def courses(gradingScheme): |
23
|
|
|
# determine the courses that use this grading scheme in one of their assignments |
24
|
|
|
course_ids = gradingScheme.assignments.all().values_list('course', flat=True) |
25
|
|
|
courses = Course.objects.filter(pk__in=course_ids) |
26
|
|
|
return ",\n".join([str(course) for course in courses]) |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
class GradingSchemeAdmin(ModelAdmin): |
30
|
|
|
list_display = ['__str__', gradings, courses] |
31
|
|
|
|
32
|
|
|
class Media: |
33
|
|
|
css = {'all': ('css/teacher.css',)} |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
def formfield_for_dbfield(self, db_field, **kwargs): |
37
|
|
|
''' Offer only gradings that are not used by other schemes, which means they are used by this scheme or not at all.''' |
38
|
|
|
if db_field.name == "gradings": |
39
|
|
|
request=kwargs['request'] |
40
|
|
|
try: |
41
|
|
|
#TODO: MockRequst object from unit test does not contain path information, so an exception occurs during test. |
42
|
|
|
# Find a test-suite compatible solution here. |
43
|
|
|
obj=resolve(request.path).args[0] |
44
|
|
|
filterexpr=Q(schemes=obj) | Q(schemes=None) |
45
|
|
|
kwargs['queryset'] = Grading.objects.filter(filterexpr).distinct() |
46
|
|
|
except: |
47
|
|
|
pass |
48
|
|
|
return super(GradingSchemeAdmin, self).formfield_for_dbfield(db_field, **kwargs) |
49
|
|
|
|