republish lessons - don't stop on exceptions
[edumed.git] / contact / models.py
1 # -*- coding: utf-8 -*-
2 import yaml
3 from django.db import models
4 from django.utils.encoding import smart_unicode
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, 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)