Skip to content

Commit

Permalink
list error resolved
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam-W1 committed Aug 23, 2024
1 parent 974cfff commit 28a4124
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 32 deletions.
23 changes: 12 additions & 11 deletions app/export_config/generate_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,20 +135,22 @@ def build_page(page: Page = None, page_display_path: str = None) -> dict:
# Goes through the set of pages and updates the conditions and next properties to account for branching
def build_navigation(partial_form_json: dict, input_pages: list[Page]) -> dict:
for page in input_pages:
# find page in prepared output results
this_page_in_results = next(p for p in partial_form_json["pages"] if p["path"] == f"/{page.display_path}")

if page.controller and page.controller.endswith("summary.js"):
continue
next_page_id = page.default_next_page_id
if next_page_id:
find_next_page = lambda id: next(p for p in input_pages if p.page_id == id) # noqa:E731
next_page = find_next_page(next_page_id)
next_path = next_page.display_path
default_next_page = find_next_page(next_page_id)
next_path = default_next_page.display_path
# add the default next page
this_page_in_results["next"].append({"path": f"/{next_path}"})
else:
# all page paths are conditionals which will be processed later
next_path = None

# find page in prepared output results
this_page_in_results = next(p for p in partial_form_json["pages"] if p["path"] == f"/{page.display_path}")

has_conditions = False
for component in page.components:
if not component.conditions:
Expand Down Expand Up @@ -181,9 +183,6 @@ def build_navigation(partial_form_json: dict, input_pages: list[Page]) -> dict:
}
)

# If there were no conditions we just continue to the next page
if not has_conditions and next_path:
this_page_in_results["next"].append({"path": f"/{next_path}"})
if not has_conditions and not next_path:
this_page_in_results["next"].append({"path": "/summary"})

Expand All @@ -197,14 +196,16 @@ def build_lists(pages: list[dict]) -> list:
for component in page["components"]:
if component.get("list"):
list_from_db = get_list_by_id(component["metadata"]["fund_builder_list_id"])
list = {
list_dict = {
"type": list_from_db.type,
"items": list_from_db.items,
"name": list_from_db.name,
"title": list_from_db.title,
}
lists.append(list)
# Remove the metadata key from built_component (no longer needed)
# Check if the list already exists in lists by name
if not any(existing_list["name"] == list_dict["name"] for existing_list in lists):
lists.append(list_dict)
# Remove the metadata key from component (no longer needed)
component.pop("metadata", None) # The second argument prevents KeyError if 'metadata' is not found

return lists
Expand Down
35 changes: 20 additions & 15 deletions app/import_config/load_form_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,26 @@ def insert_component_as_template(component, page_id, page_index, lizts):
if component_list:
for li in lizts:
if li["name"] == component_list:
new_list = Lizt(
is_template=True,
name=li.get("name"),
title=li.get("title"),
type=li.get("type"),
items=li.get("items"),
)
try:
db.session.add(new_list)
except Exception as e:
print(e)
raise e
db.session.flush() # flush to get the list id
list_id = new_list.list_id
# Check if the list already exists
existing_list = db.session.query(Lizt).filter_by(name=li.get("name")).first()
if existing_list is None:
new_list = Lizt(
is_template=True,
name=li.get("name"),
title=li.get("title"),
type=li.get("type"),
items=li.get("items"),
)
try:
db.session.add(new_list)
except Exception as e:
print(e)
raise e
db.session.flush() # flush to get the list id
list_id = new_list.list_id
else:
# If the list already exists, you can use its ID or handle it as needed
list_id = existing_list.list_id
break

new_component = Component(
Expand Down Expand Up @@ -202,7 +208,6 @@ def read_json_from_directory(directory_path):


def load_form_jsons(override_fund_config=None):
db = app.extensions["sqlalchemy"] # Move db definition here
try:
if not override_fund_config:
db = app.extensions["sqlalchemy"]
Expand Down
5 changes: 0 additions & 5 deletions tests/test_generate_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,6 @@ def test_human_to_kebab(input, exp_output):
"type": "string",
"items": [{"text": "Hello", "value": "h"}, {"text": "Goodbye", "value": "g"}],
},
{
"name": "greetings_list",
"type": "string",
"items": [{"text": "Hello", "value": "h"}, {"text": "Goodbye", "value": "g"}],
},
],
),
],
Expand Down
2 changes: 1 addition & 1 deletion tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def test_build_form_json_with_conditions(seed_dynamic_data):

org_name_page = next((p for p in result["pages"] if p["path"] == exp_second_path), None)
assert org_name_page
assert len(org_name_page["next"]) == 2
assert len(org_name_page["next"]) == 3
assert len(org_name_page["components"]) == 2

alt_names_page = next((p for p in result["pages"] if p["path"] == "/organisation-alternative-names"), None)
Expand Down

0 comments on commit 28a4124

Please sign in to comment.