mise-en-production
[auf_framonde.git] / project / cmsplugin_contact_plus / models.py
1 import threading
2
3 from django.conf import settings
4 from django.db import models
5 from django.utils.translation import ugettext_lazy as _
6 from django.contrib.sites.models import Site
7 from django.utils.encoding import python_2_unicode_compatible
8 from django.db.models import Model
9
10 from cms.models import CMSPlugin
11 from inline_ordering.models import Orderable
12 from jsonfield import JSONField
13
14 try:
15 DEFAULT_FROM_EMAIL_ADDRESS = settings.ADMINS[0][1]
16 except:
17 DEFAULT_FROM_EMAIL_ADDRESS = ''
18
19 from . import utils
20
21
22 localdata = threading.local()
23 localdata.TEMPLATE_CHOICES = utils.autodiscover_templates()
24 TEMPLATE_CHOICES = localdata.TEMPLATE_CHOICES
25
26 def get_current_site():
27 try:
28 current_site = Site.objects.get(id=1).name
29 except:
30 current_site = 'example.com'
31 return _('Contact form message from %s') % (current_site)
32 @python_2_unicode_compatible
33 class ContactPlus(CMSPlugin):
34 title = models.CharField(_('Title'),
35 null=True,
36 blank=True,
37 max_length=100,
38 help_text=_("Title for the Contact Form."))
39 email_subject = models.CharField(
40 max_length=256,
41 verbose_name=_("Email subject"),
42 default=get_current_site)
43 recipient_email = models.EmailField(_("Email of recipients"),
44 default=DEFAULT_FROM_EMAIL_ADDRESS)
45 collect_records = models.BooleanField(_('Collect Records'),
46 default=True,
47 help_text=_("If active, all records for this Form will be stored in the Database."))
48 thanks = models.TextField(_('Message displayed after submitting the contact form.'))
49 submit = models.CharField(_('Text for the Submit button.'),
50 blank=True,
51 max_length=30)
52 template = models.CharField(
53 max_length=255,
54 choices=TEMPLATE_CHOICES,
55 default='cmsplugin_contact_plus/contact.html',
56 editable=len(TEMPLATE_CHOICES) > 1)
57
58 class Meta:
59 verbose_name = "Contact Plus Form"
60 verbose_name_plural = "Contact Plus Forms"
61
62 def copy_relations(self, oldinstance):
63 for extrafield in ExtraField.objects.filter(form__pk=oldinstance.pk):
64 extrafield.pk = None
65 extrafield.save()
66 self.extrafield_set.add(
67 extrafield)
68
69 def __str__(self):
70 if self.title:
71 return self.title
72 return _("Contact Plus Form for %s") % self.recipient_email
73
74
75 FIELD_TYPE = (('CharField', 'CharField'),
76 ('BooleanField', 'BooleanField'),
77 ('EmailField', 'EmailField'),
78 ('DecimalField', 'DecimalField'),
79 ('FloatField', 'FloatField'),
80 ('IntegerField', 'IntegerField'),
81 ('FileField', 'FileField'),
82 ('ImageField', 'ImageField'),
83 ('IPAddressField', 'IPAddressField'),
84 ('MathCaptcha','Math Captcha'),
85 ('auto_Textarea', _('CharField as Textarea')),
86 ('auto_hidden_input', _('CharField as HiddenInput')),
87 ('auto_referral_page', _('Referral page as HiddenInput')),
88 ('auto_GET_parameter', _('GET parameter as HiddenInput')))
89
90
91 @python_2_unicode_compatible
92 class ExtraField(Orderable):
93 """
94 """
95 form = models.ForeignKey(ContactPlus, verbose_name=_("Contact Form"))
96 label = models.CharField(_('Label'), max_length=100)
97 fieldType = models.CharField(max_length=100, choices=FIELD_TYPE)
98 initial = models.CharField(
99 _('Inital Value'), max_length=250, blank=True, null=True)
100 required = models.BooleanField(
101 _('Mandatory field'), default=True)
102 widget = models.CharField(
103 _('Widget'), max_length=250, blank=True, null=True,
104 help_text=_("Will be ignored in the current version."))
105
106 def __str__(self):
107 return self.label
108
109
110 @python_2_unicode_compatible
111 class ContactRecord(Model):
112 """
113 """
114 contact_form = models.ForeignKey(ContactPlus, verbose_name=_("Contact Form"), null=True, on_delete=models.SET_NULL)
115 date_of_entry = models.DateTimeField(auto_now_add=True)
116 date_processed = models.DateTimeField(null=True, blank=True, help_text=_("Date the Record was processed."))
117 data = JSONField(null=True, blank=True, default={})
118
119 class Meta():
120 ordering = ['date_of_entry', 'contact_form', ]
121 verbose_name = _("Contact Record")
122 verbose_name_plural = _("Contact Records")
123
124 @property
125 def is_processed(self):
126 if self.date_processed:
127 return True
128 else:
129 return False
130
131 def __str__(self):
132 return _("Record for %(contact)s recorded on %(date)s") % {'contact':self.contact_form,
133 'date': self.date_of_entry.strftime('%d. %b %Y') }
134