Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BAU - Replace black, flake8, isort with ruff #151

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 6 additions & 15 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,25 +1,16 @@
repos:
- repo: https://github.com/psf/black
rev: 24.3.0
hooks:
- id: black
language_version: python3
- repo: https://github.com/PyCQA/flake8
rev: 7.0.0
hooks:
- id: flake8
additional_dependencies: [Flake8-pyproject]
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
rev: v4.6.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-ast
- repo: https://github.com/pycqa/isort
rev: 5.13.2
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.8.3
hooks:
- id: isort
name: isort (python)
- id: ruff
args: [--fix]
- id: ruff-format
- repo: https://github.com/Yelp/detect-secrets
rev: v1.4.0
hooks:
Expand Down
33 changes: 21 additions & 12 deletions app/all_questions/metadata_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
import fnmatch
from typing import Tuple

from bs4 import BeautifulSoup
from bs4 import NavigableString
from bs4 import BeautifulSoup, NavigableString

from app.all_questions.read_forms import build_section_header
from app.all_questions.read_forms import determine_display_value_for_condition
from app.all_questions.read_forms import determine_if_just_html_page
from app.all_questions.read_forms import increment_lowest_in_hierarchy
from app.all_questions.read_forms import remove_lowest_in_hierarchy
from app.all_questions.read_forms import strip_leading_numbers
from app.all_questions.read_forms import (
build_section_header,
determine_display_value_for_condition,
determine_if_just_html_page,
increment_lowest_in_hierarchy,
remove_lowest_in_hierarchy,
strip_leading_numbers,
)

FIELD_TYPES_WITH_MAX_WORDS = ["freetextfield", "multilinetextfield"]

