forked from arteria/cmsplugin-contact-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcms_plugins.py
executable file
·69 lines (54 loc) · 2.17 KB
/
cms_plugins.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from django.utils.translation import ugettext_lazy as _
from django.conf import settings
from cms.plugin_base import CMSPluginBase
from cms.plugin_pool import plugin_pool
from cmsplugin_contact_plus.admin import ExtraFieldInline
from cmsplugin_contact_plus.models import ContactPlus
from cmsplugin_contact_plus.forms import ContactFormPlus
import time
def handle_uploaded_file(f, ts):
destination = open('%s/%s' % (settings.MEDIA_ROOT, ts + '-' + f.name), 'wb+')
for chunk in f.chunks():
destination.write(chunk)
destination.close()
class CMSContactPlusPlugin(CMSPluginBase):
"""
"""
model = ContactPlus
inlines = [ExtraFieldInline, ]
name = _('Contact Form')
render_template = "cmsplugin_contact_plus/contact.html"
change_form_template = 'cmsplugin_contact_plus/change_form.html'
cache = False
def render(self, context, instance, placeholder):
request = context['request']
if instance and instance.template:
self.render_template = instance.template
if request.method == "POST" and "contact_plus_form_" + str(instance.id) in request.POST.keys():
form = ContactFormPlus(contactFormInstance=instance,
request=request,
data=request.POST,
files=request.FILES)
if form.is_valid():
ts = str(int(time.time()))
for fl in request.FILES:
for f in request.FILES.getlist(fl):
handle_uploaded_file(f, ts)
form.send(instance.recipient_email, request, ts, instance, form.is_multipart)
context.update({
'contact': instance,
})
return context
else:
context.update({
'contact': instance,
'form': form,
})
else:
form = ContactFormPlus(contactFormInstance=instance, request=request)
context.update({
'contact': instance,
'form': form,
})
return context
plugin_pool.register_plugin(CMSContactPlusPlugin)