Skip to content
This repository has been archived by the owner on Nov 29, 2024. It is now read-only.

Commit

Permalink
FS-3596: Refactor key report mapping (#237)
Browse files Browse the repository at this point in the history
* 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
tferns authored Oct 11, 2023
1 parent 88e0f19 commit 90933cb
Show file tree
Hide file tree
Showing 8 changed files with 462 additions and 98 deletions.
2 changes: 1 addition & 1 deletion api/routes/application/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def get_key_applications_data_report(
get_report_for_applications(
status=status, round_id=round_id, fund_id=fund_id
),
get_key_report_field_headers(),
get_key_report_field_headers(round_id),
),
"text/csv",
as_attachment=True,
Expand Down
57 changes: 57 additions & 0 deletions config/key_report_mappings/cof_r2_key_report_mapping.py
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",
),
],
)
78 changes: 78 additions & 0 deletions config/key_report_mappings/cof_r3w2_key_report_mapping.py
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",
),
],
)
19 changes: 19 additions & 0 deletions config/key_report_mappings/mappings.py
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},
)
55 changes: 55 additions & 0 deletions config/key_report_mappings/model.py
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()
6 changes: 3 additions & 3 deletions db/queries/application/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@


def get_application(app_id, include_forms=False, as_json=False) -> dict | Applications:

stmt: Select = select(Applications).filter(Applications.id == app_id)

if include_forms:
Expand All @@ -44,9 +43,10 @@ def get_application(app_id, include_forms=False, as_json=False) -> dict | Applic


def get_applications(
filters=[], include_forms=False, as_json=False
filters=None, include_forms=False, as_json=False
) -> list[dict] | list[Applications]:

if filters is None:
filters = []
stmt: Select = select(Applications)

if len(filters) > 0:
Expand Down
Loading

0 comments on commit 90933cb

Please sign in to comment.