Sending keys wip: up to point of actually sending an email
[edumed.git] / wtem / models.py
1 import random
2 import string
3
4 from django.db import models
5
6 from contact.models import Contact
7
8
9 DEBUG_KEY = '12345'
10
11 class Submission(models.Model):
12     contact = models.ForeignKey(Contact, null = True)
13     key = models.CharField(max_length = 30, unique = True)
14     first_name = models.CharField(max_length = 100)
15     last_name = models.CharField(max_length = 100)
16     email = models.EmailField(max_length = 100, unique = True)
17     answers = models.CharField(max_length = 65536, null = True, blank = True)
18     key_sent = models.BooleanField(default = False)
19
20     @classmethod
21     def generate_key(cls):
22         key = ''
23         while not key or key in [record['key'] for record in cls.objects.values('key')]:
24             key = ''.join(random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for i in range(30))
25         return key
26
27     @classmethod
28     def create(cls, first_name, last_name, email, key = None, contact = None):
29         submission = cls(
30             contact = contact,
31             key = key if key else Submission.generate_key(),
32             first_name = first_name,
33             last_name = last_name,
34             email = email
35         )
36
37         submission.save()
38         return submission
39
40
41 class Attachment(models.Model):
42     submission = models.ForeignKey(Submission)
43     name = models.CharField(max_length=100)
44     file = models.FileField(upload_to = 'wtem/attachment')