5 from django.db import models
6 from django.contrib.auth.models import User
7 from django.core.exceptions import ValidationError
8 from django.utils import simplejson
9 from django.utils.translation import ugettext as _
10 from jsonfield import JSONField
12 from contact.models import Contact
14 f = file(os.path.dirname(__file__) + '/fixtures/exercises.json')
15 exercises = simplejson.loads(f.read())
20 class Submission(models.Model):
21 contact = models.ForeignKey(Contact, null = True)
22 key = models.CharField(max_length = 30, unique = True)
23 first_name = models.CharField(max_length = 100)
24 last_name = models.CharField(max_length = 100)
25 email = models.EmailField(max_length = 100, unique = True)
26 answers = models.CharField(max_length = 65536, null = True, blank = True)
27 key_sent = models.BooleanField(default = False)
29 examiners = models.ManyToManyField(User, null = True, blank = True)
30 end_time = models.CharField(max_length = 5, null = True, blank = True)
32 def __unicode__(self):
33 return ', '.join((self.last_name, self.first_name, self.email))
36 def generate_key(cls):
38 while not key or key in [record['key'] for record in cls.objects.values('key')]:
39 key = ''.join(random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for i in range(30))
43 def create(cls, first_name, last_name, email, key = None, contact = None):
46 key = key if key else Submission.generate_key(),
47 first_name = first_name,
48 last_name = last_name,
55 def get_mark(self, user_id, exercise_id):
57 user_id = str(user_id)
58 exercise_id = str(exercise_id)
59 if self.marks and user_id in self.marks:
60 mark = self.marks[user_id].get(exercise_id, None)
63 def set_mark(self, user_id, exercise_id, mark):
64 user_id = str(user_id)
65 exercise_id = str(exercise_id)
69 self.marks.setdefault(user_id, {})[exercise_id] = mark
71 del self.marks[user_id][exercise_id]
74 def get_exercise_marks_by_examiner(self, exercise_id):
76 for examiner_id, examiner_marks in self.marks.items():
77 mark = examiner_marks.get(exercise_id, None)
79 marks[examiner_id] = mark
82 def get_final_exercise_mark(self, exercise_id):
83 exercise = exercises[int(exercise_id)-1]
84 if exercise_checked_manually(exercise):
85 marks_by_examiner = self.get_exercise_marks_by_examiner(exercise_id)
86 if len(marks_by_examiner):
87 return sum(map(int, marks_by_examiner.values())) / float(len(marks_by_examiner))
93 answer = simplejson.loads(self.answers)[exercise_id]['closed_part']
95 if t == 'edumed_uporzadkuj':
96 return exercise['points'] if map(int, answer) == exercise['answer'] else 0
97 if t == 'edumed_przyporzadkuj':
99 for bucket_id, items in answer.items():
100 for item_id in items:
102 if exercise.get('answer_mode', None) == 'possible_buckets_for_item':
103 is_correct = int(bucket_id) in exercise['answer'].get(item_id)
105 is_correct = int(item_id) in exercise['answer'].get(bucket_id, [])
107 toret += exercise['points_per_hit']
109 if t == 'edumed_wybor':
110 if len(exercise['answer']) == 1:
111 if len(answer) and int(answer[0]) == exercise['answer'][0]:
112 return exercise['points']
117 if exercise.get('answer_mode', None) == 'all_or_nothing':
118 toret = exercise['points'] if map(int, answer) == exercise['answer'] else 0
120 for id in map(int, answer):
121 if id in exercise['answer']:
122 toret += exercise['points_per_hit']
124 if t == 'edumed_prawdafalsz':
126 for idx, statement in enumerate(exercise['statements']):
127 if answer[idx] == 'true':
129 elif answer[idx] == 'false':
133 if given == statement[1]:
134 toret += exercise['points_per_hit']
136 raise NotImplementedError
139 def final_result(self):
141 for exercise_id in map(str,range(1, len(exercises) + 1)):
142 mark = self.get_final_exercise_mark(exercise_id)
148 def final_result_as_string(self):
149 return ('%.2f' % self.final_result).rstrip('0').rstrip('.')
151 class Attachment(models.Model):
152 submission = models.ForeignKey(Submission)
153 exercise_id = models.IntegerField()
154 file = models.FileField(upload_to = 'wtem/attachment')
157 class Assignment(models.Model):
158 user = models.ForeignKey(User, unique = True)
159 exercises = JSONField()
162 if not isinstance(self.exercises, list):
163 raise ValidationError(_('Assigned exercises must be declared in a list format'))
164 for exercise in self.exercises:
165 if not isinstance(exercise, int) or exercise < 1:
166 raise ValidationError(_('Invalid exercise id: %s' % exercise))
168 def __unicode__(self):
169 return self.user.username + ': ' + ','.join(map(str,self.exercises))
172 def exercise_checked_manually(exercise):
173 return (exercise['type'] in ('open', 'file_upload')) or 'open_part' in exercise