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