From e85783128309ad20a1abcbef82fe3576eedd2286 Mon Sep 17 00:00:00 2001
From: Todd Dembrey <todd.dembrey@torchbox.com>
Date: Sun, 4 Nov 2018 10:39:05 +0000
Subject: [PATCH] Allow non responded named fields to be rendered

---
 opentech/apply/funds/models/mixins.py     | 9 +++++++--
 opentech/apply/funds/tests/test_models.py | 7 +++++++
 2 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/opentech/apply/funds/models/mixins.py b/opentech/apply/funds/models/mixins.py
index 5ddbb6377..87a1dd39e 100644
--- a/opentech/apply/funds/models/mixins.py
+++ b/opentech/apply/funds/models/mixins.py
@@ -36,8 +36,13 @@ class AccessFormData:
         data = self.form_data.copy()
         for field_name, field_id in self.named_blocks.items():
             if field_id not in data:
-                response = data[field_name]
-                data[field_id] = response
+                try:
+                    response = data[field_name]
+                except KeyError:
+                    # There was no value supplied for the named field
+                    pass
+                else:
+                    data[field_id] = response
         return data
 
     @classmethod
diff --git a/opentech/apply/funds/tests/test_models.py b/opentech/apply/funds/tests/test_models.py
index 0745ef028..5943d8b76 100644
--- a/opentech/apply/funds/tests/test_models.py
+++ b/opentech/apply/funds/tests/test_models.py
@@ -468,6 +468,13 @@ class TestSubmissionRenderMethods(TestCase):
         answers = submission.render_answers()
         self.assertNotIn(rich_text_label, answers)
 
+    def test_named_blocks_dont_break_if_no_response(self):
+        submission = ApplicationSubmissionFactory()
+        # the user didn't respond
+        del submission.form_data['value']
+        self.assertTrue('value' not in submission.raw_data)
+        self.assertTrue('duration' in submission.raw_data)
+
 
 class TestRequestForPartners(TestCase):
     def test_message_when_no_round(self):
-- 
GitLab