wtem: generating submissions from contact forms
[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 class Submission(models.Model):
10     contact = models.ForeignKey(Contact, null = True)
11     key = models.CharField(max_length = 30, unique = True)
12     first_name = models.CharField(max_length = 100)
13     last_name = models.CharField(max_length = 100)
14     email = models.EmailField(max_length = 100, unique = True)
15     answers = models.CharField(max_length = 65536, null = True, blank = True)
16
17     @classmethod
18     def generate_key(cls):
19         key = ''
20         while not key and key in [record['key'] for record in cls.objects.values('key')]:
21             key = ''.join(random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for i in range(30))
22         return key
23
24     @classmethod
25     def create(cls, first_name, last_name, email, key = None, contact = None):
26         submission = cls(
27             contact = contact,
28             key = key if key else Submission.generate_key(),
29             first_name = first_name,
30             last_name = last_name,
31             email = email
32         )
33
34         submission.save()
35         return submission
36
37
38 class Attachment(models.Model):
39     submission = models.ForeignKey(Submission)
40     name = models.CharField(max_length=100)
41     file = models.FileField(upload_to = 'wtem/attachment')