From 81886377b2e3b0089de99542e5c9e940397d3211 Mon Sep 17 00:00:00 2001
From: Todd Dembrey <todd.dembrey@torchbox.com>
Date: Mon, 22 Jan 2018 22:31:02 +0000
Subject: [PATCH] Defer to the submitted data to find certain information

---
 .../apply/dashboard/templates/dashboard/dashboard.html   | 2 +-
 opentech/apply/funds/blocks.py                           | 9 ++++++---
 opentech/apply/funds/models.py                           | 8 ++++++--
 3 files changed, 13 insertions(+), 6 deletions(-)

diff --git a/opentech/apply/dashboard/templates/dashboard/dashboard.html b/opentech/apply/dashboard/templates/dashboard/dashboard.html
index bdfa001e5..d95e40df8 100644
--- a/opentech/apply/dashboard/templates/dashboard/dashboard.html
+++ b/opentech/apply/dashboard/templates/dashboard/dashboard.html
@@ -7,7 +7,7 @@
 {% for submission in applicationsubmission_list %}
     <tr>
         <td>
-            {{ submission }}
+            {{ submission.title }}
         </td>
     </tr>
 {% empty %}
diff --git a/opentech/apply/funds/blocks.py b/opentech/apply/funds/blocks.py
index cbe885a8c..f07a94967 100644
--- a/opentech/apply/funds/blocks.py
+++ b/opentech/apply/funds/blocks.py
@@ -26,15 +26,14 @@ class CustomFormFieldsBlock(FormFieldsBlock):
         else:
             error_dict = dict()
 
-        required_block_names = [block.name  for block in MustIncludeFieldBlock.__subclasses__()]
 
         block_types = [block.block_type for block in value]
-        missing = set(required_block_names) - set(block_types)
+        missing = set(REQUIRED_BLOCK_NAMES) - set(block_types)
 
         counted_types = Counter(block_types)
         duplicates = [
             name for name, count in counted_types.items()
-            if name in required_block_names and count > 1
+            if name in REQUIRED_BLOCK_NAMES and count > 1
         ]
 
         all_errors = list()
@@ -75,6 +74,7 @@ class MustIncludeStatic(StaticBlock):
     def render_form(self, *args, **kwargs):
         errors = kwargs.pop('errors')
         if errors:
+            # Pretend the error is a readonly input so that we get nice formatting
             error_message= '<div class="error"><input readonly placeholder="{}"></div>'.format(errors[0])
         else:
             error_message = ''
@@ -98,3 +98,6 @@ class TitleBlock(MustIncludeFieldBlock):
 class ValueBlock(MustIncludeFieldBlock):
     name = 'value'
     description = 'The value of the project'
+
+
+REQUIRED_BLOCK_NAMES = [block.name  for block in MustIncludeFieldBlock.__subclasses__()]
diff --git a/opentech/apply/funds/models.py b/opentech/apply/funds/models.py
index 64cf0ccc5..cbedf7486 100644
--- a/opentech/apply/funds/models.py
+++ b/opentech/apply/funds/models.py
@@ -21,7 +21,7 @@ from wagtail.wagtailforms.models import AbstractFormSubmission
 
 from opentech.apply.stream_forms.models import AbstractStreamForm
 
-from .blocks import CustomFormFieldsBlock
+from .blocks import CustomFormFieldsBlock, REQUIRED_BLOCK_NAMES
 from .forms import WorkflowFormAdminForm
 from .workflow import SingleStage, DoubleStage
 
@@ -189,4 +189,8 @@ class Round(AbstractStreamForm):
 
 
 class ApplicationSubmission(AbstractFormSubmission):
-    pass
+    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)
-- 
GitLab