From d5e1a42bf8ac065a7599ab1422bc734edb3e08e3 Mon Sep 17 00:00:00 2001
From: Todd Dembrey <todd.dembrey@torchbox.com>
Date: Wed, 26 Sep 2018 16:15:37 +0100
Subject: [PATCH] Refactor to call the blocks "Named blocks" for single and
 required

---
 opentech/apply/funds/blocks.py             |  2 ++
 opentech/apply/funds/models/mixins.py      | 14 +++++++-------
 opentech/apply/funds/models/submissions.py | 18 +++++++++---------
 opentech/apply/funds/tests/test_models.py  |  6 +++---
 opentech/apply/funds/tests/test_views.py   |  2 +-
 opentech/apply/funds/views.py              |  4 ++--
 6 files changed, 24 insertions(+), 22 deletions(-)

diff --git a/opentech/apply/funds/blocks.py b/opentech/apply/funds/blocks.py
index fbb553df5..6069c1390 100644
--- a/opentech/apply/funds/blocks.py
+++ b/opentech/apply/funds/blocks.py
@@ -120,3 +120,5 @@ class ApplicationCustomFormFieldsBlock(CustomFormFieldsBlock, FormFieldsBlock):
 REQUIRED_BLOCK_NAMES = [block.name for block in ApplicationMustIncludeFieldBlock.__subclasses__()]
 
 SINGLE_BLOCK_NAMES = [block.name for block in ApplicationSingleIncludeFieldBlock.__subclasses__()]
+
+NAMED_BLOCKS = REQUIRED_BLOCK_NAMES + SINGLE_BLOCK_NAMES
diff --git a/opentech/apply/funds/models/mixins.py b/opentech/apply/funds/models/mixins.py
index d13c71f99..6792c477a 100644
--- a/opentech/apply/funds/models/mixins.py
+++ b/opentech/apply/funds/models/mixins.py
@@ -30,7 +30,7 @@ class AccessFormData:
         # Returns the data mapped by field id instead of the data stored using the must include
         # values
         data = self.form_data.copy()
-        for field_name, field_id in self.must_include.items():
+        for field_name, field_id in self.named_blocks.items():
             if field_id not in data:
                 response = data[field_name]
                 data[field_id] = response
@@ -78,8 +78,8 @@ class AccessFormData:
         return data
 
     def get_definitive_id(self, id):
-        if id in self.must_include:
-            return self.must_include[id]
+        if id in self.named_blocks:
+            return self.named_blocks[id]
         return id
 
     def field(self, id):
@@ -112,17 +112,17 @@ class AccessFormData:
     def fields(self):
         # ALl fields on the application
         fields = self.raw_fields.copy()
-        for field_name, field_id in self.must_include.items():
+        for field_name, field_id in self.named_blocks.items():
             response = fields.pop(field_id)
             fields[field_name] = response
         return fields
 
     @property
-    def must_include(self):
+    def named_blocks(self):
         return {
             field.block.name: field.id
             for field in self.form_fields
-            if isinstance(field.block, MustIncludeFieldBlock)
+            if isinstance(field.block, SingleIncludeMixin)
         }
 
     def render_answer(self, field_id, include_question=False):
@@ -135,7 +135,7 @@ class AccessFormData:
         return [
             self.render_answer(field_id, include_question=True)
             for field_id in self.question_field_ids
-            if field_id not in self.must_include
+            if field_id not in self.named_blocks
         ]
 
     def output_answers(self):
diff --git a/opentech/apply/funds/models/submissions.py b/opentech/apply/funds/models/submissions.py
index 646422bc0..1bc7b0d04 100644
--- a/opentech/apply/funds/models/submissions.py
+++ b/opentech/apply/funds/models/submissions.py
@@ -26,7 +26,7 @@ from opentech.apply.stream_forms.models import BaseStreamForm
 
 from .mixins import AccessFormData
 from .utils import LIMIT_TO_STAFF, LIMIT_TO_STAFF_AND_REVIEWERS, WorkflowHelpers
