contact_forms = {}
+update_forms = {}
admin_list_width = 0
global admin_list_width
model = super(ContactFormMeta, cls).__new__(cls, name, bases, attrs)
if model.form_tag:
- assert model.form_tag not in contact_forms, 'Duplicate form_tag.'
- if model.admin_list:
- admin_list_width = max(admin_list_width, len(model.admin_list))
- contact_forms[model.form_tag] = model
+ if model.form_type == 'create':
+ assert model.form_tag not in contact_forms, 'Duplicate form_tag.'
+ if model.admin_list:
+ admin_list_width = max(admin_list_width, len(model.admin_list))
+ contact_forms[model.form_tag] = model
+ elif model.form_type == 'update':
+ assert model.form_tag not in update_forms, 'Duplicate form_tag.'
+ update_forms[model.form_tag] = model
return model
__metaclass__ = ContactFormMeta
form_tag = None
+ form_type = 'create'
old_form_tags = []
form_title = _('Contact form')
submit_label = _('Submit')
required_css_class = 'required'
contact = NotImplemented
+ def __init__(self, *args, **kwargs):
+ self.instance = kwargs.pop('instance', None)
+ super(ContactForm, self).__init__(*args, **kwargs)
+ if not self.is_bound and self.instance:
+ # files are omitted (not necessary for now)
+ self.fields['contact'].initial = self.instance.contact
+ body = self.instance.body
+ for field, value in body.iteritems():
+ if field in self.fields:
+ self.fields[field].initial = value
+
+ 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,
+ }
+
def save(self, request, formsets=None):
from .models import Attachment, Contact
body = {}
if sub_body:
body.setdefault(f.form_tag, []).append(sub_body)
- contact = Contact.objects.create(
- body=body,
- ip=request.META['REMOTE_ADDR'],
- contact=self.cleaned_data['contact'],
- form_tag=self.form_tag)
+ if self.instance:
+ contact = self.instance
+ contact.body = body
+ email_changed = contact.contact != self.cleaned_data['contact']
+ contact.contact = self.cleaned_data['contact']
+ assert contact.form_tag == self.form_tag
+ contact.save()
+ else:
+ contact = Contact.objects.create(
+ body=body,
+ ip=request.META['REMOTE_ADDR'],
+ contact=self.cleaned_data['contact'],
+ form_tag=self.form_tag)
+ email_changed = True
+ # not intended to be used with update forms
for name, value in self.cleaned_data.items():
if isinstance(value, UploadedFile):
attachment = Attachment(contact=contact, tag=name)
attachment.save()
site = Site.objects.get_current()
- dictionary = {
- 'form_tag': self.form_tag,
- 'site_name': getattr(self, 'site_name', site.name),
- 'site_domain': getattr(self, 'site_domain', site.domain),
- 'contact': contact,
- }
+ dictionary = self.get_dictionary(contact)
context = RequestContext(request)
mail_managers_subject = render_to_string([
'contact/%s/mail_managers_subject.txt' % self.form_tag,
except ValidationError:
pass
else:
- mail_subject = render_to_string([
- 'contact/%s/mail_subject.txt' % self.form_tag,
- 'contact/mail_subject.txt',
- ], dictionary, context).strip()
- if self.result_page:
- mail_body = render_to_string(
- 'contact/%s/results_email.txt' % contact.form_tag,
- {
- 'contact': contact,
- 'results': self.results(contact),
- }, context)
- else:
- mail_body = render_to_string([
- 'contact/%s/mail_body.txt' % self.form_tag,
- 'contact/mail_body.txt',
- ], dictionary, context)
- send_mail(mail_subject, mail_body, 'no-reply@%s' % site.domain, [contact.contact], fail_silently=True)
- if self.mailing or (self.mailing_field and self.cleaned_data[self.mailing_field]):
- email = self.cleaned_data['contact']
- mailing.subscribe(email)
+ if not self.instance:
+ mail_subject = render_to_string([
+ 'contact/%s/mail_subject.txt' % self.form_tag,
+ 'contact/mail_subject.txt',
+ ], dictionary, context).strip()
+ if self.result_page:
+ mail_body = render_to_string(
+ 'contact/%s/results_email.txt' % contact.form_tag,
+ {
+ 'contact': contact,
+ 'results': self.results(contact),
+ }, context)
+ else:
+ mail_body = render_to_string([
+ 'contact/%s/mail_body.txt' % self.form_tag,
+ 'contact/mail_body.txt',
+ ], dictionary, context)
+ send_mail(mail_subject, mail_body, 'no-reply@%s' % site.domain, [contact.contact], fail_silently=True)
+ if email_changed and (self.mailing or (self.mailing_field and self.cleaned_data[self.mailing_field])):
+ mailing.subscribe(contact.contact)
return contact
from django.contrib.auth.decorators import permission_required
from django.http import Http404
from django.shortcuts import get_object_or_404, redirect, render
+from django.views.decorators.cache import never_cache
from fnpdjango.utils.views import serve_file
from honeypot.decorators import check_honeypot
-from .forms import contact_forms
+from .forms import contact_forms, update_forms
from .models import Attachment, Contact
@check_honeypot
-def form(request, form_tag, force_enabled=False):
+@never_cache
+def form(request, form_tag, force_enabled=False, contact_id=None, key=None):
+ update = bool(contact_id and key)
try:
- form_class = contact_forms[form_tag]
+ if update and form_tag in update_forms:
+ form_class = update_forms[form_tag]
+ else:
+ form_class = contact_forms[form_tag]
except KeyError:
raise Http404
- if (getattr(form_class, 'disabled', False) and
- not (force_enabled and request.user.is_superuser)):
- template = getattr(form_class, 'disabled_template', None)
- if template:
- return render(request, template, {'title': form_class.form_title})
- raise Http404
+ if not (force_enabled and request.user.is_superuser):
+ disabled = getattr(form_class, 'disabled', False)
+ if disabled:
+ template = getattr(form_class, 'disabled_template', None)
+ if template:
+ return render(request, template, {'title': form_class.form_title})
+ raise Http404
+ if contact_id:
+ contact = get_object_or_404(Contact, id=contact_id, form_tag=form_tag)
+ if form_tag != 'olimpiada':
+ raise Http404
+ if key != contact.key:
+ raise Http404
+ else:
+ contact = None
if request.method == 'POST':
- form = form_class(request.POST, request.FILES)
+ form = form_class(request.POST, request.FILES, instance=contact)
else:
- form = form_class(initial=request.GET)
- formset_classes = getattr(form, 'form_formsets', {})
+ form = form_class(initial=request.GET, instance=contact)
if request.method == 'POST':
- formsets = {
- prefix: formset_class(request.POST, request.FILES, prefix=prefix)
- for prefix, formset_class in formset_classes.iteritems()}
+ formsets = form.get_formsets(request)
if form.is_valid() and all(formset.is_valid() for formset in formsets.itervalues()):
contact = form.save(request, formsets.values())
if form.result_page:
else:
return redirect('contact_thanks', form_tag)
else:
- formsets = {prefix: formset_class(prefix=prefix) for prefix, formset_class in formset_classes.iteritems()}
+ formsets = form.get_formsets()
return render(
request, ['contact/%s/form.html' % form_tag, 'contact/form.html'],
- {'form': form, 'formsets': formsets}
+ {'form': form, 'formsets': formsets, 'formset_errors': any(formset.errors for formset in formsets.values())}
)