059f9e8dc2140659e18a5df8524313d7a249725c
[edumed.git] / contact / forms.py
1 # -*- coding: utf-8 -*-
2 import os
3
4 from django.contrib.sites.models import Site
5 from django.core.exceptions import ValidationError
6 from django.core.files.uploadedfile import UploadedFile
7 from django.core.mail import send_mail, mail_managers
8 from django.core.validators import validate_email
9 from django import forms
10 from django.db.models.fields.files import FieldFile
11 from django.template.loader import render_to_string
12 from django.template import RequestContext
13 from django.utils.translation import ugettext_lazy as _
14
15 from . import mailing
16
17
18 contact_forms = {}
19 update_forms = {}
20 admin_list_width = 0
21
22
23 class ContactFormMeta(forms.Form.__class__):
24     def __new__(cls, name, bases, attrs):
25         global admin_list_width
26         model = super(ContactFormMeta, cls).__new__(cls, name, bases, attrs)
27         if model.form_tag:
28             if model.form_type == 'create':
29                 assert model.form_tag not in contact_forms, 'Duplicate form_tag.'
30                 if model.admin_list:
31                     admin_list_width = max(admin_list_width, len(model.admin_list))
32                 contact_forms[model.form_tag] = model
33             elif model.form_type == 'update':
34                 assert model.form_tag not in update_forms, 'Duplicate form_tag.'
35                 update_forms[model.form_tag] = model
36         return model
37
38
39 class ContactForm(forms.Form):
40     """Subclass and define some fields."""
41     __metaclass__ = ContactFormMeta
42
43     form_tag = None
44     form_type = 'create'
45     updatable = False
46     old_form_tags = []
47     form_title = _('Contact form')
48     submit_label = _('Submit')
49     admin_list = None
50     result_page = False
51     mailing_field = None
52     mailing = False
53     data_processing = None
54     form_formsets = {}
55
56     disabled = False
57     disabled_template = None
58
59     required_css_class = 'required'
60     contact = NotImplemented
61
62     def __init__(self, data=None, files=None, *args, **kwargs):
63         self.instance = kwargs.pop('instance', None)
64         if self.instance and (data is not None or files is not None):
65             for attachment in self.instance.attachment_set.all():
66                 if attachment.tag not in files:
67                     files[attachment.tag] = attachment.file
68         super(ContactForm, self).__init__(data, files, *args, **kwargs)
69         if not self.is_bound and self.instance:
70             self.fields['contact'].initial = self.instance.contact
71             self.fields['contact'].widget = forms.HiddenInput()
72             body = self.instance.body
73             for field, value in body.iteritems():
74                 if field in self.fields:
75                     self.fields[field].initial = value
76             for attachment in self.instance.attachment_set.all():
77                 self.fields[attachment.tag].initial = attachment
78
79     def get_dictionary(self, contact):
80         site = Site.objects.get_current()
81         return {
82             'form_tag': self.form_tag,
83             'site_name': getattr(self, 'site_name', site.name),
84             'site_domain': getattr(self, 'site_domain', site.domain),
85             'contact': contact,
86         }
87
88     @classmethod
89     def is_disabled(cls):
90         # end_time = localtime_to_utc(datetime(*cls.ends_on)) if cls.ends_on else None
91         # expired = end_time and end_time < timezone.now()
92         return cls.disabled  # or expired
93
94     def formset_initial(self, prefix):
95         if not self.instance:
96             return None
97         return self.instance.body.get(prefix)
98
99     def get_formsets(self, request=None):
100         request_data = {'data': request.POST, 'files': request.FILES} if request else {}
101         kwargs_instance = dict(request_data)
102         kwargs_instance['instance'] = self.instance
103         formsets = {}
104         for prefix, formset_class in self.form_formsets.iteritems():
105             if getattr(formset_class, 'takes_instance', False):
106                 kwargs = kwargs_instance
107             else:
108                 kwargs = request_data
109             formsets[prefix] = formset_class(
110                 prefix=prefix, initial=self.formset_initial(prefix), **kwargs)
111         return formsets
112
113     def save(self, request, formsets=None):
114         from .models import Attachment, Contact
115         body = {}
116         for name, value in self.cleaned_data.items():
117             if not isinstance(value, UploadedFile) and not isinstance(value, FieldFile) and name != 'contact':
118                 body[name] = value
119
120         for formset in formsets or []:
121             for f in formset.forms:
122                 sub_body = {}
123                 for name, value in f.cleaned_data.items():
124                     if not isinstance(value, UploadedFile):
125                         sub_body[name] = value
126                 if sub_body:
127                     body.setdefault(f.form_tag, []).append(sub_body)
128
129         if self.instance:
130             contact = self.instance
131             contact.body = body
132             email_changed = contact.contact != self.cleaned_data['contact']
133             contact.contact = self.cleaned_data['contact']
134             assert contact.form_tag == self.form_tag
135             contact.save()
136         else:
137             contact = Contact.objects.create(
138                 body=body,
139                 ip=request.META['REMOTE_ADDR'],
140                 contact=self.cleaned_data['contact'],
141                 form_tag=self.form_tag)
142             contact.generate_key()
143             email_changed = True
144         for name, value in self.cleaned_data.items():
145             if isinstance(value, UploadedFile):
146                 for attachment in Attachment.objects.filter(contact=contact, tag=name):
147                     os.remove(attachment.file.path)
148                     attachment.delete()
149                 attachment = Attachment(contact=contact, tag=name)
150                 attachment.file.save(value.name, value)
151                 attachment.save()
152
153         site = Site.objects.get_current()
154         dictionary = self.get_dictionary(contact)
155         context = RequestContext(request)
156         mail_managers_subject = render_to_string([
157                 'contact/%s/mail_managers_subject.txt' % self.form_tag,
158                 'contact/mail_managers_subject.txt', 
159             ], dictionary, context).strip()
160         mail_managers_body = render_to_string([
161                 'contact/%s/mail_managers_body.txt' % self.form_tag,
162                 'contact/mail_managers_body.txt', 
163             ], dictionary, context)
164         mail_managers(mail_managers_subject, mail_managers_body, fail_silently=True)
165
166         try:
167             validate_email(contact.contact)
168         except ValidationError:
169             pass
170         else:
171             if not self.instance:
172                 mail_subject = render_to_string([
173                         'contact/%s/mail_subject.txt' % self.form_tag,
174                         'contact/mail_subject.txt',
175                     ], dictionary, context).strip()
176                 if self.result_page:
177                     mail_body = render_to_string(
178                         'contact/%s/results_email.txt' % contact.form_tag,
179                         {
180                             'contact': contact,
181                             'results': self.results(contact),
182                         }, context)
183                 else:
184                     mail_body = render_to_string([
185                             'contact/%s/mail_body.txt' % self.form_tag,
186                             'contact/mail_body.txt',
187                         ], dictionary, context)
188                 send_mail(mail_subject, mail_body, 'no-reply@%s' % site.domain, [contact.contact], fail_silently=True)
189             if email_changed and (self.mailing or (self.mailing_field and self.cleaned_data[self.mailing_field])):
190                 mailing.subscribe(contact.contact)
191
192         return contact