Django 1.6
[prawokultury.git] / contact / forms.py
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 _
10 from .models import Attachment, Contact
11
12
13 contact_forms = {}
14 admin_list_width = 0
15 class ContactFormMeta(forms.Form.__class__):
16     def __new__(cls, *args, **kwargs):
17         global admin_list_width
18         model = super(ContactFormMeta, cls).__new__(cls, *args, **kwargs)
19         assert model.form_tag not in contact_forms, 'Duplicate form_tag.'
20         if model.admin_list:
21             admin_list_width = max(admin_list_width, len(model.admin_list))
22         contact_forms[model.form_tag] = model
23         return model
24
25
26 class ContactForm(forms.Form):
27     """Subclass and define some fields."""
28     __metaclass__ = ContactFormMeta
29
30     form_tag = None
31     form_title = _('Contact form')
32     submit_label = _('Submit')
33     admin_list = None
34
35     required_css_class = 'required'
36     contact = forms.CharField(max_length=128)
37
38     def save(self, request):
39         body = {}
40         for name, value in self.cleaned_data.items():
41             if not isinstance(value, UploadedFile) and name != 'contact':
42                     body[name] = value
43         contact = Contact.objects.create(body=body,
44                     ip=request.META['REMOTE_ADDR'],
45                     contact=self.cleaned_data['contact'],
46                     form_tag=self.form_tag)
47         for name, value in self.cleaned_data.items():
48             if isinstance(value, UploadedFile):
49                 attachment = Attachment(contact=contact, tag=name)
50                 attachment.file.save(value.name, value)
51                 attachment.save()
52
53         site = Site.objects.get_current()
54         dictionary = {
55             'form_tag': self.form_tag,
56             'site_name': site.name,
57             'site_domain': site.domain,
58             'contact': contact,
59         }
60         context = RequestContext(request)
61         mail_managers_subject = render_to_string([
62                 'contact/%s/mail_managers_subject.txt' % self.form_tag,
63                 'contact/mail_managers_subject.txt', 
64             ], dictionary, context)
65         mail_managers_body = render_to_string([
66                 'contact/%s/mail_managers_body.txt' % self.form_tag,
67                 'contact/mail_managers_body.txt', 
68             ], dictionary, context)
69         mail_managers(mail_managers_subject, mail_managers_body, 
70             fail_silently=True)
71
72         try:
73             validate_email(contact.contact)
74         except ValidationError:
75             pass
76         else:
77             mail_subject = render_to_string([
78                     'contact/%s/mail_subject.txt' % self.form_tag,
79                     'contact/mail_subject.txt', 
80                 ], dictionary, context)
81             mail_body = render_to_string([
82                     'contact/%s/mail_body.txt' % self.form_tag,
83                     'contact/mail_body.txt', 
84                 ], dictionary, context)
85             send_mail(mail_subject, mail_body,
86                 'no-reply@%s' % site.domain,
87                 [contact.contact],
88                 fail_silently=True)
89
90         return contact