From 923c45409b5358974e5da9e872bffb55b6c614ea Mon Sep 17 00:00:00 2001 From: Parbhat Puri <parbhatpuri17@gmail.com> Date: Wed, 6 Feb 2019 13:44:17 +0000 Subject: [PATCH] Upload document in Wagtail form page --- .../migrations/0002_auto_20190206_1257.py | 18 +++++ opentech/public/forms/models.py | 66 ++++++++++++++++++- 2 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 opentech/public/forms/migrations/0002_auto_20190206_1257.py diff --git a/opentech/public/forms/migrations/0002_auto_20190206_1257.py b/opentech/public/forms/migrations/0002_auto_20190206_1257.py new file mode 100644 index 000000000..516f38b5c --- /dev/null +++ b/opentech/public/forms/migrations/0002_auto_20190206_1257.py @@ -0,0 +1,18 @@ +# Generated by Django 2.0.9 on 2019-02-06 12:57 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('public_forms', '0001_initial'), + ] + + operations = [ + migrations.AlterField( + model_name='formfield', + name='field_type', + field=models.CharField(choices=[('singleline', 'Single line text'), ('multiline', 'Multi-line text'), ('email', 'Email'), ('number', 'Number'), ('url', 'URL'), ('checkbox', 'Checkbox'), ('checkboxes', 'Checkboxes'), ('dropdown', 'Drop down'), ('multiselect', 'Multiple select'), ('radio', 'Radio buttons'), ('date', 'Date'), ('datetime', 'Date/time'), ('hidden', 'Hidden field'), ('document', 'Upload Document')], max_length=16, verbose_name='field type'), + ), + ] diff --git a/opentech/public/forms/models.py b/opentech/public/forms/models.py index b627a83a2..890ecc636 100644 --- a/opentech/public/forms/models.py +++ b/opentech/public/forms/models.py @@ -1,4 +1,10 @@ +import json + +from django.core.serializers.json import DjangoJSONEncoder +from django.conf import settings from django.db import models +from django.forms import FileField +from django.utils.translation import ugettext_lazy as _ from modelcluster.fields import ParentalKey @@ -6,17 +12,41 @@ from wagtail.core.fields import RichTextField from wagtail.admin.edit_handlers import ( FieldPanel, FieldRowPanel, MultiFieldPanel, InlinePanel ) -from wagtail.contrib.forms.models import AbstractEmailForm, AbstractFormField +from wagtail.contrib.forms.forms import FormBuilder +from wagtail.contrib.forms.models import ( + AbstractEmailForm, AbstractFormField, FORM_FIELD_CHOICES +) +from wagtail.documents.models import get_document_model from wagtail.search import index from opentech.public.utils.models import BasePage +def filename_to_title(filename): + from os.path import splitext + if filename: + result = splitext(filename)[0] + result = result.replace('-', ' ').replace('_', ' ') + return result.title() + + class FormField(AbstractFormField): + FORM_FIELD_CHOICES = FORM_FIELD_CHOICES + (('document', 'Upload Document'),) + field_type = models.CharField( + verbose_name=_('field type'), + max_length=16, + choices=FORM_FIELD_CHOICES + ) page = ParentalKey('FormPage', on_delete=models.CASCADE, related_name='form_fields') +class ExtendedFormBuilder(FormBuilder): + def create_document_field(self, field, options): + return FileField(**options) + + class FormPage(AbstractEmailForm, BasePage): + form_builder = ExtendedFormBuilder subpage_types = [] intro = RichTextField(blank=True) @@ -38,3 +68,37 @@ class FormPage(AbstractEmailForm, BasePage): FieldPanel('subject'), ], "Email"), ] + + def process_form_submission(self, form): + cleaned_data = form.cleaned_data + + for name, field in form.fields.items(): + if isinstance(field, FileField): + file_data = cleaned_data[name] + if file_data: + DocumentModel = get_document_model() + if form.user.is_anonymous: + document = DocumentModel( + file=cleaned_data[name], + title=filename_to_title(cleaned_data[name].name), + ) + else: + document = DocumentModel( + file=cleaned_data[name], + title=filename_to_title(cleaned_data[name].name), + uploaded_by_user=form.user, + ) + document.save() + if settings.DEBUG: + file_details_dict = {name: 'localhost:8000' + document.url} + else: + file_details_dict = {name: 'https://www.opentech.fund' + document.url} + cleaned_data.update(file_details_dict) + else: + del cleaned_data[name] + + form_data = json.dumps(cleaned_data, cls=DjangoJSONEncoder) + return self.get_submission_class().objects.create( + form_data=form_data, + page=self, + ) -- GitLab