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