Last active
October 4, 2021 11:08
-
-
Save tobiase/27f4f6427fb9e4858581fcc7256b81fc to your computer and use it in GitHub Desktop.
Prefill wagtailforms with URL parameters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import absolute_import, unicode_literals | |
import django | |
import django.forms | |
import wagtail.wagtailforms.models as form_models | |
from django.db import models | |
from django.shortcuts import render | |
from django.utils.translation import ugettext_lazy as _ | |
from modelcluster.fields import ParentalKey | |
from wagtail.wagtailadmin.edit_handlers import ( | |
FieldPanel, FieldRowPanel, | |
InlinePanel, MultiFieldPanel | |
) | |
from wagtail.wagtailcore.fields import RichTextField | |
from wagtail.wagtailcore.models import Page | |
from wagtail.wagtailforms.forms import FormBuilder | |
from wagtail.wagtailforms.models import AbstractEmailForm, AbstractFormField | |
FORM_FIELD_CHOICES = form_models.FORM_FIELD_CHOICES + ( | |
('hidden', _('Hidden')), | |
) | |
class FormField(AbstractFormField): | |
field_type = models.CharField(verbose_name=_('field type'), max_length=16, choices=FORM_FIELD_CHOICES) | |
page = ParentalKey('FormPage', related_name='form_fields') | |
class CmsFormBuilder(FormBuilder): | |
def create_hidden_field(self, field, options): | |
options['max_length'] = 255 | |
return django.forms.CharField(widget=django.forms.HiddenInput, **options) | |
FormBuilder.FIELD_TYPES['hidden'] = create_hidden_field | |
class FormPage(AbstractEmailForm): | |
form_builder = CmsFormBuilder | |
intro = RichTextField(blank=True) | |
outro = RichTextField(blank=True) | |
thank_you_text = RichTextField(blank=True) | |
content_panels = AbstractEmailForm.content_panels + [ | |
FieldPanel('intro', classname="full"), | |
InlinePanel('form_fields', label="Form fields"), | |
FieldPanel('outro', classname="full"), | |
FieldPanel('thank_you_text', classname="full"), | |
MultiFieldPanel([ | |
FieldRowPanel([ | |
FieldPanel('from_address', classname="col6"), | |
FieldPanel('to_address', classname="col6"), | |
]), | |
FieldPanel('subject'), | |
], "Email"), | |
] | |
def serve(self, request, *args, **kwargs): | |
if request.method == 'POST': | |
form = self.get_form(request.POST, page=self, user=request.user) | |
if form.is_valid(): | |
self.process_form_submission(form) | |
# render the landing_page | |
# TODO: It is much better to redirect to it | |
return render( | |
request, | |
self.get_landing_page_template(request), | |
self.get_context(request) | |
) | |
else: | |
form = self.get_form(page=self, user=request.user) | |
for key in request.GET: | |
try: | |
form.fields[key].initial = request.GET[key] | |
except KeyError: | |
# Ignore unexpected parameters | |
pass | |
context = self.get_context(request) | |
context['form'] = form | |
return render( | |
request, | |
self.get_template(request), | |
context | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment