1 # -*- coding: utf-8 -*-
2 from django.contrib.sites.models import Site
3 from django.core.exceptions import ValidationError
4 from django.core.files.uploadedfile import UploadedFile
5 from django.core.mail import send_mail, mail_managers
6 from django.core.validators import validate_email
7 from django import forms
8 from django.template.loader import render_to_string
9 from django.template import RequestContext
10 from django.utils.translation import ugettext_lazy as _
19 class ContactFormMeta(forms.Form.__class__):
20 def __new__(cls, name, bases, attrs):
21 global admin_list_width
22 model = super(ContactFormMeta, cls).__new__(cls, name, bases, attrs)
23 assert model.form_tag not in contact_forms, 'Duplicate form_tag.'
25 admin_list_width = max(admin_list_width, len(model.admin_list))
26 contact_forms[model.form_tag] = model
30 class ContactForm(forms.Form):
31 """Subclass and define some fields."""
32 __metaclass__ = ContactFormMeta
36 form_title = _('Contact form')
37 submit_label = _('Submit')
42 required_css_class = 'required'
43 contact = NotImplemented
44 data_processing = None
46 def save(self, request, formsets=None):
47 from .models import Attachment, Contact
49 for name, value in self.cleaned_data.items():
50 if not isinstance(value, UploadedFile) and name != 'contact':
53 for formset in formsets or []:
54 for f in formset.forms:
56 for name, value in f.cleaned_data.items():
57 if not isinstance(value, UploadedFile):
58 sub_body[name] = value
60 body.setdefault(f.form_tag, []).append(sub_body)
62 contact = Contact.objects.create(
64 ip=request.META['REMOTE_ADDR'],
65 contact=self.cleaned_data['contact'],
66 form_tag=self.form_tag)
67 for name, value in self.cleaned_data.items():
68 if isinstance(value, UploadedFile):
69 attachment = Attachment(contact=contact, tag=name)
70 attachment.file.save(value.name, value)
73 site = Site.objects.get_current()
75 'form_tag': self.form_tag,
76 'site_name': getattr(self, 'site_name', site.name),
77 'site_domain': getattr(self, 'site_domain', site.domain),
80 context = RequestContext(request)
81 mail_managers_subject = render_to_string([
82 'contact/%s/mail_managers_subject.txt' % self.form_tag,
83 'contact/mail_managers_subject.txt',
84 ], dictionary, context).strip()
85 mail_managers_body = render_to_string([
86 'contact/%s/mail_managers_body.txt' % self.form_tag,
87 'contact/mail_managers_body.txt',
88 ], dictionary, context)
89 mail_managers(mail_managers_subject, mail_managers_body, fail_silently=True)
92 validate_email(contact.contact)
93 except ValidationError:
96 mail_subject = render_to_string([
97 'contact/%s/mail_subject.txt' % self.form_tag,
98 'contact/mail_subject.txt',
99 ], dictionary, context).strip()
101 mail_body = render_to_string(
102 'contact/%s/results_email.txt' % contact.form_tag,
105 'results': self.results(contact),
108 mail_body = render_to_string([
109 'contact/%s/mail_body.txt' % self.form_tag,
110 'contact/mail_body.txt',
111 ], dictionary, context)
112 send_mail(mail_subject, mail_body, 'no-reply@%s' % site.domain, [contact.contact], fail_silently=True)
113 if self.mailing_field and self.cleaned_data[self.mailing_field]:
114 email = self.cleaned_data['contact']
115 mailing.subscribe(email)