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.utils.translation import ugettext_lazy as _
 
  10 from jsonfield import JSONField
 
  12 from contact.models import Contact
 
  15 class Participant(models.Model):
 
  16     contact = models.ForeignKey(Contact, verbose_name=_('contact'), null=True)
 
  17     key = models.CharField(_('key'), max_length=30, unique=True)
 
  18     first_name = models.CharField(_('first name'), max_length=100)
 
  19     last_name = models.CharField(_('last_name'), max_length=100)
 
  20     email = models.EmailField(_('email'), max_length=100, unique=True)
 
  21     key_sent = models.BooleanField(_('key sent'), default=False)
 
  24         verbose_name = _('participant')
 
  25         verbose_name_plural = _('participants')
 
  27     def __unicode__(self):
 
  28         return ', '.join((self.last_name, self.first_name, self.email))
 
  30     # copy-paste from wtem
 
  32     def generate_key(cls):
 
  34         while not key or key in [record['key'] for record in cls.objects.values('key')]:
 
  35             key = ''.join(random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits)
 
  39     # copy-paste from wtem
 
  41     def create(cls, first_name, last_name, email, key=None, contact=None):
 
  42         return cls.objects.create(
 
  44             key=key if key else cls.generate_key(),
 
  45             first_name=first_name,
 
  50         return key == self.key
 
  53 class Assignment(models.Model):
 
  54     title = models.CharField(_('title'), max_length=128)
 
  55     content = models.TextField(_('content'))
 
  56     deadline = models.DateTimeField(_('deadline'))
 
  57     max_points = models.IntegerField(_('max points'))
 
  58     file_descriptions = JSONField(_('file descriptions'))
 
  61         ordering = ['deadline']
 
  62         verbose_name = _('assignment')
 
  63         verbose_name_plural = _('assignments')
 
  65     def __unicode__(self):
 
  69 class Answer(models.Model):
 
  70     participant = models.ForeignKey(Participant)
 
  71     assignment = models.ForeignKey(Assignment)
 
  74         unique_together = ['participant', 'assignment']
 
  77 def attachment_path(instance, filename):
 
  78     answer = instance.answer
 
  79     return 'stage2/attachment/%s/%s/%s' % (answer.participant_id, answer.assignment_id, filename)
 
  82 class Attachment(models.Model):
 
  83     answer = models.ForeignKey(Answer)
 
  84     file_no = models.IntegerField()
 
  85     file = models.FileField(upload_to=attachment_path)
 
  88         return os.path.basename(self.file.name)
 
  90     def download_url(self):
 
  92             'stage2_participant_file',
 
  93             args=(self.answer.assignment_id, self.file_no,
 
  94                   self.answer.participant_id, self.answer.participant.key))
 
  97 class Mark(models.Model):
 
  98     expert = models.ForeignKey(User)
 
  99     answer = models.ForeignKey(Answer)
 
 100     points = models.DecimalField(max_digits=3, decimal_places=1)
 
 103         unique_together = ['expert', 'answer']