Do not assign submissions without answers
[edumed.git] / contact / models.py
1 # -*- coding: utf-8 -*-
2 import yaml
3 from django.core.files.storage import FileSystemStorage
4 from django.db import models
5 from django.utils.encoding import smart_unicode
6 from django.utils.translation import ugettext_lazy as _
7 from jsonfield import JSONField
8 from . import app_settings
9
10
11 class Contact(models.Model):
12     created_at = models.DateTimeField(_('submission date'), auto_now_add=True)
13     ip = models.IPAddressField(_('IP address'))
14     contact = models.CharField(_('contact'), max_length=128)
15     form_tag = models.CharField(_('form'), max_length=32, db_index=True)
16     body = JSONField(_('body'))
17
18     @staticmethod
19     def pretty_print(value, for_html=False):
20         if type(value) in (tuple, list, dict):
21             value = yaml.safe_dump(value, 
22                 allow_unicode=True,
23                 default_flow_style=False)
24             if for_html:
25                 value = smart_unicode(value).replace(u" ", unichr(160))
26         return value
27
28     class Meta:
29         ordering = ('-created_at',)
30         verbose_name = _('submitted form')
31         verbose_name_plural = _('submitted forms')
32
33     def __unicode__(self):
34         return unicode(self.created_at)
35
36
37 class Attachment(models.Model):
38     contact = models.ForeignKey(Contact)
39     tag = models.CharField(max_length=64)
40     file = models.FileField(upload_to='contact/attachment')
41
42     @models.permalink
43     def get_absolute_url(self):
44         return ('contact_attachment', [self.contact_id, self.tag])
45
46
47 __import__(app_settings.FORMS_MODULE)