Skip to content

Commit

Permalink
precommit fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
NarenderRajuB committed Oct 18, 2024
1 parent e7005e4 commit fe08b92
Show file tree
Hide file tree
Showing 22 changed files with 121 additions and 76 deletions.
2 changes: 1 addition & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
]
}
},
}
}
2 changes: 1 addition & 1 deletion .github/workflows/copilot_deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ on:
- dev
- test
- uat

push:
# Ignore README markdown and the docs folder
# Only automatically deploy when something in the app or tests folder has changed
Expand Down
13 changes: 9 additions & 4 deletions app/blueprints/fund_builder/forms/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@
from wtforms import FileField
from wtforms import HiddenField
from wtforms import StringField
from wtforms.validators import DataRequired, Regexp
from wtforms.validators import DataRequired
from wtforms.validators import Regexp


class TemplateUploadForm(FlaskForm):
template_name = StringField("Template Name", validators=[DataRequired()])
file = FileField("Upload File", validators=[DataRequired()])


class TemplateFormForm(FlaskForm):

form_id=HiddenField()
form_id = HiddenField()

template_name = StringField(
"Template Name",
description="Name of this template, only used in FAB",
Expand All @@ -26,5 +28,8 @@ class TemplateFormForm(FlaskForm):
url_path = StringField(
"URL Path",
description="The portion of the URL that equates to the form name. Use only letters, numbers and hyphens (-)",
validators=[DataRequired(), Regexp("^[a-z0-9-]*$", message="URL Path must only contain lowercase letters, numbers and hyphens (-)")],
validators=[
DataRequired(),
Regexp("^[a-z0-9-]*$", message="URL Path must only contain lowercase letters, numbers and hyphens (-)"),
],
)
5 changes: 4 additions & 1 deletion app/blueprints/fund_builder/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,7 @@ def view_form_questions(round_id, form_id):
"view_questions.html", round=round, fund=fund, question_html=html, title=form.name_in_apply_json["en"]
)


def create_export_zip(directory_to_zip, zip_file_name) -> str:
# Output zip file path (temporary)
output_zip_path = Config.TEMP_FILE_PATH / zip_file_name
Expand All @@ -538,7 +539,9 @@ def create_export_files(round_id):
generate_config_for_round(round_id)
round_short_name = get_round_by_id(round_id).short_name

output_zip_path = create_export_zip(directory_to_zip=Config.TEMP_FILE_PATH / round_short_name, zip_file_name=round_short_name)
output_zip_path = create_export_zip(
directory_to_zip=Config.TEMP_FILE_PATH / round_short_name, zip_file_name=round_short_name
)

# Ensure the file is removed after sending it
@after_this_request
Expand Down
37 changes: 23 additions & 14 deletions app/blueprints/templates/routes.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import json

from flask import Blueprint, request
from flask import Blueprint
from flask import redirect
from flask import render_template
from flask import request
from flask import url_for
from werkzeug.utils import secure_filename

from app.blueprints.fund_builder.forms.templates import TemplateFormForm, TemplateUploadForm
from app.blueprints.fund_builder.forms.templates import TemplateFormForm
from app.blueprints.fund_builder.forms.templates import TemplateUploadForm
from app.db.models.application_config import Form
from app.db.queries.application import delete_form, get_all_template_forms, get_form_by_id, update_form
from app.db.queries.application import delete_form
from app.db.queries.application import get_all_template_forms
from app.db.queries.application import get_all_template_sections
from app.db.queries.application import get_form_by_id
from app.db.queries.application import get_form_by_template_name
from app.db.queries.application import update_form

# Blueprint for routes used by FAB PoC to manage templates
template_bp = Blueprint(
Expand Down Expand Up @@ -90,19 +95,23 @@ def view_templates():
def edit_form_template(form_id):
template_form = TemplateFormForm()
params = {
"breadcrumb_items": [
{"text": "Home", "href": url_for("build_fund_bp.index")},
{"text": "Manage Templates", "href": url_for("template_bp.view_templates")},
{"text": "Rename Template", "href": "#"}
],}
"breadcrumb_items": [
{"text": "Home", "href": url_for("build_fund_bp.index")},
{"text": "Manage Templates", "href": url_for("template_bp.view_templates")},
{"text": "Rename Template", "href": "#"},
],
}

if request.method == "POST":
if template_form.validate_on_submit():
update_form(form_id=form_id, new_form_config={
"runner_publish_name":template_form.url_path.data,
"name_in_apply_json":{"en": template_form.tasklist_name.data},
"template_name":template_form.template_name.data
})
update_form(
form_id=form_id,
new_form_config={
"runner_publish_name": template_form.url_path.data,
"name_in_apply_json": {"en": template_form.tasklist_name.data},
"template_name": template_form.template_name.data,
},
)
return redirect(url_for("template_bp.view_templates"))
params["template_form"] = template_form
return render_template("edit_form_template.html", **params)
Expand All @@ -118,5 +127,5 @@ def edit_form_template(form_id):
template_form.url_path.data = existing_form.runner_publish_name
params["template_form"] = template_form
return render_template("edit_form_template.html", **params)

