translations for admin
[edumed.git] / stage2 / models.py
1 # -*- coding: utf-8 -*-
2 import os
3 import random
4 import string
5
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
11
12 from contact.models import Contact
13
14
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)
22
23     class Meta:
24         verbose_name = _('participant')
25         verbose_name_plural = _('participants')
26
27     def __unicode__(self):
28         return ', '.join((self.last_name, self.first_name, self.email))
29
30     # copy-paste from wtem
31     @classmethod
32     def generate_key(cls):
33         key = ''
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)
36                           for i in range(30))
37         return key
38
39     # copy-paste from wtem
40     @classmethod
41     def create(cls, first_name, last_name, email, key=None, contact=None):
42         return cls.objects.create(
43             contact=contact,
44             key=key if key else cls.generate_key(),
45             first_name=first_name,
46             last_name=last_name,
47             email=email)
48
49     def check(self, key):
50         return key == self.key
51
52
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'))
59
60     class Meta:
61         ordering = ['deadline']
62         verbose_name = _('assignment')
63         verbose_name_plural = _('assignments')
64
65     def __unicode__(self):
66         return self.title
67
68
69 class Answer(models.Model):
70     participant = models.ForeignKey(Participant)
71     assignment = models.ForeignKey(Assignment)
72
73     class Meta:
74         unique_together = ['participant', 'assignment']
75
76
77 def attachment_path(instance, filename):
78     answer = instance.answer
79     return 'stage2/attachment/%s/%s/%s' % (answer.participant_id, answer.assignment_id, filename)
80
81
82 class Attachment(models.Model):
83     answer = models.ForeignKey(Answer)
84     file_no = models.IntegerField()
85     file = models.FileField(upload_to=attachment_path)
86
87     def filename(self):
88         return os.path.basename(self.file.name)
89
90     def download_url(self):
91         return reverse(
92             'stage2_participant_file',
93             args=(self.answer.assignment_id, self.file_no,
94                   self.answer.participant_id, self.answer.participant.key))
95
96
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)
101
102     class Meta:
103         unique_together = ['expert', 'answer']