1 from django.contrib.sites.models import Site
2 from django.core.exceptions import ValidationError
3 from django.core.files.uploadedfile import UploadedFile
4 from django.core.mail import send_mail, mail_managers
5 from django.core.validators import validate_email
6 from django import forms
7 from django.template.loader import render_to_string
8 from django.template import RequestContext
9 from django.utils.translation import ugettext_lazy as _
11 from contact import mailing
12 from .models import Attachment, Contact
17 class ContactFormMeta(forms.Form.__class__):
18 def __new__(cls, *args, **kwargs):
19 global admin_list_width
20 model = super(ContactFormMeta, cls).__new__(cls, *args, **kwargs)
21 assert model.form_tag not in contact_forms, 'Duplicate form_tag.'
23 admin_list_width = max(admin_list_width, len(model.admin_list))
24 contact_forms[model.form_tag] = model
28 class ContactForm(forms.Form):
29 """Subclass and define some fields."""
30 __metaclass__ = ContactFormMeta
35 form_title = _('Contact form')
36 submit_label = _('Submit')
38 notify_on_register = True
42 required_css_class = 'required'
43 contact = forms.EmailField(label=_('E-mail'), max_length=128)
45 def __init__(self, *args, **kwargs):
46 key = kwargs.pop('key', None)
47 super(ContactForm, self).__init__(*args, **kwargs)
50 def save(self, request):
53 for name, value in self.cleaned_data.items():
54 if not isinstance(value, UploadedFile) and name != 'contact' and not name.startswith('_'):
56 save_as_tag = self.save_as_tag or self.form_tag
58 contact = Contact.objects.get(form_tag=save_as_tag, key=key)
60 contact.ip = request.META['REMOTE_ADDR'] or '127.0.0.1'
61 contact.contact = self.cleaned_data['contact']
64 contact = Contact.objects.create(body=body,
65 ip=request.META['REMOTE_ADDR'],
66 contact=self.cleaned_data['contact'],
68 for name, value in self.cleaned_data.items():
69 if isinstance(value, UploadedFile):
70 contact.attachment_set.filter(tag=name).delete()
71 attachment = Attachment(contact=contact, tag=name)
72 attachment.file.save(value.name, value)
75 site = Site.objects.get_current()
77 'form_tag': self.form_tag,
78 'site_name': site.name,
79 'site_domain': site.domain,
83 context = RequestContext(request)
84 if self.notify_on_register:
85 mail_managers_subject = render_to_string([
86 'contact/%s/mail_managers_subject.txt' % self.form_tag,
87 'contact/mail_managers_subject.txt',
88 ], dictionary, context).strip()
89 mail_managers_body = render_to_string([
90 'contact/%s/mail_managers_body.txt' % self.form_tag,
91 'contact/mail_managers_body.txt',
92 ], dictionary, context)
93 mail_managers(mail_managers_subject, mail_managers_body, fail_silently=True)
96 validate_email(contact.contact)
97 except ValidationError:
101 mail_subject = render_to_string([
102 'contact/%s/mail_subject.txt' % self.form_tag,
103 'contact/mail_subject.txt',
104 ], dictionary, context).strip()
105 mail_body = render_to_string([
106 'contact/%s/mail_body.txt' % self.form_tag,
107 'contact/mail_body.txt',
108 ], dictionary, context)
109 send_mail(mail_subject, mail_body,
110 'no-reply@%s' % site.domain,
113 if self.mailing_field and self.cleaned_data[self.mailing_field]:
114 mailing.subscribe(contact.contact)