fix in key generation
[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
19     @classmethod
20     def generate_key(cls):
21         key = ''
22         while not key or key in [record['key'] for record in cls.objects.values('key')]:
23             key = ''.join(random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for i in range(30))
24         return key
25
26     @classmethod
27     def create(cls, first_name, last_name, email, key = None, contact = None):
28         submission = cls(
29             contact = contact,
30             key = key if key else Submission.generate_key(),
31             first_name = first_name,
32             last_name = last_name,
33             email = email
34         )
35
36         submission.save()
37         return submission
38
39
40 class Attachment(models.Model):
41     submission = models.ForeignKey(Submission)
42     name = models.CharField(max_length=100)
43     file = models.FileField(upload_to = 'wtem/attachment')