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

Commit

Permalink
Merge pull request #239 from communitiesuk/add-cyp-mapping-report-for…
Browse files Browse the repository at this point in the history
…-feedback-report

Added CYP report mappings
  • Loading branch information
RamuniN authored Oct 18, 2023
2 parents 90933cb + 749a247 commit 1833fee
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 20 deletions.
6 changes: 6 additions & 0 deletions config/key_report_mappings/cof_r3w2_key_report_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
COF_R3W2_KEY_REPORT_MAPPING = KeyReportMapping(
round_id="6af19a5e-9cae-4f00-9194-cf10d2d7c8a7",
mapping=[
FormMappingItem(
form_name="applicant-information-cof-r3-w2",
form_name_cy="gwybodaeth-am-yr-ymgeisydd-cof-r3-w2",
key="NlHSBg",
return_field="applicant_email",
),
FormMappingItem(
form_name="organisation-information-cof-r3-w2",
form_name_cy="gwybodaeth-am-y-sefydliad-cof-r3-w2",
Expand Down
18 changes: 18 additions & 0 deletions config/key_report_mappings/cyp_r1_key_report_mapping.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from config.key_report_mappings.model import FormMappingItem
from config.key_report_mappings.model import KeyReportMapping

CYP_R1_KEY_REPORT_MAPPING = KeyReportMapping(
round_id="888aae3d-7e2c-4523-b9c1-95952b3d1644",
mapping=[
FormMappingItem(
form_name="applicant-information-cyp",
key="BKOHaM",
return_field="applicant_email",
),
FormMappingItem(
form_name="about-your-organisation-cyp",
key="JbmcJE",
return_field="organisation_name",
),
],
)
13 changes: 13 additions & 0 deletions config/key_report_mappings/mappings.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,27 @@
from config.key_report_mappings.cof_r3w2_key_report_mapping import (
COF_R3W2_KEY_REPORT_MAPPING,
)
from config.key_report_mappings.cyp_r1_key_report_mapping import (
CYP_R1_KEY_REPORT_MAPPING,
)

MAPPINGS = (
COF_R2_KEY_REPORT_MAPPING,
COF_R3W2_KEY_REPORT_MAPPING,
CYP_R1_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},
)


def get_report_mapping_for_round(round_id):
if round_id == COF_R2_KEY_REPORT_MAPPING.round_id:
return COF_R2_KEY_REPORT_MAPPING
elif round_id == COF_R3W2_KEY_REPORT_MAPPING.round_id:
return COF_R3W2_KEY_REPORT_MAPPING
elif round_id == CYP_R1_KEY_REPORT_MAPPING.round_id:
return CYP_R1_KEY_REPORT_MAPPING
37 changes: 17 additions & 20 deletions db/queries/feedback/queries.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from datetime import datetime

import jsonpath_rw_ext
from config.key_report_mappings.mappings import get_report_mapping_for_round
from db import db
from db.models import Applications
from db.models import Feedback
from db.models.feedback import EndOfApplicationSurveyFeedback
from db.queries.application.queries import get_applications
from db.queries.reporting.queries import map_application_key_fields
from db.schemas.application import ApplicationSchema
from db.schemas.end_of_application_survey import EndOfApplicationSurveyFeedbackSchema
from db.schemas.form import FormsRunnerSchema
from external_services.data import get_application_sections


Expand Down Expand Up @@ -116,35 +118,30 @@ def get_answer_value(form, search_key, search_value):
filters.append(Applications.status == status)
applications = get_applications(filters=filters, include_forms=True)

mapping_report = get_report_mapping_for_round(round_id)

# get section id & names map
application_sections = get_application_sections(fund_id, round_id, language="en")
for section in application_sections:
section_names[str(section["id"])] = section["title"]

# extract section feedbacks & end of survey feedbacks for all applications
serialiser = FormsRunnerSchema()
eoas_serialiser = EndOfApplicationSurveyFeedbackSchema()
applicant_serialiser = ApplicationSchema()

for application in applications:

# extract applicant email & organisation
for form in application.forms:
form_dict = serialiser.dump(form)
try:
if "applicant-information" in form.name:
applicant_email = get_answer_value(
form_dict, "title", "Lead contact email address"
)
except Exception as e:
print(f"Coudn't extract applicant email. Exception :{e}")
applicant_email = ""
try:
if "organisation-information" in form.name:
applicant_organisation = get_answer_value(
form_dict, "title", "Organisation name"
)
except Exception as e:
print(f"Coudn't extract applicant organisation. Exception :{e}")
applicant_organisation = ""
try:
result = map_application_key_fields(
applicant_serialiser.dump(application), mapping_report.mapping, round_id
)
applicant_email = result["applicant_email"]
applicant_organisation = result["organisation_name"]
except Exception as e:
print(f"Coudn't extract applicant email & organisation. Exception :{e}")
applicant_email = ""
applicant_organisation = ""

# extract sections feedback
for feedback in application.feedbacks:
Expand Down
11 changes: 11 additions & 0 deletions tests/test_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,16 @@ def test_extract_postcode(input_str, expected_output):
{
"language": "en",
"forms": [
{
"name": "applicant-information-cof-r3-w2",
"questions": [
{
"fields": [
{"key": "NlHSBg", "answer": "[email protected]"},
]
}
],
},
{
"name": "organisation-information-cof-r3-w2",
"questions": [
Expand Down Expand Up @@ -441,6 +451,7 @@ def test_extract_postcode(input_str, expected_output):
},
{
"eoi_reference": "Ref1234",
"applicant_email": "[email protected]",
"organisation_name": "OrgName",
"organisation_type": "Non-Profit",
"asset_type": "Building",
Expand Down

0 comments on commit 1833fee

Please sign in to comment.