1 # -*- coding: utf-8 -*-
5 from django.contrib import admin
6 from django import forms
7 from django.utils import simplejson
8 from django.core.urlresolvers import reverse
10 from .models import Submission, Assignment
11 from .middleware import get_current_request
14 f = file(os.path.dirname(__file__) + '/fixtures/exercises.json')
15 exercises = simplejson.loads(f.read())
18 def get_user_exercises(user):
19 assignment = Assignment.objects.get(user = user)
20 return [e for e in exercises if e['id'] in assignment.exercises]
23 readonly_fields = ('submitted_by', 'first_name', 'last_name', 'email', 'key', 'key_sent')
26 class SubmissionFormBase(forms.ModelForm):
29 exclude = ('answers', 'marks', 'contact') + readonly_fields
32 def get_open_answer(answers, exercise):
33 def get_option(options, id):
34 for option in options:
35 if option['id'] == int(id):
38 exercise_id = str(exercise['id'])
39 answer = answers[exercise_id]
40 if exercise['type'] == 'open':
42 if exercise['type'] == 'edumed_wybor':
43 ok = set(map(str, exercise['answer'])) == set(map(str,answer['closed_part']))
44 toret = u'Czesc testowa [%s]:\n' % ('poprawna' if ok else 'niepoprawna')
45 for selected in answer['closed_part']:
46 option = get_option(exercise['options'], selected)
47 toret += '%s: %s\n' % (selected, option['text'])
49 toret += u'<nie wybrano odpowiedzi>\n'
50 toret += u'\nCzesc otwarta (%s):\n\n' % ' '.join(exercise['open_part'])
51 toret += answer['open_part']
56 def get_form(request, submission):
58 if submission.answers:
59 answers = simplejson.loads(submission.answers)
60 user_exercises = get_user_exercises(request.user)
61 for exercise in exercises:
62 if exercise not in user_exercises:
64 if exercise['type'] == 'open' or exercise.get('open_part', None):
65 answer_field_name = 'exercise_%s' % exercise['id']
66 mark_field_name = 'markof_%s_by_%s' % (exercise['id'], request.user.id)
67 fields[answer_field_name] = forms.CharField(
68 widget = forms.Textarea(attrs={'readonly':True}),
69 initial = get_open_answer(answers, exercise),
70 label = 'Rozwiązanie zadania %s' % exercise['id']
73 fields[mark_field_name] = forms.ChoiceField(
74 choices = [(None, '-')] + [(i,i) for i in range(exercise['max_points']+1)],
75 initial = submission.get_mark(user_id = request.user.id, exercise_id = exercise['id']),
76 label = u'Twoja ocena zadania %s' % exercise['id']
78 return type('SubmissionForm', (SubmissionFormBase,), fields)
81 class SubmissionAdmin(admin.ModelAdmin):
82 list_display = ('__unicode__', 'todo',)
83 readonly_fields = readonly_fields
85 def get_form(self, request, obj, **kwargs):
86 return get_form(request, obj)
88 def submitted_by(self, instance):
90 return '<a href="%s">%s</a>' % (
91 reverse('admin:contact_contact_change', args = [instance.contact.id]),
92 instance.contact.contact
95 submitted_by.allow_tags = True
96 submitted_by.short_description = "Zgłoszony/a przez"
98 def todo(self, submission):
99 user = get_current_request().user
100 user_exercises = get_user_exercises(user)
101 user_marks = submission.marks.get(str(user.id), {})
102 return ','.join([str(e['id']) for e in user_exercises if str(e['id']) not in user_marks.keys()])
104 def save_model(self, request, submission, form, change):
105 for name, value in form.cleaned_data.items():
106 if name.startswith('markof_'):
107 parts = name.split('_')
108 exercise_id = parts[1]
110 submission.set_mark(user_id = user_id, exercise_id = exercise_id, mark = value)
114 admin.site.register(Submission, SubmissionAdmin)
115 admin.site.register(Assignment)