update emails for stage2
[edumed.git] / stage2 / models.py
index dc7ac63..c21c53a 100644 (file)
@@ -3,7 +3,7 @@ import os
 import random
 import string
 
 import random
 import string
 
-from django.contrib.auth.models import User
+from django.conf import settings
 from django.core.urlresolvers import reverse
 from django.db import models
 from django.db.models.signals import post_save
 from django.core.urlresolvers import reverse
 from django.db import models
 from django.db.models.signals import post_save
@@ -22,6 +22,7 @@ class Participant(models.Model):
     last_name = models.CharField(_('last_name'), max_length=100)
     email = models.EmailField(_('email'), max_length=100, unique=True)
     key_sent = models.BooleanField(_('key sent'), default=False)
     last_name = models.CharField(_('last_name'), max_length=100)
     email = models.EmailField(_('email'), max_length=100, unique=True)
     key_sent = models.BooleanField(_('key sent'), default=False)
+    complete_set = models.BooleanField(default=False)
 
     class Meta:
         verbose_name = _('participant')
 
     class Meta:
         verbose_name = _('participant')
@@ -62,10 +63,14 @@ class Assignment(models.Model):
     content_url = models.URLField(_('URL'))
     deadline = models.DateTimeField(_('deadline'))
     max_points = models.IntegerField(_('max points'))
     content_url = models.URLField(_('URL'))
     deadline = models.DateTimeField(_('deadline'))
     max_points = models.IntegerField(_('max points'))
-    experts = models.ManyToManyField(User, verbose_name=_('experts'), related_name='stage2_assignments')
+    experts = models.ManyToManyField(
+        settings.AUTH_USER_MODEL, verbose_name=_('experts'), related_name='stage2_assignments')
     arbiters = models.ManyToManyField(
     arbiters = models.ManyToManyField(
-        User, blank=True, verbose_name=_('arbiters'), related_name='stage2_arbitrated')
-    file_descriptions = JSONField(_('file descriptions'))
+        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'), default=[], blank=True)
+    field_descriptions = JSONField(_('field descriptions'), default=[], blank=True)
 
     class Meta:
         ordering = ['deadline', 'title']
 
     class Meta:
         ordering = ['deadline', 'title']
@@ -76,7 +81,9 @@ class Assignment(models.Model):
         return self.title
 
     def available_answers(self, expert):
         return self.title
 
     def available_answers(self, expert):
-        answers = self.answer_set.exclude(mark__expert=expert).exclude(complete=True)
+        answers = self.answer_set.exclude(mark__expert=expert).filter(participant__complete_set=True)
+        if expert not in self.supervisors.all():
+            answers = answers.exclude(complete=True)
         if expert in self.arbiters.all():
             answers = answers.filter(need_arbiter=True)
         return answers
         if expert in self.arbiters.all():
             answers = answers.filter(need_arbiter=True)
         return answers
@@ -88,6 +95,7 @@ class Assignment(models.Model):
 class Answer(models.Model):
     participant = models.ForeignKey(Participant)
     assignment = models.ForeignKey(Assignment)
 class Answer(models.Model):
     participant = models.ForeignKey(Participant)
     assignment = models.ForeignKey(Assignment)
+    field_values = JSONField(_('field values'), default={})
     # useful redundancy
     complete = models.BooleanField(default=False)
     need_arbiter = models.BooleanField(default=False)
     # useful redundancy
     complete = models.BooleanField(default=False)
     need_arbiter = models.BooleanField(default=False)
@@ -115,7 +123,39 @@ class Answer(models.Model):
         marks = self.mark_set.all()
         if len(marks) < 2:
             return None
         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):
 
 
 def attachment_path(instance, filename):
@@ -145,7 +185,7 @@ class Attachment(models.Model):
 
 
 class Mark(models.Model):
 
 
 class Mark(models.Model):
-    expert = models.ForeignKey(User)
+    expert = models.ForeignKey(settings.AUTH_USER_MODEL)
     answer = models.ForeignKey(Answer)
     points = models.DecimalField(verbose_name=_('points'), max_digits=3, decimal_places=1)
 
     answer = models.ForeignKey(Answer)
     points = models.DecimalField(verbose_name=_('points'), max_digits=3, decimal_places=1)