1 # -*- coding: utf-8 -*-
6 from django.contrib.auth.models import User
7 from django.core.urlresolvers import reverse
8 from django.db import models
9 from django.db.models.signals import post_save
10 from django.dispatch import receiver
11 from django.utils.translation import ugettext_lazy as _
12 from django.utils import timezone
13 from jsonfield import JSONField
15 from contact.models import Contact
18 class Participant(models.Model):
19 contact = models.ForeignKey(Contact, verbose_name=_('contact'), null=True)
20 key = models.CharField(_('key'), max_length=30, unique=True)
21 first_name = models.CharField(_('first name'), max_length=100)
22 last_name = models.CharField(_('last_name'), max_length=100)
23 email = models.EmailField(_('email'), max_length=100, unique=True)
24 key_sent = models.BooleanField(_('key sent'), default=False)
27 verbose_name = _('participant')
28 verbose_name_plural = _('participants')
30 def __unicode__(self):
31 return ', '.join((self.last_name, self.first_name, self.email))
33 # copy-paste from wtem
35 def generate_key(cls):
37 while not key or key in [record['key'] for record in cls.objects.values('key')]:
38 key = ''.join(random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits)
42 # copy-paste from wtem
44 def create(cls, first_name, last_name, email, key=None, contact=None):
45 return cls.objects.create(
47 key=key if key else cls.generate_key(),
48 first_name=first_name,
53 return key == self.key
56 return sum(answer.score() for answer in self.answer_set.all())
59 class Assignment(models.Model):
60 title = models.CharField(_('title'), max_length=128)
61 content = models.TextField(_('content'))
62 deadline = models.DateTimeField(_('deadline'))
63 max_points = models.IntegerField(_('max points'))
64 experts = models.ManyToManyField(User, verbose_name=_('experts'), related_name='stage2_assignments')
65 file_descriptions = JSONField(_('file descriptions'))
68 ordering = ['deadline', 'title']
69 verbose_name = _('assignment')
70 verbose_name_plural = _('assignments')
72 def __unicode__(self):
75 def available_answers(self, expert):
76 return self.answer_set.exclude(mark__expert=expert).exclude(complete=True)
79 return self.deadline >= timezone.now()
82 class Answer(models.Model):
83 participant = models.ForeignKey(Participant)
84 assignment = models.ForeignKey(Assignment)
86 complete = models.BooleanField(default=False)
89 unique_together = ['participant', 'assignment']
91 def update_complete(self):
92 marks = self.mark_set.all()
97 complete = abs(mark1.points - mark2.points) < 0.2 * self.assignment.max_points
100 self.complete = complete
104 marks = self.mark_set.all()
107 return self.mark_set.aggregate(models.Avg('points'))
110 def attachment_path(instance, filename):
111 answer = instance.answer
112 return 'stage2/attachment/%s/%s/%s/%s' % (answer.participant_id, answer.assignment_id, instance.file_no, filename)
115 class Attachment(models.Model):
116 answer = models.ForeignKey(Answer)
117 file_no = models.IntegerField()
118 file = models.FileField(upload_to=attachment_path)
121 ordering = ['file_no']
124 return os.path.basename(self.file.name)
126 def download_url(self):
128 'stage2_participant_file',
129 args=(self.answer.assignment_id, self.file_no,
130 self.answer.participant_id, self.answer.participant.key))
132 def expert_download_url(self):
133 return reverse('stage2_expert_download', args=[self.id])
136 class Mark(models.Model):
137 expert = models.ForeignKey(User)
138 answer = models.ForeignKey(Answer)
139 points = models.DecimalField(verbose_name=_('points'), max_digits=3, decimal_places=1)
142 unique_together = ['expert', 'answer']
145 @receiver(post_save, sender=Mark, dispatch_uid='update_answer')
146 def update_answer(sender, **kwargs):
147 mark = kwargs['instance']
148 mark.answer.update_complete()