From f9581d0cd4f0bbd555e3fef928a8016b70f69364 Mon Sep 17 00:00:00 2001
From: Todd Dembrey <todd.dembrey@torchbox.com>
Date: Tue, 23 Jan 2018 15:01:57 +0000
Subject: [PATCH] Update the submission to use a JSON field to store data

---
 .../migrations/0005_applicationsubmission.py  |  3 ++-
 opentech/apply/funds/models.py                | 22 +++++++++++++++++++
 2 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/opentech/apply/funds/migrations/0005_applicationsubmission.py b/opentech/apply/funds/migrations/0005_applicationsubmission.py
index f2c19d924..ac820aae9 100644
--- a/opentech/apply/funds/migrations/0005_applicationsubmission.py
+++ b/opentech/apply/funds/migrations/0005_applicationsubmission.py
@@ -2,6 +2,7 @@
 # Generated by Django 1.11.8 on 2018-01-22 09:40
 from __future__ import unicode_literals
 
+import django.contrib.postgres.fields.jsonb
 from django.db import migrations, models
 import django.db.models.deletion
 
@@ -18,7 +19,7 @@ class Migration(migrations.Migration):
             name='ApplicationSubmission',
             fields=[
                 ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
-                ('form_data', models.TextField()),
+                ('form_data', django.contrib.postgres.fields.jsonb.JSONField()),
                 ('submit_time', models.DateTimeField(auto_now_add=True, verbose_name='submit time')),
                 ('page', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='wagtailcore.Page')),
             ],
diff --git a/opentech/apply/funds/models.py b/opentech/apply/funds/models.py
index cbedf7486..9c0b27500 100644
--- a/opentech/apply/funds/models.py
+++ b/opentech/apply/funds/models.py
@@ -1,6 +1,7 @@
 from datetime import date
 
 from django.core.exceptions import ValidationError
+from django.contrib.postgres.fields import JSONField
 from django.db import models
 from django.db.models import Q
 from django.http import Http404
@@ -56,6 +57,13 @@ class FundType(AbstractStreamForm):
     def get_submission_class(self):
         return ApplicationSubmission
 
+    def process_form_submission(self, form):
+        # Handle passing to JSONField
+        return self.get_submission_class().objects.create(
+            form_data=form.cleaned_data,
+            page=self,
+        )
+
     @property
     def workflow_class(self):
         return WORKFLOW_CLASS[self.get_workflow_display()]
@@ -189,8 +197,22 @@ class Round(AbstractStreamForm):
 
 
 class ApplicationSubmission(AbstractFormSubmission):
+    form_data = JSONField()
+
+    def get_data(self):
+        # Updated for JSONField
+        form_data = self.form_data
+        form_data.update({
+            'submit_time': self.submit_time,
+        })
+
+        return form_data
+
     def __getattr__(self, item):
         # fall back to values defined on the data
         if item in REQUIRED_BLOCK_NAMES:
             return self.get_data()[item]
         return super().__getattr__(item)
+
+    def __str__(self):
+        return str(super().__str__())
-- 
GitLab