mise-en-production
[auf_framonde.git] / project / cmsplugin_contact_plus / forms.py
1 from django import forms
2 from django.core.mail import EmailMessage
3 from django.template.loader import render_to_string
4 from django.contrib.sites.models import Site
5 from django.template.defaultfilters import slugify
6 from django.conf import settings
7 from django.utils.translation import ugettext_lazy as _
8
9 from simplemathcaptcha.fields import MathCaptchaField
10 from .models import ContactPlus, ContactRecord
11 from .signals import contact_message_sent
12
13
14 class ContactFormPlus(forms.Form):
15
16 def __init__(self, contactFormInstance, request, *args, **kwargs):
17 super(ContactFormPlus, self).__init__(*args, **kwargs)
18 if 'instance' not in kwargs:
19 for extraField in contactFormInstance.extrafield_set.all():
20 if extraField.fieldType == 'CharField':
21 self.fields[slugify(extraField.label)] = forms.CharField(label=extraField.label,
22 initial=extraField.initial,
23 required=extraField.required)
24 elif extraField.fieldType == 'BooleanField':
25 self.fields[slugify(extraField.label)] = forms.BooleanField(label=extraField.label,
26 initial=extraField.initial,
27 required=extraField.required)
28 elif extraField.fieldType == 'EmailField':
29 self.fields[slugify(extraField.label)] = forms.EmailField(label=extraField.label,
30 initial=extraField.initial,
31 required=extraField.required)
32 elif extraField.fieldType == 'DecimalField':
33 self.fields[slugify(extraField.label)] = forms.DecimalField(label=extraField.label,
34 initial=extraField.initial,
35 required=extraField.required)
36 elif extraField.fieldType == 'FloatField':
37 self.fields[slugify(extraField.label)] = forms.FloatField(label=extraField.label,
38 initial=extraField.initial,
39 required=extraField.required)
40 elif extraField.fieldType == 'FileField':
41 self.fields[slugify(extraField.label)] = forms.FileField(label=extraField.label,
42 initial=extraField.initial,
43 required=extraField.required)
44 elif extraField.fieldType == 'ImageField':
45 self.fields[slugify(extraField.label)] = forms.ImageField(label=extraField.label,
46 initial=extraField.initial,
47 required=extraField.required)
48 elif extraField.fieldType == 'IntegerField':
49 self.fields[slugify(extraField.label)] = forms.IntegerField(label=extraField.label,
50 initial=extraField.initial,
51 required=extraField.required)
52 elif extraField.fieldType == 'IPAddressField':
53 self.fields[slugify(extraField.label)] = forms.IPAddressField(label=extraField.label,
54 initial=extraField.initial,
55 required=extraField.required)
56 elif extraField.fieldType == 'auto_Textarea':
57 self.fields[slugify(extraField.label)] = forms.CharField(label=extraField.label,
58 initial=extraField.initial,
59 widget=forms.Textarea,
60 required=extraField.required)
61 elif extraField.fieldType == 'auto_hidden_input':
62 self.fields[slugify(extraField.label)] = forms.CharField(label=extraField.label,
63 initial=extraField.initial,
64 widget=forms.HiddenInput,
65 required=False)
66 elif extraField.fieldType == 'auto_referral_page':
67 lInitial = _("No referral available.")
68 if request:
69 lInitial = request.META.get('HTTP_REFERER', _('No referral available.'))
70 self.fields[slugify(extraField.label)] = forms.CharField(label=extraField.label,
71 initial=lInitial, # NOTE: This overwrites extraField.initial!
72 widget=forms.HiddenInput,
73 required=False)
74 elif extraField.fieldType == 'MathCaptcha':
75 self.fields[slugify(extraField.label)] = MathCaptchaField(
76 label=extraField.label,
77 initial=extraField.initial,
78 required=True)
79 elif extraField.fieldType == 'auto_GET_parameter':
80 lInitial = _("Key/value parameter not available.")
81 if request:
82 lInitial = request.GET.get(slugify(extraField.label), 'n/a')
83 self.fields[slugify(extraField.label)] = forms.CharField(label=extraField.label,
84 initial=lInitial, # NOTE: This overwrites extraField.initial!
85 widget=forms.HiddenInput,
86 required=False)
87
88 def send(self, recipient_email, request, ts, instance=None, multipart=False):
89 current_site = Site.objects.get_current()
90 if instance:
91 order = ContactPlus.objects.get(id=instance.id).extrafield_set.order_by('inline_ordering_position')
92 ordered_dic_list = []
93 for field in order:
94 key = slugify(field.label)
95 value = self.cleaned_data.get(key, '(no input)')
96 # redefine value for files...
97 if field.fieldType in ["FileField", "ImageField"]:
98 val = ts + '-' + str(value)
99 if settings.MEDIA_URL.startswith("http"):
100 value = "%s%s" % (settings.MEDIA_URL, val)
101 else:
102 value = "http://%s%s%s" % (current_site, settings.MEDIA_URL, val)
103 ordered_dic_list.append({field.label: value})
104
105 # Automatically match reply-to email adress in form
106 tmp_headers = {}
107 try:
108 reply_email_label = getattr(settings, 'CONTACT_PLUS_REPLY_EMAIL_LABEL', None)
109 if reply_email_label is not None:
110 tmp_headers.update({'Reply-To': self.cleaned_data[reply_email_label]})
111 except:
112 pass
113
114 email_message = EmailMessage(
115 "[" + current_site.domain.upper() + "]",
116 render_to_string("cmsplugin_contact_plus/email.txt", {'data': self.cleaned_data,
117 'ordered_data': ordered_dic_list,
118 'instance': instance,
119 }),
120 from_email=getattr(settings, 'DEFAULT_FROM_EMAIL', None),
121 to=[recipient_email, ],
122 headers=tmp_headers,)
123 email_message.send(fail_silently=True)
124
125 if instance.collect_records:# and not multipart:
126 record = ContactRecord(contact_form=instance, data=ordered_dic_list)#self.cleaned_data)
127 record.save()
128
129 contact_message_sent.send(sender=self, data=self.cleaned_data)