From d66e023ebf965a99e222019c3ad48a3d5ae660f9 Mon Sep 17 00:00:00 2001
From: Todd Dembrey <todd.dembrey@torchbox.com>
Date: Wed, 24 Jan 2018 22:14:06 +0000
Subject: [PATCH] Move the submission onto the round where it belongs

---
 ..._update_encoder_add_round_to_submission.py | 30 +++++++++++++
 opentech/apply/funds/models.py                | 44 +++++++++----------
 2 files changed, 51 insertions(+), 23 deletions(-)
 create mode 100644 opentech/apply/funds/migrations/0011_update_encoder_add_round_to_submission.py

diff --git a/opentech/apply/funds/migrations/0011_update_encoder_add_round_to_submission.py b/opentech/apply/funds/migrations/0011_update_encoder_add_round_to_submission.py
new file mode 100644
index 000000000..99eec0fd5
--- /dev/null
+++ b/opentech/apply/funds/migrations/0011_update_encoder_add_round_to_submission.py
@@ -0,0 +1,30 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.11.8 on 2018-01-24 22:09
+from __future__ import unicode_literals
+
+import django.contrib.postgres.fields.jsonb
+import django.core.serializers.json
+from django.db import migrations, models
+import django.db.models.deletion
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('wagtailcore', '0040_page_draft_title'),
+        ('funds', '0010_update_for_rich_text_block'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='applicationsubmission',
+            name='round',
+            field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, related_name='submissions', to='wagtailcore.Page'),
+            preserve_default=False,
+        ),
+        migrations.AlterField(
+            model_name='applicationsubmission',
+            name='form_data',
+            field=django.contrib.postgres.fields.jsonb.JSONField(encoder=django.core.serializers.json.DjangoJSONEncoder),
+        ),
+    ]
diff --git a/opentech/apply/funds/models.py b/opentech/apply/funds/models.py
index 8347c2162..7691f3c4b 100644
--- a/opentech/apply/funds/models.py
+++ b/opentech/apply/funds/models.py
@@ -39,28 +39,7 @@ def admin_url(page):
     return reverse('wagtailadmin_pages:edit', args=(page.id,))
 
 
-class AbstractApplicationStreamForm(AbstractStreamForm):
-    def get_submission_class(self):
-        return ApplicationSubmission
-
-    def process_form_submission(self, form):
-        cleaned_data = form.cleaned_data
-        for field in self.get_defined_fields():
-            # Update the ids which are unique to use the unique name
-            if isinstance(field.block, MustIncludeFieldBlock):
-                response = cleaned_data.pop(field.id)
-                cleaned_data[field.block.name] = response
-
-        return self.get_submission_class().objects.create(
-            form_data=cleaned_data,
-            page=self,
-        )
-
-    class Meta:
-        abstract = True
-
-
-class FundType(AbstractApplicationStreamForm):
+class FundType(AbstractStreamForm):
     parent_page_types = ['apply_home.ApplyHomePage']
     subpage_types = ['funds.Round']
 
@@ -128,7 +107,7 @@ class ApplicationForm(models.Model):
         return self.name
 
 
-class Round(AbstractApplicationStreamForm):
+class Round(AbstractStreamForm):
     parent_page_types = ['funds.FundType']
     subpage_types = []  # type: ignore
 
@@ -153,6 +132,24 @@ class Round(AbstractApplicationStreamForm):
         # Only return the first form, will need updating for when working with 2 stage WF
         return self.get_parent().specific.forms.all()[0].fields
 
+    def get_submission_class(self):
+        return ApplicationSubmission
+
+    def process_form_submission(self, form):
+        cleaned_data = form.cleaned_data
+        for field in self.get_defined_fields():
+            # Update the ids which are unique to use the unique name
+            if isinstance(field.block, MustIncludeFieldBlock):
+                response = cleaned_data.pop(field.id)
+                cleaned_data[field.block.name] = response
+
+        return self.get_submission_class().objects.create(
+            form_data=cleaned_data,
+            page=self.get_parent(),
+            round=self,
+        )
+
+
     def clean(self):
         super().clean()
 
@@ -228,6 +225,7 @@ class JSONOrderable(models.QuerySet):
 
 class ApplicationSubmission(AbstractFormSubmission):
     form_data = JSONField(encoder=DjangoJSONEncoder)
+    round = models.ForeignKey('wagtailcore.Page', on_delete=models.CASCADE, related_name='submissions')
 
     objects = JSONOrderable.as_manager()
 
-- 
GitLab