X-Git-Url: https://git.mdrn.pl/edumed.git/blobdiff_plain/b6836ce0cc2dce392a9f604b781d6430740ce1de..4cba0fdf6957b65fa1e0b91b14d4197464e4e73b:/stage2/models.py?ds=inline diff --git a/stage2/models.py b/stage2/models.py index 972376a..37308fc 100644 --- a/stage2/models.py +++ b/stage2/models.py @@ -8,6 +8,7 @@ from django.core.urlresolvers import reverse from django.db import models from django.db.models.signals import post_save from django.dispatch import receiver +from django.utils.html import format_html from django.utils.translation import ugettext_lazy as _ from django.utils import timezone from jsonfield import JSONField @@ -69,7 +70,8 @@ class Assignment(models.Model): settings.AUTH_USER_MODEL, blank=True, verbose_name=_('arbiters'), related_name='stage2_arbitrated') supervisors = models.ManyToManyField( settings.AUTH_USER_MODEL, verbose_name=_('supervisors'), related_name='stage2_supervised') - file_descriptions = JSONField(_('file descriptions')) + file_descriptions = JSONField(_('file descriptions'), default=[], blank=True) + field_descriptions = JSONField(_('field descriptions'), default=[], blank=True) class Meta: ordering = ['deadline', 'title'] @@ -80,13 +82,35 @@ class Assignment(models.Model): return self.title def available_answers(self, expert): - answers = self.answer_set.exclude(mark__expert=expert).filter(participant__complete_set=True) - if expert not in self.supervisors.all(): + answers = self.answer_set.exclude(mark__expert=expert) + assigned_to_expert = self.answer_set.filter(expert=expert).exists() + is_supervisor = expert in self.supervisors.all() + is_arbiter = expert in self.arbiters.all() + if assigned_to_expert: + expert_answers = answers.filter(expert=expert) + if expert_answers or not is_arbiter: + answers = expert_answers + answers = answers.extra(where=[ + "field_values::text ~ ': *\"[^\"]+\"' " + "OR EXISTS (SELECT id FROM stage2_attachment WHERE answer_id = stage2_answer.id) " + "OR EXISTS (SELECT id FROM stage2_fieldoption WHERE answer_id = stage2_answer.id)"]) + if not is_supervisor: answers = answers.exclude(complete=True) - if expert in self.arbiters.all(): + if not is_supervisor or not self.is_active(): + answers = answers.filter(participant__complete_set=True) + if is_arbiter: answers = answers.filter(need_arbiter=True) return answers + def field_counts(self, answers): + for field_desc in self.field_descriptions: + field_name, params = field_desc + if params['type'] == 'options': + field_count = FieldOption.objects.filter(answer__in=list(answers), set__name=params['option_set']).count() + else: # text, link + field_count = sum(1 for answer in answers if answer.field_values.get(field_name)) + yield field_name, field_count + def is_active(self): return self.deadline >= timezone.now() @@ -94,6 +118,9 @@ class Assignment(models.Model): class Answer(models.Model): participant = models.ForeignKey(Participant) assignment = models.ForeignKey(Assignment) + field_values = JSONField(_('field values'), default={}) + experts = models.ManyToManyField( + settings.AUTH_USER_MODEL, verbose_name=_('experts'), related_name='stage2_assigned_answers') # useful redundancy complete = models.BooleanField(default=False) need_arbiter = models.BooleanField(default=False) @@ -101,6 +128,18 @@ class Answer(models.Model): class Meta: unique_together = ['participant', 'assignment'] + 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'{url}', url=value) + yield field_name, value + def update_complete(self): marks = self.mark_set.all() if len(marks) < 2: @@ -121,7 +160,39 @@ class Answer(models.Model): marks = self.mark_set.all() if len(marks) < 2: return None - return self.mark_set.aggregate(models.Avg('points')) + return self.mark_set.aggregate(avg=models.Avg('points'))['avg'] + + +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 def attachment_path(instance, filename):