Skip to content

Commit

Permalink
push updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam-W1 committed Oct 29, 2024
1 parent 39c2fc4 commit 1cc5bf0
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 9 deletions.
17 changes: 14 additions & 3 deletions app/export_config/generate_form.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import copy
import json
from dataclasses import asdict

from app.db.models import Component
Expand Down Expand Up @@ -122,6 +123,7 @@ def build_page(page: Page = None) -> dict:
{
"path": f"/{page.display_path}",
"title": page.name_in_apply_json["en"],
"section": page.section["name"] if page.section else None,
}
)
# Having a 'null' controller element breaks the form-json, needs to not be there if blank
Expand Down Expand Up @@ -166,7 +168,10 @@ def build_navigation(partial_form_json: dict, input_pages: list[Page]) -> dict:
for condition in component.conditions:
destination_path = f"/{condition['destination_page_path'].lstrip('/')}"

this_page_in_results["next"].append(
# find source page of condition
source_path = condition["source_page_path"]
source_page = next(p for p in input_pages if p.display_path == source_path)
source_page["next"].append(
{
"path": destination_path,
"condition": condition["name"],
Expand Down Expand Up @@ -206,7 +211,7 @@ def _find_page_by_controller(pages, controller_name) -> dict:
return next((p for p in pages if p.controller and p.controller.endswith(controller_name)), None)


def build_start_page(content: str, form: Form) -> dict:
def build_start_page(content: str, form: Form, section=None) -> dict:
"""
Builds the start page which contains just an html component comprising a bullet
list of the headings of all pages in this form
Expand All @@ -217,6 +222,7 @@ def build_start_page(content: str, form: Form) -> dict:
"title": form.name_in_apply_json["en"],
"path": f"/intro-{human_to_kebab_case(form.name_in_apply_json['en'])}",
"controller": "./pages/start.js",
"section": json.loads(section)["name"] if section else None,
}
)
ask_about = None
Expand Down Expand Up @@ -265,7 +271,12 @@ def build_form_json(form: Form) -> dict:
for page in form.pages:
results["pages"].append(build_page(page=page))
if page.section:
results["sections"].append(page.section)
section_dict = page.section
# only keep keys with value
cleaned_section_dict = {k: v for k, v in section_dict.items() if v is not None and v != ""}
# only add section is doesnt already exist
if cleaned_section_dict not in results["sections"]:
results["sections"].append(cleaned_section_dict)

# start page is the page with the controller ending start.js
start_page = _find_page_by_controller(form.pages, "start.js")
Expand Down
20 changes: 14 additions & 6 deletions app/import_config/load_form_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@
from app.db.models import Page # noqa:E402
from app.shared.data_classes import Condition # noqa:E402
from app.shared.data_classes import ConditionValue # noqa:E402
from app.shared.data_classes import FormSection # noqa:E402
from app.shared.helpers import find_enum # noqa:E402


def _build_condition(condition_data, destination_page_path) -> Condition:
def _build_condition(condition_data, source_page_path, destination_page_path) -> Condition:
sub_conditions = []
for c in condition_data["value"]["conditions"]:
sc = {
Expand All @@ -40,6 +41,7 @@ def _build_condition(condition_data, destination_page_path) -> Condition:
display_name=condition_data["displayName"],
value=condition_value,
destination_page_path=destination_page_path,
source_page_path=source_page_path,
)
return result

Expand Down Expand Up @@ -74,7 +76,7 @@ def add_conditions_to_components(db, page: dict, conditions: dict, page_id):
component_to_update = components_cache[runner_component_name]

# Create a new Condition instance with a different variable name
new_condition = _build_condition(condition_data, destination_page_path=path["path"])
new_condition = _build_condition(condition_data, page["path"], destination_page_path=path["path"])

# Add the new condition to the conditions list of the component to update
if component_to_update.conditions:
Expand Down Expand Up @@ -134,7 +136,13 @@ def insert_component_as_template(component, page_id, page_index, lizts):
return new_component


def insert_page_as_template(page, form_id):
def insert_page_as_template(page, form_id, form_config):
section_config = None
if page.get("section"):
# find section in form_config
section = next((s for s in form_config["sections"] if s["name"] == page["section"]), None)
section_config = FormSection(**section).as_dict()

new_page = Page(
form_id=form_id,
display_path=page.get("path").lstrip("/"),
Expand All @@ -143,7 +151,7 @@ def insert_page_as_template(page, form_id):
controller=page.get("controller", None),
is_template=True,
template_name=page.get("title", None),
section=page.get("section", None),
section=section_config,
)
try:
db.session.add(new_page)
Expand Down Expand Up @@ -185,7 +193,7 @@ def insert_form_config(form_config, form_id):
for page in form_config.get("pages", []):
if page["path"] == start_page_path:
page["controller"] = "start.js"
inserted_page = insert_page_as_template(page, form_id)
inserted_page = insert_page_as_template(page, form_id, form_config)
inserted_pages.append(inserted_page)
db.session.flush() # flush to get the page id
for c_idx, component in enumerate(page.get("components", [])):
Expand All @@ -195,7 +203,7 @@ def insert_form_config(form_config, form_id):
inserted_components.append(inserted_component)
db.session.flush() # flush to make components available for conditions

# conditions span across pages in a form
# conditions span across pages in a form so we need all components available before adding conditions
for page in form_config.get("pages", []):
# get page id
inserted_page = find_page_by_path(page["path"], form_id)
Expand Down
2 changes: 2 additions & 0 deletions tasks/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ def init_salmon_fishing_fund():
name="organisation_other_names_no",
display_name="Other Name No",
destination_page_path="/organisation-address",
source_page_path="/organisation-name",
value=ConditionValue(
name="Other Name No",
conditions=[
Expand All @@ -253,6 +254,7 @@ def init_salmon_fishing_fund():
name="organisation_other_names_yes",
display_name="Other Name Yes",
destination_page_path="/organisation-alternative-names",
source_page_path="/organisation-name",
value=ConditionValue(
name="Other Name Yes",
conditions=[
Expand Down
2 changes: 2 additions & 0 deletions tests/test_form_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def test_build_conditions(input_condition, exp_result):
name="condition a",
conditions=[SubCondition(field={"name": "c1"}, operator="is", value={}, coordinator=None)],
),
source_page_path="next-a",
)
)
],
Expand All @@ -72,6 +73,7 @@ def test_build_conditions(input_condition, exp_result):
SubCondition(field={"name": "c1"}, operator="is", value={}, coordinator="or"),
],
),
source_page_path="next-a",
)
)
],
Expand Down
2 changes: 2 additions & 0 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ def test_build_form_json_no_conditions(seed_dynamic_data):
name="organisation_other_names_no",
display_name="org other names no",
destination_page_path="/summary",
source_page_path="/organisation-name",
value=ConditionValue(
name="org other names no",
conditions=[
Expand All @@ -169,6 +170,7 @@ def test_build_form_json_no_conditions(seed_dynamic_data):
name="organisation_other_names_yes",
display_name="org other names yes",
destination_page_path="/organisation-alternative-names",
source_page_path="/organisation-name",
value=ConditionValue(
name="org other names yes",
conditions=[
Expand Down
3 changes: 3 additions & 0 deletions tests/unit_test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
name="org_type_a",
display_name="org type a",
destination_page_path="/org-type-a",
source_page_path="next-a",
value=ConditionValue(
name="org type a",
conditions=[
Expand All @@ -120,6 +121,7 @@
name="org_type_b",
display_name="org type b",
destination_page_path="/org-type-b",
source_page_path="next-b",
value=ConditionValue(
name="org type b",
conditions=[
Expand All @@ -141,6 +143,7 @@
name="org_type_c",
display_name="org type c",
destination_page_path="/org-type-c",
source_page_path="next-c",
value=ConditionValue(
name="org type c",
conditions=[
Expand Down

0 comments on commit 1cc5bf0

Please sign in to comment.