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