return redirect(url_for("template_bp.view_templates"))
2 changes: 1 addition & 1 deletion app/blueprints/templates/templates/edit_form_template.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ <h1 class="govuk-heading-l">{{pageHeading}}</h1>
</form>
</fieldset>
</div>
{% endblock %}
{% endblock %}
34 changes: 19 additions & 15 deletions app/db/migrations/versions/~2024_10_16_1347-da30746cec39_.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,40 @@
Create Date: 2024-10-16 13:47:16.968837
"""
from alembic import op
import sqlalchemy as sa

import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = 'da30746cec39'
down_revision = 'ca61d3b746f6'
revision = "da30746cec39"
down_revision = "ca61d3b746f6"
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###

op.execute("ALTER TYPE componenttype ADD VALUE 'MULTI_INPUT_FIELD';")
with op.batch_alter_table('component', schema=None) as batch_op:
batch_op.add_column(sa.Column('children', postgresql.JSON(none_as_null=True, astext_type=sa.Text()), nullable=True))
batch_op.add_column(sa.Column('schema', postgresql.JSON(astext_type=sa.Text()), nullable=True))
with op.batch_alter_table('page', schema=None) as batch_op:
batch_op.add_column(sa.Column('options', postgresql.JSON(none_as_null=True, astext_type=sa.Text()), nullable=True))
with op.batch_alter_table("component", schema=None) as batch_op:
batch_op.add_column(
sa.Column("children", postgresql.JSON(none_as_null=True, astext_type=sa.Text()), nullable=True)
)
batch_op.add_column(sa.Column("schema", postgresql.JSON(astext_type=sa.Text()), nullable=True))
with op.batch_alter_table("page", schema=None) as batch_op:
batch_op.add_column(
sa.Column("options", postgresql.JSON(none_as_null=True, astext_type=sa.Text()), nullable=True)
)

# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('component', schema=None) as batch_op:
batch_op.drop_column('children')
batch_op.drop_column('schema')
with op.batch_alter_table('page', schema=None) as batch_op:
batch_op.drop_column('options')
with op.batch_alter_table("component", schema=None) as batch_op:
batch_op.drop_column("children")
batch_op.drop_column("schema")
with op.batch_alter_table("page", schema=None) as batch_op:
batch_op.drop_column("options")
# ### end Alembic commands ###
4 changes: 2 additions & 2 deletions app/db/models/application_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class ComponentType(Enum):
FILE_UPLOAD_FIELD = "FileUploadField"
MONTH_YEAR_FIELD = "MonthYearField"
TIME_FIELD = "TimeField"
MULTI_INPUT_FIELD="MultiInputField"
MULTI_INPUT_FIELD = "MultiInputField"


READ_ONLY_COMPONENTS = [
Expand Down Expand Up @@ -165,7 +165,7 @@ class Page(BaseModel):
)
source_template_id = Column(UUID(as_uuid=True), nullable=True)
controller = Column(String(), nullable=True)
options=Column(JSON(none_as_null=True))
options = Column(JSON(none_as_null=True))

def __repr__(self):
return f"Page(/{self.display_path} - {self.name_in_apply_json['en']}, Components: {self.components})"
Expand Down
2 changes: 1 addition & 1 deletion app/export_config/generate_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def build_component(component: Component) -> dict:
built_component.update({"values": {"type": "listRef"}})

if component.type is ComponentType.MULTI_INPUT_FIELD:
built_component.update({"children":component.children})
built_component.update({"children": component.children})

return built_component

Expand Down
4 changes: 2 additions & 2 deletions app/export_config/generate_fund_round_form_jsons.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"properties": {
"path": {"type": "string"},
"title": {"type": "string"},
"options":{"type":"object"},
"options": {"type": "object"},
"components": {
"type": "array",
"items": {
Expand All @@ -42,7 +42,7 @@
"metadata": {
"type": "object",
},
"children": {"type": "array"}
"children": {"type": "array"},
},
},
},
Expand Down
13 changes: 6 additions & 7 deletions app/export_config/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,24 @@

def write_config(config, filename, round_short_name, config_type):
# Get the directory of the current file


# Construct the path to the output directory relative to this file's location
base_output_dir = Config.TEMP_FILE_PATH / round_short_name

if config_type == "form_json":
output_dir = base_output_dir/ "form_runner"
output_dir = base_output_dir / "form_runner"
content_to_write = config
file_path = output_dir/ f"{human_to_kebab_case(filename)}.json"
file_path = output_dir / f"{human_to_kebab_case(filename)}.json"
elif config_type == "python_file":
output_dir = base_output_dir/ "fund_store"
output_dir = base_output_dir / "fund_store"
config_dict = convert_to_dict(config) # Convert config to dict for non-JSON types
content_to_write = "LOADER_CONFIG="
content_to_write += str(config_dict)
file_path = output_dir/ f"{human_to_snake_case(filename)}.py"
file_path = output_dir / f"{human_to_snake_case(filename)}.py"
elif config_type == "html":
output_dir = base_output_dir/"html"
output_dir = base_output_dir / "html"
content_to_write = config
file_path = output_dir/f"{filename}_all_questions_en.html"
file_path = output_dir / f"{filename}_all_questions_en.html"

# Ensure the output directory exists
os.makedirs(output_dir, exist_ok=True)
Expand Down
10 changes: 3 additions & 7 deletions app/import_config/load_form_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,7 @@ def _build_condition(condition_data, destination_page_path) -> Condition:

def _get_component_by_runner_name(db, runner_component_name, page_id):

return (
db.session.query(Component)
.filter(Component.runner_component_name == runner_component_name)
.first()
)
return db.session.query(Component).filter(Component.runner_component_name == runner_component_name).first()


def add_conditions_to_components(db, page: dict, conditions: dict, page_id):
Expand Down Expand Up @@ -129,7 +125,7 @@ def insert_component_as_template(component, page_id, page_index, lizts):
runner_component_name=component.get("name", None),
list_id=list_id,
children=component.get("children", None),
schema=component.get("schema", None)
schema=component.get("schema", None),
)
try:
db.session.add(new_component)
Expand All @@ -148,7 +144,7 @@ def insert_page_as_template(page, form_id):
controller=page.get("controller", None),
is_template=True,
template_name=page.get("title", None),
options = page.get("options", None),
options=page.get("options", None),
)
try:
db.session.add(new_page)
Expand Down
2 changes: 1 addition & 1 deletion config/envs/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ class DefaultConfig(object):
FORM_RUNNER_URL_REDIRECT = getenv("FORM_RUNNER_EXTERNAL_HOST", "http://localhost:3009")
SQLALCHEMY_DATABASE_URI = environ.get("DATABASE_URL")

TEMP_FILE_PATH=Path("/tmp")
TEMP_FILE_PATH = Path("/tmp")
2 changes: 1 addition & 1 deletion config/envs/development.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ class DevelopmentConfig(Config):
"DATABASE_URL",
"postgresql://postgres:password@fab-db:5432/fab", # pragma: allowlist secret
)
TEMP_FILE_PATH=Path("app") / "export_config" / "output"
TEMP_FILE_PATH = Path("app") / "export_config" / "output"
2 changes: 1 addition & 1 deletion config/envs/unit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ class UnitTestConfig(Config):
"DATABASE_URL_UNIT_TEST",
"postgresql://postgres:[email protected]:5432/fab_unit_test", # pragma: allowlist secret
)
TEMP_FILE_PATH=Path("app") / "export_config" / "output"
TEMP_FILE_PATH = Path("app") / "export_config" / "output"
6 changes: 4 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import shutil

import pytest
from flask_migrate import upgrade
from sqlalchemy import text

from app.create_app import create_app
from config import Config
from tasks.test_data import init_unit_test_data
from tasks.test_data import insert_test_data
from tests.helpers import create_test_fund
from tests.helpers import create_test_organisation
from tests.helpers import create_test_round
from config import Config
import shutil

pytest_plugins = ["fsd_test_utils.fixtures.db_fixtures"]

Expand All @@ -21,6 +22,7 @@ def temp_output_dir():
if temp_dir.exists():
shutil.rmtree(temp_dir)


@pytest.fixture
def test_fund(flask_test_client, _db, clear_test_data):
"""
Expand Down
3 changes: 1 addition & 2 deletions tests/test_config_export.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import ast
import json
import shutil
from pathlib import Path

