Skip to content

Commit

Permalink
generalization of all questions page
Browse files Browse the repository at this point in the history
  • Loading branch information
nuwan-samarasinghe committed Feb 6, 2025
1 parent 3410533 commit 5ce3a0d
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 39 deletions.
10 changes: 8 additions & 2 deletions app/blueprints/application/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ def view_all_questions(round_id):
fund=fund,
question_html=html,
title=f"All Questions for {fund.short_name} - {round.short_name}",
all_questions_view=True
)


Expand Down Expand Up @@ -263,7 +264,12 @@ def view_form_questions(round_id, section_id, form_id):
section_data,
lang="en",
)
html = generate_html(print_data, True)
html = generate_html(print_data, False)
return render_template(
"view_questions.html", round=round, fund=fund, question_html=html, title=form.name_in_apply_json["en"]
"view_questions.html",
round=round,
fund=fund,
question_html=html,
title=form.name_in_apply_json["en"],
all_questions_view=False
)
19 changes: 12 additions & 7 deletions app/blueprints/application/templates/view_questions.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% extends "base.html" %}
{% from "govuk_frontend_jinja/components/back-link/macro.html" import govukBackLink %}

{% set pageHeading %}{{title}} {% endset %}
{% set pageHeading %}{{ title }} {% endset %}
{% block beforeContent %}
{{ super() }}
<div class="govuk-grid-row">
Expand All @@ -14,11 +14,16 @@
</div>
{% endblock beforeContent %}
{% block content %}
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<span class="govuk-caption-l">[Preview] Application questions</span>
<h1 class="govuk-heading-xl">{{pageHeading}}</h1>
{{ question_html|safe}}
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
{% if all_questions_view %}
<span class="govuk-caption-l">[Preview] Application questions</span>
<h1 class="govuk-heading-xl">{{ pageHeading }}</h1>
{% else %}
<h1 class="govuk-heading-l govuk-!-margin-bottom-0">{{ pageHeading }}</h1>
<p class="govuk-body govuk-!-margin-bottom-9">This template contains the following questions.</p>
{% endif %}
{{ question_html | safe }}
</div>
</div>
</div>
{% endblock %}
2 changes: 1 addition & 1 deletion app/blueprints/template/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def template_questions(form_id):
section_data,
lang="en",
)
html = generate_html(print_data, False, False)
html = generate_html(print_data, False)
return render_template("view_template_questions.html", question_html=html, form=form)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
{% block content %}
<div class="govuk-grid-row">
<div class="govuk-grid-column-full">
<h1 class="govuk-heading-l">{{ form.template_name }}</h1>
<p class="govuk-body">This template contains the following questions.</p>
<h1 class="govuk-heading-l govuk-!-margin-bottom-0">{{ form.template_name }}</h1>
<p class="govuk-body govuk-!-margin-bottom-9">This template contains the following questions.</p>
</div>
</div>
<div class="govuk-grid-row">
Expand Down
51 changes: 26 additions & 25 deletions app/export_config/generate_all_questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def generate_table_of_contents(air, sections):
# --------------------------
# Component Rendering Section
# --------------------------
def render_components(air, components, show_field_types=False):
def render_components(air, components):
"""
Renders components within a page.
Expand All @@ -65,29 +65,29 @@ def render_components(air, components, show_field_types=False):
</div>
"""
for component in components:
with air.div(klass="govuk-body all-questions-component"):
title = component.get("title")
if not component.get("hide_title") and title:
with air.p(klass="govuk-body"):
air(title)
if show_field_types:
air(f" [{component['type']}]")

for text in component.get("text", []):
if isinstance(text, list):
with air.ul(klass="govuk-list govuk-list--bullet"):
for bullet in text:
with air.li():
air(bullet)
else:
with air.p(klass="govuk-body"):
air(text)
title = component.get("title")
text_list = component.get("text", [])

if (title and not component.get("hide_title")) or text_list:
with air.div(klass="govuk-body"):
if title and not component.get("hide_title"):
with air.p(klass="govuk-body"):
air(title)

for text in text_list:
if isinstance(text, list):
with air.ul(klass="govuk-list govuk-list--bullet"):
for bullet in text:
with air.li():
air(bullet)
else:
with air.p(klass="govuk-body"):
air(text)

# --------------------------
# Main HTML Generation Section
# --------------------------
def generate_html(sections, show_field_types=False, allow_table_of_content=True):
def generate_html(sections, all_question_view=True):
"""
Generates an HTML document for the given sections.
Expand All @@ -100,17 +100,17 @@ def generate_html(sections, show_field_types=False, allow_table_of_content=True)
"""
air = Airium()
with air.div(klass="govuk-!-margin-bottom-8"):
if allow_table_of_content:
if all_question_view:
generate_table_of_contents(air, sections)
air.hr(klass="govuk-section-break govuk-section-break--l govuk-section-break--visible")