-from ..blocks import ApplicationCustomFormFieldsBlock, REQUIRED_BLOCK_NAMES
+from ..blocks import ApplicationCustomFormFieldsBlock, NAMED_BLOCKS
 from ..workflow import (
     active_statuses,
     DETERMINATION_RESPONSE_PHASES,
@@ -51,7 +51,7 @@ class JSONOrderable(models.QuerySet):
 
         def build_json_order_by(field):
             try:
-                if field.replace('-', '') not in REQUIRED_BLOCK_NAMES:
+                if field.replace('-', '') not in NAMED_BLOCKS:
                     return field
             except AttributeError:
                 return field
@@ -251,8 +251,8 @@ class ApplicationSubmissionMetaclass(AddTransitions):
 
         # We want to access the redered display of the required fields.
         # Treat in similar way to django's get_FIELD_display
-        for required_name in REQUIRED_BLOCK_NAMES:
-            partial_method_name = f'_{required_name}_method'
+        for block_name in NAMED_BLOCKS:
+            partial_method_name = f'_{block_name}_method'
             # We need to generate the partial method and the wrap it in property so
             # we can access the required fields like normal fields. e.g. self.title
             # Partial method requires __get__ to be called in order to bind it to the
@@ -262,17 +262,17 @@ class ApplicationSubmissionMetaclass(AddTransitions):
             setattr(
                 cls,
                 partial_method_name,
-                partialmethod(cls._get_REQUIRED_value, name=required_name),
+                partialmethod(cls._get_REQUIRED_value, name=block_name),
             )
             setattr(
                 cls,
-                f'{required_name}',
+                f'{block_name}',
                 property(getattr(cls, partial_method_name)),
             )
             setattr(
                 cls,
-                f'get_{required_name}_display',
-                partialmethod(cls._get_REQUIRED_display, name=required_name),
+                f'get_{block_name}_display',
+                partialmethod(cls._get_REQUIRED_display, name=block_name),
             )
         return cls
 
@@ -460,7 +460,7 @@ class ApplicationSubmission(
         self.process_file_data(self.form_data)
 
     def process_form_data(self):
-        for field_name, field_id in self.must_include.items():
+        for field_name, field_id in self.named_blocks.items():
             response = self.form_data.pop(field_id, None)
             if response:
                 self.form_data[field_name] = response
diff --git a/opentech/apply/funds/tests/test_models.py b/opentech/apply/funds/tests/test_models.py
index 012127318..0745ef028 100644
--- a/opentech/apply/funds/tests/test_models.py
+++ b/opentech/apply/funds/tests/test_models.py
@@ -445,10 +445,10 @@ class TestApplicationSubmission(TestCase):
 
 
 class TestSubmissionRenderMethods(TestCase):
-    def test_must_include_not_included_in_answers(self):
+    def test_named_blocks_not_included_in_answers(self):
         submission = ApplicationSubmissionFactory()
         answers = submission.render_answers()
-        for name in submission.must_include:
+        for name in submission.named_blocks:
             field = submission.field(name)
             self.assertNotIn(field.value['field_label'], answers)
 
@@ -456,7 +456,7 @@ class TestSubmissionRenderMethods(TestCase):
         submission = ApplicationSubmissionFactory()
         answers = submission.output_answers()
         for field_name in submission.question_field_ids:
-            if field_name not in submission.must_include:
+            if field_name not in submission.named_blocks:
                 field = submission.field(field_name)
                 self.assertIn(field.value['field_label'], answers)
 
diff --git a/opentech/apply/funds/tests/test_views.py b/opentech/apply/funds/tests/test_views.py
index 6888d4d74..b255510b2 100644
--- a/opentech/apply/funds/tests/test_views.py
+++ b/opentech/apply/funds/tests/test_views.py
@@ -32,7 +32,7 @@ def prepare_form_data(submission, **kwargs):
         field_id = submission.field(field).id
         data[field_id] = value
 
-    address_field = submission.must_include['address']
+    address_field = submission.named_blocks['address']
     address = data.pop(address_field)
     data.update(**prepare_address(address, address_field))
 
diff --git a/opentech/apply/funds/views.py b/opentech/apply/funds/views.py
index 39120dd22..c72ceb10a 100644
--- a/opentech/apply/funds/views.py
+++ b/opentech/apply/funds/views.py
@@ -393,7 +393,7 @@ class RevisionCompareView(DetailView):
             compare(*fields, should_bleach=False)
             for fields in zip(from_required, to_required)
         ]
-        for field, diff in zip(self.object.must_include, diffed_required):
+        for field, diff in zip(self.object.named_blocks, diffed_required):
             setattr(self.object, 'get_{}_display'.format(field), diff)
 
         # Compare all the answers
@@ -407,7 +407,7 @@ class RevisionCompareView(DetailView):
     def render_required(self):
         return [
             getattr(self.object, 'get_{}_display'.format(field))()
-            for field in self.object.must_include
+            for field in self.object.named_blocks
         ]
 
     def get_context_data(self, **kwargs):
-- 
GitLab