+ def __init__(self, data=None, files=None, *args, **kwargs):
+ self.instance = kwargs.pop('instance', None)
+ if self.instance and (data is not None or files is not None):
+ for attachment in self.instance.attachment_set.all():
+ if attachment.tag not in files:
+ files[attachment.tag] = attachment.file
+ super(ContactForm, self).__init__(data, files, *args, **kwargs)
+ if not self.is_bound and self.instance:
+ self.fields['contact'].initial = self.instance.contact
+ self.fields['contact'].widget = forms.HiddenInput()
+ body = self.instance.body
+ for field, value in body.iteritems():
+ if field in self.fields:
+ self.fields[field].initial = value
+ for attachment in self.instance.attachment_set.all():
+ self.fields[attachment.tag].initial = attachment
+
+ def get_dictionary(self, contact):
+ site = Site.objects.get_current()
+ return {
+ 'form_tag': self.form_tag,
+ 'site_name': getattr(self, 'site_name', site.name),
+ 'site_domain': getattr(self, 'site_domain', site.domain),
+ 'contact': contact,
+ }
+
+ @classmethod
+ def is_disabled(cls):
+ # end_time = localtime_to_utc(datetime(*cls.ends_on)) if cls.ends_on else None
+ # expired = end_time and end_time < timezone.now()
+ return cls.disabled # or expired
+
+ def formset_initial(self, prefix):
+ if not self.instance:
+ return None
+ return self.instance.body.get(prefix)
+
+ def get_formsets(self, request=None):
+ request_data = {'data': request.POST, 'files': request.FILES} if request else {}
+ kwargs_instance = dict(request_data)
+ kwargs_instance['instance'] = self.instance
+ formsets = {}
+ for prefix, formset_class in self.form_formsets.iteritems():
+ if getattr(formset_class, 'takes_instance', False):
+ kwargs = kwargs_instance
+ else:
+ kwargs = request_data
+ formsets[prefix] = formset_class(
+ prefix=prefix, initial=self.formset_initial(prefix), **kwargs)
+ return formsets
+