import pytest
Expand Down Expand Up @@ -142,7 +141,7 @@ def test_generate_config_for_round_invalid_input(seed_dynamic_data):
generate_config_for_round(round_id)


def test_generate_form_jsons_for_round_valid_input(seed_dynamic_data,temp_output_dir):
def test_generate_form_jsons_for_round_valid_input(seed_dynamic_data, temp_output_dir):
# Setup: Prepare valid input parameters
round_id = seed_dynamic_data["rounds"][0].round_id
round_short_name = seed_dynamic_data["rounds"][0].short_name
Expand Down
5 changes: 2 additions & 3 deletions tests/test_config_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,10 @@ def test_import_multi_input_field(seed_dynamic_data, _db):
assert forms.count() == 1
pages = _db.session.query(Page).filter(Page.form_id == forms.first().form_id)
assert pages.count() == 3
page_with_multi_input = next(p for p in pages if p.display_path=='capital-costs-for-your-project')
page_with_multi_input = next(p for p in pages if p.display_path == "capital-costs-for-your-project")
assert page_with_multi_input
assert page_with_multi_input.options
multi_input_component = next(c for c in page_with_multi_input.components if c.title=='Capital costs')
multi_input_component = next(c for c in page_with_multi_input.components if c.title == "Capital costs")
assert multi_input_component
assert multi_input_component.type == ComponentType.MULTI_INPUT_FIELD
assert len(multi_input_component.children) == 4

2 changes: 1 addition & 1 deletion tests/test_data/multi-input-exported.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,4 @@
"outputs": [],
"skipSummary": false,
"name": "Apply for funding to save an asset in your community"
}
}
2 changes: 1 addition & 1 deletion tests/test_data/multi_input.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@
],
"section": "bgUGuD"
}

],
"lists": [],
"sections": [
Expand Down
Loading

0 comments on commit fe08b92

Please sign in to comment.