This repository has been archived by the owner on Nov 29, 2024. It is now read-only.
generated from communitiesuk/funding-service-design-TEMPLATE
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FS-3596: Refactor key report mapping (#237)
* FS-3596: Refactor key report mapping * FS-3596: Fix tests on key report mapping * FS-3596: Remove irrelevant polymorphism * FS-3596: Add columns from application * FS-3596: Update test coverage * FS-3596: Remove hardcoded link
- Loading branch information
Showing
8 changed files
with
462 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from config.key_report_mappings.model import extract_postcode | ||
from config.key_report_mappings.model import FormMappingItem | ||
from config.key_report_mappings.model import KeyReportMapping | ||
|
||
COF_R2_KEY_REPORT_MAPPING = KeyReportMapping( | ||
round_id="c603d114-5364-4474-a0c4-c41cbf4d3bbd", | ||
mapping=[ | ||
FormMappingItem( | ||
form_name="organisation-information", | ||
form_name_cy="gwybodaeth-am-y-sefydliad", | ||
key="WWWWxy", | ||
return_field="eoi_reference", | ||
), | ||
FormMappingItem( | ||
form_name="organisation-information", | ||
form_name_cy="gwybodaeth-am-y-sefydliad", | ||
key="YdtlQZ", | ||
return_field="organisation_name", | ||
), | ||
FormMappingItem( | ||
form_name="organisation-information", | ||
form_name_cy="gwybodaeth-am-y-sefydliad", | ||
key="lajFtB", | ||
return_field="organisation_type", | ||
), | ||
FormMappingItem( | ||
form_name="asset-information", | ||
form_name_cy="gwybodaeth-am-yr-ased", | ||
key="yaQoxU", | ||
return_field="asset_type", | ||
), | ||
FormMappingItem( | ||
form_name="project-information", | ||
form_name_cy="gwybodaeth-am-y-prosiect", | ||
key="yEmHpp", | ||
return_field="geography", | ||
formatter=extract_postcode, | ||
), | ||
FormMappingItem( | ||
form_name="funding-required", | ||
form_name_cy="cyllid-sydd-ei-angen", | ||
key="JzWvhj", | ||
return_field="capital", | ||
), | ||
FormMappingItem( | ||
form_name="funding-required", | ||
form_name_cy="cyllid-sydd-ei-angen", | ||
key="jLIgoi", | ||
return_field="revenue", | ||
), | ||
FormMappingItem( | ||
form_name="organisation-information-ns", | ||
key="opFJRm", | ||
return_field="organisation_name_nstf", | ||
), | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
from config.key_report_mappings.model import ApplicationColumnMappingItem | ||
from config.key_report_mappings.model import extract_postcode | ||
from config.key_report_mappings.model import FormMappingItem | ||
from config.key_report_mappings.model import KeyReportMapping | ||
|
||
COF_R3W2_KEY_REPORT_MAPPING = KeyReportMapping( | ||
round_id="6af19a5e-9cae-4f00-9194-cf10d2d7c8a7", | ||
mapping=[ | ||
FormMappingItem( | ||
form_name="organisation-information-cof-r3-w2", | ||
form_name_cy="gwybodaeth-am-y-sefydliad-cof-r3-w2", | ||
key="WWWWxy", | ||
return_field="eoi_reference", | ||
), | ||
FormMappingItem( | ||
form_name="organisation-information-cof-r3-w2", | ||
form_name_cy="gwybodaeth-am-y-sefydliad-cof-r3-w2", | ||
key="YdtlQZ", | ||
return_field="organisation_name", | ||
), | ||
FormMappingItem( | ||
form_name="organisation-information-cof-r3-w2", | ||
form_name_cy="gwybodaeth-am-y-sefydliad-cof-r3-w2", | ||
key="lajFtB", | ||
return_field="organisation_type", | ||
), | ||
FormMappingItem( | ||
form_name="asset-information-cof-r3-w2", | ||
form_name_cy="gwybodaeth-am-yr-ased-cof-r3-w2", | ||
key="oXGwlA", | ||
return_field="asset_type", | ||
), | ||
FormMappingItem( | ||
form_name="asset-information-cof-r3-w2", | ||
form_name_cy="gwybodaeth-am-yr-ased-cof-r3-w2", | ||
key="aJGyCR", | ||
return_field="asset_type_other", | ||
), | ||
FormMappingItem( | ||
form_name="project-information-cof-r3-w2", | ||
form_name_cy="gwybodaeth-am-y-prosiect-cof-r3-w2", | ||
key="EfdliG", | ||
return_field="geography", | ||
formatter=extract_postcode, | ||
), | ||
FormMappingItem( | ||
form_name="funding-required-cof-r3-w2", | ||
form_name_cy="cyllid-sydd-ei-angen-cof-r3-w2", | ||
key="ABROnB", | ||
return_field="capital", | ||
), | ||
FormMappingItem( | ||
form_name="funding-required-cof-r3-w2", | ||
form_name_cy="cyllid-sydd-ei-angen-cof-r3-w2", | ||
key="tSKhQQ", | ||
return_field="revenue", | ||
formatter=lambda answer: sum([x["UyaAHw"] for x in answer or []]), | ||
), | ||
ApplicationColumnMappingItem( | ||
column_name="reference", | ||
return_field="ref", | ||
), | ||
ApplicationColumnMappingItem( | ||
column_name="id", | ||
return_field="link", | ||
), | ||
# ApplicationColumnMappingItem( # think we'd need to add a concept for grabbing email by account id # noqa | ||
# column_name="email", # however that data belongs in the account-store # noqa | ||
# return_field="account_id" # noqa | ||
# ), # noqa | ||
FormMappingItem( | ||
form_name="project-information-cof-r3-w2", | ||
form_name_cy="gwybodaeth-am-y-prosiect-cof-r3-w2", | ||
key="apGjFS", | ||
return_field="project_name", | ||
), | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from collections import defaultdict | ||
|
||
from config.key_report_mappings.cof_r2_key_report_mapping import ( | ||
COF_R2_KEY_REPORT_MAPPING, | ||
) | ||
from config.key_report_mappings.cof_r3w2_key_report_mapping import ( | ||
COF_R3W2_KEY_REPORT_MAPPING, | ||
) | ||
|
||
MAPPINGS = ( | ||
COF_R2_KEY_REPORT_MAPPING, | ||
COF_R3W2_KEY_REPORT_MAPPING, | ||
) | ||
|
||
ROUND_ID_TO_KEY_REPORT_MAPPING = defaultdict( | ||
# default COF R2 as at the time of this refactor, that was used by default in existing code | ||
lambda: COF_R2_KEY_REPORT_MAPPING.mapping, | ||
{m.round_id: m.mapping for m in MAPPINGS}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import re | ||
from dataclasses import dataclass | ||
from typing import Any | ||
from typing import Callable | ||
|
||
|
||
@dataclass | ||
class MappingItem: | ||
return_field: str | ||
formatter: Callable[[Any], Any] = None | ||
|
||
|
||
@dataclass | ||
class FormMappingItem(MappingItem): | ||
key: str | None = None | ||
form_name: str | None = None | ||
form_name_cy: str | None = None | ||
|
||
def get_form_name(self, language: str = "en"): | ||
if language == "cy": | ||
return self.form_name_cy | ||
return self.form_name | ||
|
||
def format_answer(self, field: dict) -> str: | ||
if (answer := field.get("answer")) and self.formatter: | ||
return self.formatter(answer) | ||
return answer # no formatting required by default | ||
|
||
|
||
@dataclass | ||
class ApplicationColumnMappingItem(MappingItem): | ||
column_name: str | None = None | ||
|
||
def format_answer(self, data: Any) -> str: | ||
if data and self.formatter: | ||
return self.formatter(data) | ||
return data | ||
|
||
|
||
@dataclass | ||
class KeyReportMapping: | ||
round_id: str | ||
mapping: list[MappingItem] | ||
|
||
|
||
# this was extracted from existing functionality, not a fan of regex for this | ||
def extract_postcode(postcode: str) -> str | None: | ||
postcode = re.search( | ||
"([A-Za-z][A-Ha-hJ-Yj-y]?[0-9][A-Za-z0-9]?" | ||
" ?[0-9][A-Za-z]{2}|[Gg][Ii][Rr]" | ||
" ?0[Aa]{2})", # noqa | ||
postcode, | ||
) | ||
if postcode: | ||
return postcode.group() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.