672666b14c34bc6b2a60fae4d6a76f62094c5c02
[edumed.git] / wtem / admin.py
1 # -*- coding: utf-8 -*-
2
3 import os
4
5 from django.contrib import admin
6 from django import forms
7 from django.utils import simplejson
8 from django.core.urlresolvers import reverse
9
10 from .models import Submission, Assignment
11
12
13 f = file(os.path.dirname(__file__) + '/fixtures/exercises.json')
14 exercises = simplejson.loads(f.read())
15 f.close()
16
17 def get_user_exercises(user):
18     assignment = Assignment.objects.get(user = user)
19     return [e for e in exercises if e['id'] in assignment.exercises]
20
21
22 readonly_fields = ('submitted_by', 'first_name', 'last_name', 'email', 'key', 'key_sent')
23
24
25 class SubmissionFormBase(forms.ModelForm):
26     class Meta:
27         model = Submission
28         exclude = ('answers', 'marks', 'contact') + readonly_fields
29
30
31 def get_form(request, submission):
32     fields = dict()
33     if submission.answers:
34         answers = simplejson.loads(submission.answers)
35         user_exercises = get_user_exercises(request.user)
36         for exercise in exercises:
37             if exercise not in user_exercises:
38                 continue
39             if exercise['type'] == 'open':
40                 answer_field_name = 'exercise_%s' % exercise['id']
41                 mark_field_name = 'markof_%s_by_%s' % (exercise['id'], request.user.id)
42                 fields[answer_field_name] = forms.CharField(
43                     widget = forms.Textarea(attrs={'readonly':True}),
44                     initial = answers[str(exercise['id'])],
45                     label = 'Rozwiązanie zadania %s' % exercise['id']
46                 )
47
48                 fields[mark_field_name] = forms.ChoiceField(
49                     choices = [(None, '-')] + [(i,i) for i in range(exercise['max_points']+1)],
50                     initial = submission.get_mark(user_id = request.user.id, exercise_id = exercise['id']),
51                     label = u'Twoja ocena zadania %s' % exercise['id']
52                 )
53     return type('SubmissionForm', (SubmissionFormBase,), fields)
54
55
56 class SubmissionAdmin(admin.ModelAdmin):
57     readonly_fields = readonly_fields
58
59     def get_form(self, request, obj, **kwargs):
60         return get_form(request, obj)
61     
62     def submitted_by(self, instance):
63         if instance.contact:
64             return '<a href="%s">%s</a>' % (
65                 reverse('admin:contact_contact_change', args = [instance.contact.id]),
66                 instance.contact.contact
67             )
68         return '-'
69     submitted_by.allow_tags = True
70     submitted_by.short_description = "Zgłoszony/a przez"
71
72     def save_model(self, request, submission, form, change):
73         for name, value in form.cleaned_data.items():
74             if name.startswith('markof_'):
75                 parts = name.split('_')
76                 exercise_id = parts[1]
77                 user_id = parts[3]
78                 submission.set_mark(user_id = user_id, exercise_id = exercise_id, mark = value)
79         submission.save()
80
81
82 admin.site.register(Submission, SubmissionAdmin)
83 admin.site.register(Assignment)