diff --git a/opentech/apply/funds/blocks.py b/opentech/apply/funds/blocks.py index fbb553df57fc8a97599d3ac88572017a9311e633..6069c1390ba8a2813b8236d793602d664fa49156 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 d13c71f99ce9d37cd8a02e5345e0620b62610783..6792c477a71a1e65c31de90fd20a9876396927bc 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 646422bc08ced60de8d222412a6e76843a3bf54f..1bc7b0d049aebef8c4fd0036ec79ee44864b221d 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 012127318004a6c19f08ec575060cc3652a7242e..0745ef0280d4c152d7f9147fe5c6f6f8a3e22a87 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 6888d4d7475d0fcb86dda61b1b2957b772aefb0d..b255510b2c94a5d1d2a6da003c99c2d8fc3507c0 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 39120dd227f1c11b2df649bc27a71feed15af5bb..c72ceb10a04ef159b5d7d01b6efe4be03ea3a39d 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):