Expand Down Expand Up @@ -249,7 +250,7 @@ def update_wording_for_multi_input_fields(text: list) -> list:
def determine_title_and_text_for_component(
component: dict,
include_html_components: bool = True,
form_lists: list = [],
form_lists: list = None,
is_child: bool = False,
) -> Tuple[str, list]:
"""Determines the title and text to display for an individual component.
Expand All @@ -265,6 +266,8 @@ def determine_title_and_text_for_component(
Returns:
Tuple[str, list]: First item is the title, second is the text to display
"""
if form_lists is None:
form_lists = []
title: str = component["title"] if "title" in component else None
text = []
# skip details, eg about-your-org-cyp GNpQfE
Expand Down Expand Up @@ -319,9 +322,9 @@ def determine_title_and_text_for_component(
def build_components_from_page(
full_page_json: dict,
include_html_components: bool = True,
form_lists: list = [],
form_conditions: list = [],
index_of_printed_headers: dict = {},
form_lists: list = None,
form_conditions: list = None,
index_of_printed_headers: dict = None,
lang: str = "en",
) -> list:
"""Builds a list of the components to display from this page, including their title and text, and
Expand All @@ -348,6 +351,12 @@ def build_components_from_page(
```
"""
# Find out which components in this page determine, through conditions, where we go next
if index_of_printed_headers is None:
index_of_printed_headers = {}
if form_conditions is None:
form_conditions = []
if form_lists is None:
form_lists = []
components_with_conditions = []
for condition in form_conditions:
components_with_conditions.extend([value["field"]["name"] for value in condition["value"]["conditions"]])
Expand Down
4 changes: 3 additions & 1 deletion app/all_questions/read_forms.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
def determine_display_value_for_condition(
condition_value: str,
list_name: str = None,
form_lists: list[dict] = [],
form_lists: list[dict] = None,
lang: str = "en",
) -> str:
"""Determines the display value for the given condition string - either translating true/false into
Expand All @@ -17,6 +17,8 @@ def determine_display_value_for_condition(
Returns:
str: The display value
"""
if form_lists is None:
form_lists = []
if condition_value.casefold() == "true":
return "Yes" if lang == "en" else "Ydy"
elif condition_value.casefold() == "false":
Expand Down
24 changes: 11 additions & 13 deletions app/blueprints/dev/routes.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
from flask import Blueprint
from flask import current_app
from flask import redirect
from flask import request
from flask import url_for

from app.blueprints.self_serve.data.data_access import clear_all_responses
from app.blueprints.self_serve.data.data_access import get_all_components
from app.blueprints.self_serve.data.data_access import get_all_pages
from app.blueprints.self_serve.data.data_access import get_all_sections
from app.blueprints.self_serve.data.data_access import get_responses
from app.blueprints.self_serve.data.data_access import get_saved_forms
from app.blueprints.self_serve.data.data_access import save_response
from flask import Blueprint, current_app, redirect, request, url_for

from app.blueprints.self_serve.data.data_access import (
clear_all_responses,
get_all_components,
get_all_pages,
get_all_sections,
get_responses,
get_saved_forms,
save_response,
)

# Blueprint for dev related routes, eg. saving responses from when the form is in preview mode in the form runner
dev_bp = Blueprint(
Expand Down Expand Up @@ -58,7 +56,7 @@
updated_form = save_response(form_dict=form_dict)
return updated_form, 201


Check warning

Code scanning / CodeQL

Reflected server-side cross-site scripting Medium

Cross-site scripting vulnerability due to a
user-provided value
.
# ====================================================================================================
# Functions below this line are used by the original prototype version (drag and drop) before being
# updated to use the database
Expand Down
8 changes: 2 additions & 6 deletions app/blueprints/fund_builder/forms/fund.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
from enum import Enum

from flask_wtf import FlaskForm
from wtforms import HiddenField
from wtforms import RadioField
from wtforms import StringField
from wtforms import TextAreaField
from wtforms.validators import DataRequired, ValidationError
from wtforms.validators import Length
from wtforms import HiddenField, RadioField, StringField, TextAreaField
from wtforms.validators import DataRequired, Length, ValidationError

from app.db.models.fund import FundingType
from app.db.queries.fund import get_fund_by_short_name
Expand Down
15 changes: 3 additions & 12 deletions app/blueprints/fund_builder/forms/round.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,9 @@
import json
import re

from flask_wtf import FlaskForm
from flask_wtf import Form
from wtforms import FormField
from wtforms import HiddenField
from wtforms import RadioField
from wtforms import StringField
from wtforms import TextAreaField
from wtforms import URLField
from wtforms.validators import DataRequired
from wtforms.validators import Length
from wtforms.validators import ValidationError
from flask_wtf import FlaskForm, Form
from wtforms import FormField, HiddenField, RadioField, StringField, TextAreaField, URLField
from wtforms.validators import DataRequired, Length, ValidationError

from app.db.queries.fund import get_fund_by_id
from app.db.queries.round import get_round_by_short_name_and_fund_id
Expand Down Expand Up @@ -128,7 +120,6 @@ def validate_minute(self, field):


class RoundForm(FlaskForm):

JSON_FIELD_HINT = "Valid json format, using double quotes, lowercase true/false"

round_id = HiddenField("Round ID")
Expand Down
3 changes: 1 addition & 2 deletions app/blueprints/fund_builder/forms/section.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from flask_wtf import FlaskForm
from wtforms import HiddenField
from wtforms import StringField
from wtforms import HiddenField, StringField
from wtforms.validators import DataRequired


Expand Down
8 changes: 2 additions & 6 deletions app/blueprints/fund_builder/forms/templates.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
from flask_wtf import FlaskForm
from wtforms import FileField
from wtforms import HiddenField
from wtforms import StringField
from wtforms.validators import DataRequired
from wtforms.validators import Regexp
from wtforms import FileField, HiddenField, StringField
from wtforms.validators import DataRequired, Regexp


class TemplateUploadForm(FlaskForm):
Expand All @@ -12,7 +9,6 @@ class TemplateUploadForm(FlaskForm):


class TemplateFormForm(FlaskForm):

form_id = HiddenField()

template_name = StringField(
Expand Down
69 changes: 34 additions & 35 deletions app/blueprints/fund_builder/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,43 @@
from random import randint

import requests
from flask import Blueprint
from flask import Response
from flask import after_this_request
from flask import flash
from flask import g
from flask import redirect
from flask import render_template
from flask import request
from flask import send_file
from flask import url_for
from flask import (
Blueprint,
Response,
after_this_request,
flash,
g,
redirect,
render_template,
request,
send_file,
url_for,
)
from fsd_utils.authentication.decorators import login_requested

from app.all_questions.metadata_utils import generate_print_data_for_sections
from app.blueprints.fund_builder.forms.fund import FundForm
from app.blueprints.fund_builder.forms.round import RoundForm
from app.blueprints.fund_builder.forms.round import get_datetime
from app.blueprints.fund_builder.forms.round import RoundForm, get_datetime
from app.blueprints.fund_builder.forms.section import SectionForm
from app.db.models.fund import Fund
from app.db.models.fund import FundingType
from app.db.models.fund import Fund, FundingType
from app.db.models.round import Round
from app.db.queries.application import clone_single_form
from app.db.queries.application import clone_single_round
from app.db.queries.application import delete_form_from_section
from app.db.queries.application import delete_section_from_round
from app.db.queries.application import get_all_template_forms
from app.db.queries.application import get_form_by_id
from app.db.queries.application import get_section_by_id
from app.db.queries.application import insert_new_section
from app.db.queries.application import move_form_down
from app.db.queries.application import move_form_up
from app.db.queries.application import move_section_down
from app.db.queries.application import move_section_up
from app.db.queries.application import update_section
from app.db.queries.fund import add_fund
from app.db.queries.fund import get_all_funds
from app.db.queries.fund import get_fund_by_id
from app.db.queries.fund import update_fund
from app.db.queries.round import add_round
from app.db.queries.round import get_round_by_id
from app.db.queries.round import update_round
from app.db.queries.application import (
clone_single_form,
clone_single_round,
delete_form_from_section,
delete_section_from_round,
get_all_template_forms,
get_form_by_id,
get_section_by_id,
insert_new_section,
move_form_down,
move_form_up,
move_section_down,
move_section_up,
update_section,
)
from app.db.queries.fund import add_fund, get_all_funds, get_fund_by_id, update_fund
from app.db.queries.round import add_round, get_round_by_id, update_round
from app.export_config.generate_all_questions import print_html
from app.export_config.generate_assessment_config import (
generate_assessment_config_for_round,
Expand Down Expand Up @@ -220,7 +217,9 @@ def build_application(round_id):
@build_fund_bp.route("/fund/<fund_id>/round/<round_id>/clone")
def clone_round(round_id, fund_id):
cloned = clone_single_round(
round_id=round_id, new_fund_id=fund_id, new_short_name=f"R-C{randint(0, 999)}" # nosec B311
round_id=round_id,
new_fund_id=fund_id,
new_short_name=f"R-C{randint(0, 999)}", # nosec B311
)
flash(f"Cloned new round: {cloned.short_name}")

Expand Down
6 changes: 1 addition & 5 deletions app/blueprints/self_serve/data/data_access.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
from app.blueprints.self_serve.data.not_a_db import COMPONENTS
from app.blueprints.self_serve.data.not_a_db import FORMS
from app.blueprints.self_serve.data.not_a_db import LISTS
from app.blueprints.self_serve.data.not_a_db import PAGES
from app.blueprints.self_serve.data.not_a_db import SECTIONS
from app.blueprints.self_serve.data.not_a_db import COMPONENTS, FORMS, LISTS, PAGES, SECTIONS

saved_responses = []
saved_sections = {}
Expand Down
1 change: 0 additions & 1 deletion app/blueprints/self_serve/data/not_a_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,6 @@
"component_names": [],
"show_in_builder": False,
"controller": "./pages/summary.js",
"show_in_builder": False,
},
{
"id": "alternative-organisation-name",
Expand Down
5 changes: 1 addition & 4 deletions app/blueprints/self_serve/forms/form_form.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
from flask_wtf import FlaskForm
from wtforms import HiddenField
from wtforms import SelectMultipleField
from wtforms import StringField
from wtforms import HiddenField, SelectMultipleField, StringField
from wtforms.validators import InputRequired


class FormForm(FlaskForm):

id = HiddenField()
builder_display_name = StringField(
label="Display Name in this tool",
Expand Down
5 changes: 1 addition & 4 deletions app/blueprints/self_serve/forms/page_form.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
from flask_wtf import FlaskForm
from wtforms import BooleanField
from wtforms import SelectMultipleField
from wtforms import StringField
from wtforms import BooleanField, SelectMultipleField, StringField
from wtforms.validators import InputRequired


class PageForm(FlaskForm):

id = StringField(
label="Page ID",
validators=[InputRequired(message="Supply a unique ID")],
Expand Down
7 changes: 2 additions & 5 deletions app/blueprints/self_serve/forms/question_form.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from flask_wtf import FlaskForm
from wtforms import RadioField
from wtforms import StringField
from wtforms.validators import InputRequired
from wtforms.validators import Optional
from wtforms import RadioField, StringField
from wtforms.validators import InputRequired, Optional
from wtforms.widgets import RadioInput


Expand All @@ -15,7 +13,6 @@ def __init__(self, *args, **kwargs):


class QuestionForm(FlaskForm):

id = StringField(
label="Question ID",
validators=[InputRequired(message="Supply a unique ID")],
Expand Down
5 changes: 1 addition & 4 deletions app/blueprints/self_serve/forms/section_form.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
from flask_wtf import FlaskForm
from wtforms import IntegerField
from wtforms import SelectMultipleField
from wtforms import StringField
from wtforms import IntegerField, SelectMultipleField, StringField
from wtforms.validators import InputRequired


class SectionForm(FlaskForm):

id = StringField(
label="Section ID",
validators=[InputRequired(message="Supply a unique ID")],
Expand Down
Loading
Loading