1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
6 from hashlib import sha1
7 from django.db import models
8 from django.urls import reverse
9 from django.utils.encoding import smart_text
10 from django.utils.translation import ugettext_lazy as _
11 from . import app_settings
14 class Contact(models.Model):
15 created_at = models.DateTimeField(_('submission date'), auto_now_add=True)
16 ip = models.GenericIPAddressField(_('IP address'))
17 contact = models.EmailField(_('contact'), max_length=128)
18 form_tag = models.CharField(_('form'), max_length=32, db_index=True)
19 body = models.TextField(_('body'))
22 def pretty_print(value, for_html=False):
23 if type(value) in (tuple, list, dict):
24 value = yaml.safe_dump(value, allow_unicode=True, default_flow_style=False)
26 value = smart_text(value).replace(" ", chr(160))
30 ordering = ('-created_at',)
31 verbose_name = _('submitted form')
32 verbose_name_plural = _('submitted forms')
35 return str(self.created_at)
38 serialized_body = ';'.join(sorted('%s:%s' % item for item in self.get_body_json().items()))
39 data = '%s%s%s%s%s' % (self.id, self.contact, serialized_body, self.ip, self.form_tag)
40 return sha1(data).hexdigest()
44 from .views import contact_forms
45 orig_fields = contact_forms[self.form_tag]().fields
48 return list(orig_fields.keys())
51 body = self.get_body_json()
52 return [(key, body[key]) for key in self.keys() if key in body]
54 def get_body_json(self):
55 return json.loads(self.body or '{}')
58 class Attachment(models.Model):
59 contact = models.ForeignKey(Contact, models.CASCADE)
60 tag = models.CharField(max_length=64)
61 file = models.FileField(upload_to='contact/attachment')
63 def get_absolute_url(self):
64 return reverse('contact_attachment', args=[self.contact_id, self.tag])
67 __import__(app_settings.FORMS_MODULE)