Skip to content

Instantly share code, notes, and snippets.

@lb-
Last active September 3, 2017 12:36
Show Gist options
  • Save lb-/a040b1e680af42b054a0544177baf99c to your computer and use it in GitHub Desktop.
Save lb-/a040b1e680af42b054a0544177baf99c to your computer and use it in GitHub Desktop.
Wagtail Forms - Upload Image Field - Override process_form_submission Method
from wagtail.wagtailimages import get_image_model
class FormPage(AbstractEmailForm):
form_builder = ExtendedFormBuilder # as per step 3
def serve(self, request, *args, **kwargs):
# ... as per step 4
def process_form_submission(self, form):
cleaned_data = form.cleaned_data
for name, field in form.fields.iteritems():
if isinstance(field, WagtailImageField):
image_file_data = cleaned_data[name]
if image_file_data:
ImageModel = get_image_model()
image = ImageModel(
file=cleaned_data[name],
title=filename_to_title(cleaned_data[name].name),
collection=self.upload_image_to_collection,
# assumes there is always a user - will fail otherwise
uploaded_by_user=form.user,
)
image.save()
cleaned_data.update({name: image.id})
else:
# remove the value from the data
del cleaned_data[name]
form_data = json.dumps(cleaned_data, cls=DjangoJSONEncoder)
submission_object = dict(
page=self,
form_data=form_data,
user=form.user,
)
# the rest of the form page definition ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment