+ def fields(self):
+ for field_desc in self.assignment.field_descriptions:
+ field_name, params = field_desc
+ if params['type'] == 'options':
+ option = self.fieldoption_set.filter(set__name=params['option_set'])
+ value = option.get().value if option else '--------'
+ else: # text, link
+ value = self.field_values.get(field_name, '')
+ if params['type'] == 'link':
+ value = format_html(u'<a href="{url}">{url}</a>', url=value)
+ yield field_name, value
+
+ def total_points(self):
+ criterion_count = self.assignment.markcriterion_set.count()
+ for expert_id in self.mark_set.values_list('expert_id', flat=True).distinct():
+ marks = self.mark_set.filter(expert_id=expert_id)
+ if len(marks) == criterion_count:
+ yield sum(mark.points for mark in marks)
+
+ def update_complete(self):
+ total_points = list(self.total_points())
+ if len(total_points) < 2:
+ complete = False
+ need_arbiter = False
+ elif len(total_points) == 2:
+ points1, points2 = total_points
+ complete = abs(points1 - points2) < 0.2 * self.assignment.max_points
+ need_arbiter = not complete
+ else:
+ complete = True
+ need_arbiter = False
+ self.complete = complete
+ self.need_arbiter = need_arbiter
+ self.save()
+
+ def score(self):
+ total_marks = list(self.total_points())
+ if len(total_marks) < 2:
+ return None
+ return sum(total_marks) / len(total_marks)
+
+ # unrelated to `complete' attribute, but whatever
+ def is_complete(self):
+ file_count = len(self.assignment.file_descriptions)
+ field_count = len(self.assignment.field_descriptions)
+ if self.attachment_set.count() < file_count:
+ return False
+ if self.fieldoption_set.count() + sum(1 for k, v in self.field_values.iteritems() if v) < field_count:
+ return False
+ return True
+
+
+class FieldOptionSet(models.Model):
+ name = models.CharField(verbose_name=_('nazwa'), max_length=32, db_index=True)
+
+ class Meta:
+ verbose_name = _('option set')
+ verbose_name_plural = _('option sets')
+
+ def __unicode__(self):
+ return self.name
+
+ def choices(self, answer):
+ return [('', '--------')] + [
+ (option.id, option.value)
+ for option in self.fieldoption_set.extra(
+ where=['answer_id is null or answer_id = %s'],
+ params=[answer.id])]
+
+
+class FieldOption(models.Model):
+ set = models.ForeignKey(FieldOptionSet, verbose_name=_('zestaw'))
+ value = models.CharField(verbose_name=_('value'), max_length=255)
+ answer = models.ForeignKey(Answer, verbose_name=_('answer'), null=True, blank=True)
+
+ class Meta:
+ ordering = ['set', 'value']
+ verbose_name = _('option')
+ verbose_name_plural = _('options')
+
+ def __unicode__(self):
+ return self.value
+