for idx, (anchor, details) in enumerate(sections.items(), start=1):
if anchor == "assessment_display_info":
continue

air.hr(klass="govuk-section-break govuk-section-break--l govuk-section-break--visible")

with air.h2(klass="govuk-heading-l", id=anchor):
air(f"{idx}. {details['title_text']}")
if all_question_view:
with air.h2(klass="govuk-heading-l", id=anchor):
air(f"{idx}. {details['title_text']}")

for heading, header_info in sorted(details["form_print_data"].items(),
key=lambda item: str(item[1]["heading_number"])):
Expand All @@ -120,6 +120,7 @@ def generate_html(sections, show_field_types=False, allow_table_of_content=True)
with getattr(air, tag)(klass=f"govuk-heading-{'m' if tag == 'h3' else 's'}"):
air(heading_text)

render_components(air, header_info["components"], show_field_types)
render_components(air, header_info["components"])
air.hr(klass="govuk-section-break govuk-section-break--l govuk-section-break--visible")

return str(air)
10 changes: 8 additions & 2 deletions tests/test_config_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
generate_all_round_html,
)
from app.export_config.helpers import validate_json
from bs4 import BeautifulSoup


def read_data_from_output_file(file):
Expand Down Expand Up @@ -268,7 +269,7 @@ def test_generate_fund_round_html(seed_dynamic_data, temp_output_dir):
/ "html"
/ f"{fund_short_name.casefold()}_{round_short_name.casefold()}_all_questions_en.html",
"expected_output": frontend_html_prefix
+ '<div class="govuk-!-margin-bottom-8">\n <h2 class="govuk-heading-m">\n Table of contents\n </h2>\n <ol class="govuk-list govuk-list--number">\n <li>\n <a class="govuk-link" href="#organisation-information">\n Organisation Information\n </a>\n </li>\n </ol>\n <hr class="govuk-section-break govuk-section-break--l govuk-section-break--visible" />\n <h2 class="govuk-heading-l" id="organisation-information">\n 1. Organisation Information\n </h2>\n <h3 class="govuk-heading-m">\n About your organisation\n </h3>\n <h4 class="govuk-heading-s">\n Organisation Name\n </h4>\n <div class="govuk-body all-questions-component">\n <p class="govuk-body">\n What is your organisation\'s name?\n </p>\n <p class="govuk-body">\n This must match the registered legal organisation name\n </p>\n </div>\n <div class="govuk-body all-questions-component">\n <p class="govuk-body">\n How is your organisation classified?\n </p>\n <ul class="govuk-list govuk-list--bullet">\n <li>\n Charity\n </li>\n <li>\n Public Limited Company\n </li>\n </ul>\n </div>\n</div>' # noqa: E501
+ '<div class="govuk-!-margin-bottom-8">\n <h2 class="govuk-heading-m">Table of contents</h2>\n <ol class="govuk-list govuk-list--number">\n <li><a class="govuk-link" href="#organisation-information">Organisation Information</a></li>\n </ol>\n <hr class="govuk-section-break govuk-section-break--l govuk-section-break--visible" />\n <h2 class="govuk-heading-l" id="organisation-information">1. Organisation Information</h2>\n <h3 class="govuk-heading-m">About your organisation</h3>\n <hr class="govuk-section-break govuk-section-break--l govuk-section-break--visible" />\n <h4 class="govuk-heading-s">Organisation Name</h4>\n <div class="govuk-body">\n <p class="govuk-body">What is your organisation\'s name?</p>\n <p class="govuk-body">This must match the registered legal organisation name</p>\n </div>\n <div class="govuk-body">\n <p class="govuk-body">How is your organisation classified?</p>\n <ul class="govuk-list govuk-list--bullet">\n <li>Charity</li>\n <li>Public Limited Company</li>\n </ul>\n </div>\n <hr class="govuk-section-break govuk-section-break--l govuk-section-break--visible" />\n</div>' # noqa: E501
+ frontend_html_suffix,
}
]
Expand All @@ -278,7 +279,7 @@ def test_generate_fund_round_html(seed_dynamic_data, temp_output_dir):

with open(expected_file["path"], "r") as file:
data = file.read()
assert data == expected_file["expected_output"]
assert _normalize_html(data) == _normalize_html(expected_file["expected_output"]), "HTML outputs do not match"


def test_generate_fund_round_html_invalid_input(seed_dynamic_data):
Expand Down Expand Up @@ -328,3 +329,8 @@ def test_create_export_zip(temp_output_dir):
assert output
output_path = Path(output)
assert output_path.exists()


def _normalize_html(html):
"""Parses and normalizes HTML using BeautifulSoup to avoid formatting differences."""
return BeautifulSoup(html, "html.parser").prettify()

0 comments on commit 5ce3a0d

Please sign in to comment.