cc60c73e26d9505d9555179e8a74f4441b6378c6
[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             body = self.instance.body
72             for field, value in body.iteritems():
73                 if field in self.fields:
74                     self.fields[field].initial = value
75             for attachment in self.instance.attachment_set.all():
76                 self.fields[attachment.tag].initial = attachment
77
78     def get_dictionary(self, contact):
79         site = Site.objects.get_current()
80         return {
81             'form_tag': self.form_tag,
82             'site_name': getattr(self, 'site_name', site.name),
83             'site_domain': getattr(self, 'site_domain', site.domain),
84             'contact': contact,
85         }
86
87     @classmethod
88     def is_disabled(cls):
89         # end_time = localtime_to_utc(datetime(*cls.ends_on)) if cls.ends_on else None
90         # expired = end_time and end_time < timezone.now()
91         return cls.disabled  # or expired
92
93     def formset_initial(self, prefix):
94         if not self.instance:
95             return None
96         return self.instance.body.get(prefix)
97
98     def get_formsets(self, request=None):
99         request_data = {'data': request.POST, 'files': request.FILES} if request else {}
100         kwargs_instance = dict(request_data)
101         kwargs_instance['instance'] = self.instance
102         formsets = {}
103         for prefix, formset_class in self.form_formsets.iteritems():
104             if getattr(formset_class, 'takes_instance', False):
105                 kwargs = kwargs_instance
106             else:
107                 kwargs = request_data
108             formsets[prefix] = formset_class(
109                 prefix=prefix, initial=self.formset_initial(prefix), **kwargs)
110         return formsets
111
112     def save(self, request, formsets=None):
113         from .models import Attachment, Contact
114         body = {}
115         for name, value in self.cleaned_data.items():
116             if not isinstance(value, UploadedFile) and not isinstance(value, FieldFile) and name != 'contact':
117                 body[name] = value
118
119         for formset in formsets or []:
120             for f in formset.forms:
121                 sub_body = {}
122                 for name, value in f.cleaned_data.items():
123                     if not isinstance(value, UploadedFile):
124                         sub_body[name] = value
125                 if sub_body:
126                     body.setdefault(f.form_tag, []).append(sub_body)
127
128         if self.instance:
129             contact = self.instance
130             contact.body = body
131             email_changed = contact.contact != self.cleaned_data['contact']
132             contact.contact = self.cleaned_data['contact']
133             assert contact.form_tag == self.form_tag
134             contact.save()
135         else:
136             contact = Contact.objects.create(
137                 body=body,
138                 ip=request.META['REMOTE_ADDR'],
139                 contact=self.cleaned_data['contact'],
140                 form_tag=self.form_tag)
141             contact.generate_key()
142             contact.save()
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