Skip to content
This repository has been archived by the owner on Dec 3, 2024. It is now read-only.

Commit

Permalink
Merge pull request #535 from communitiesuk/bau/ruff-formatting
Browse files Browse the repository at this point in the history
Apply ruff formatting to codebase before lift+shift
  • Loading branch information
samuelhwilliams authored Dec 2, 2024
2 parents 6e0005d + ec22e92 commit 49e31cb
Show file tree
Hide file tree
Showing 153 changed files with 1,174 additions and 874 deletions.
2 changes: 2 additions & 0 deletions .env.development
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ FLASK_RUN_PORT=5000
FLASK_RUN_HOST=localhost

# Below credentials belongs to local development server. Do not commit any external services credentials
# pragma: allowlist nextline secret
DATABASE_URL=postgresql://postgres:password@localhost:5432/assessment_store
APPLICATION_STORE_API_HOST=http://localhost:3002
AWS_ACCESS_KEY_ID=FSDIOSFODNN7EXAMPLE
# pragma: allowlist nextline secret
AWS_SECRET_ACCESS_KEY=fsdlrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
AWS_ENDPOINT_OVERRIDE=http://localhost:4566
AWS_REGION=eu-west-2
Expand Down
70 changes: 33 additions & 37 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,39 +1,35 @@
repos:
- repo: https://github.com/psf/black
rev: 23.10.1
hooks:
- id: black
language_version: python3
args:
- --target-version=py310
- repo: https://github.com/PyCQA/flake8
rev: 7.0.0
- repo: https://github.com/PyCQA/docformatter
# We are pinning a specific hash here because the current latest released version (v1.7.5) is
# incompatible with pre-commit v4+
# issue: https://github.com/PyCQA/docformatter/issues/289
rev: eb1df347edd128b30cd3368dddc3aa65edcfac38
hooks:
- id: flake8
additional_dependencies: [Flake8-pyproject]
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-ast
- repo: https://github.com/asottile/reorder-python-imports
rev: v3.12.0
hooks:
- id: reorder-python-imports
name: Reorder Python imports (src, tests)
args: ["--application-directories", "src"]
- repo: https://github.com/PyCQA/docformatter
# We are pinning a specific hash here because the current latest released version (v1.7.5) is
# incompatible with pre-commit v4+
# issue: https://github.com/PyCQA/docformatter/issues/289
rev: eb1df347edd128b30cd3368dddc3aa65edcfac38
hooks:
- id: docformatter
additional_dependencies: [tomli]
- repo: https://github.com/Yelp/detect-secrets
rev: v1.4.0
hooks:
- id: detect-secrets
args: ['--disable-plugin', 'HexHighEntropyString']
exclude: .env.development
- id: docformatter
additional_dependencies: [tomli]
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-ast
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version. If bumping this, please also bump requirements-dev.in
rev: v0.7.4
hooks:
# Run the linter.
- id: ruff
args: [--fix]
# Run the formatter.
- id: ruff-format
- repo: https://github.com/Yelp/detect-secrets
rev: v1.4.0
hooks:
- id: detect-secrets
args:
[
"--disable-plugin",
"HexHighEntropyString",
"--disable-plugin",
"Base64HighEntropyString",
]
21 changes: 15 additions & 6 deletions _helpers/task_executer_service.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import json
import threading

from fsd_utils.sqs_scheduler.task_executer_service import TaskExecutorService

from config.mappings.assessment_mapping_fund_round import fund_round_data_key_mappings
from db.queries import bulk_insert_application_record
from fsd_utils.sqs_scheduler.task_executer_service import TaskExecutorService


class AssessmentTaskExecutorService(TaskExecutorService):
Expand All @@ -12,21 +13,29 @@ def message_executor(self, message):
the GOV notify service to send emails :param message Json message."""
current_thread = threading.current_thread()
thread_id = f"[{current_thread.name}:{current_thread.ident}]"
self.logger.info(f"[{thread_id}] Notification Triggered")
self.logger.info("[{thread_id}] Notification Triggered", extra=dict(thread_id=thread_id))
message_id = message["sqs"]["MessageId"]
try:
application_json = json.loads(message["s3"])
application_json_list = []
fund_round_shortname = "".join(application_json["reference"].split("-")[:2])
# Check if the import config exists for the application
if fund_round_shortname not in fund_round_data_key_mappings.keys():
self.logger.warning(f"Missing import config for the application: {application_json['reference']}.")
self.logger.warning(
"Missing import config for the application: {reference}.",
extra=dict(reference=application_json["reference"]),
)
return message

application_json_list.append(application_json)
bulk_insert_application_record(application_json_list, is_json=True)
self.logger.info(f"{thread_id} Processed the message: {message_id}")
self.logger.info(
"{thread_id} Processed the message: {message_id}",
extra=dict(thread_id=thread_id, message_id=message_id),
)
return message

except Exception as e:
self.logger.error(f"An error occurred while processing the message {message_id}", e)
except Exception:
self.logger.exception(
"An error occurred while processing the message {message_id}", extra=dict(message_id=message_id)
)
1 change: 0 additions & 1 deletion api/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
"""
.. include:: ./README.md
"""
# flake8: noqa
17 changes: 11 additions & 6 deletions api/models/notification.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import json
from uuid import uuid4

from config import Config
from flask import current_app

from config import Config

NOTIFICATION_CONST = "notification"
NOTIFICATION_S3_KEY_CONST = "application/notification"

Expand All @@ -30,7 +31,10 @@ def send(template_type: str, to_email: str, full_name: str, content: dict):
"full_name": full_name,
"content": content,
}
current_app.logger.info(f" json payload '{template_type}' to '{to_email}'.")
current_app.logger.info(
" json payload '{template_type}' to '{to_email}'.",
extra=dict(template_type=template_type, to_email=to_email),
)
try:
sqs_extended_client = Notification._get_sqs_client()
message_id = sqs_extended_client.submit_single_message(
Expand All @@ -45,11 +49,12 @@ def send(template_type: str, to_email: str, full_name: str, content: dict):
},
},
)
current_app.logger.info(f"Message sent to SQS queue and message id is [{message_id}]")
current_app.logger.info(
"Message sent to SQS queue and message id is [{message_id}]", extra=dict(message_id=message_id)
)
return message_id
except Exception as e:
current_app.logger.error("An error occurred while sending message")
current_app.logger.error(e)
except Exception:
current_app.logger.exception("An error occurred while sending message")

@staticmethod
def _get_sqs_client():
Expand Down
3 changes: 2 additions & 1 deletion api/models/sub_criteria.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
import inspect
from dataclasses import dataclass

from api.models.theme import Theme
from dataclass_dict_convert import dataclass_dict_convert

from api.models.theme import Theme


@dataclass_dict_convert()
@dataclass()
Expand Down
3 changes: 2 additions & 1 deletion api/models/theme.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
from dataclasses import dataclass
from typing import List

from api.models.answer import Answer
from dataclass_dict_convert import dataclass_dict_convert

from api.models.answer import Answer


@dataclass_dict_convert()
@dataclass()
Expand Down
77 changes: 41 additions & 36 deletions api/routes/__init__.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,44 @@
from .assessment_routes import all_assessments_for_fund_round_id
from .assessment_routes import assessment_metadata_for_application_id
from .assessment_routes import assessment_stats_for_fund_round_id
from .assessment_routes import assessment_stats_for_multiple_round_ids
from .assessment_routes import get_application_data_for_export
from .assessment_routes import get_application_json
from .assessment_routes import get_application_json_and_sub_criterias
from .assessment_routes import get_assessor_task_list_state
from .assessment_routes import get_banner_state
from .assessment_routes import sub_criteria
from .assessment_routes import update_ar_status_to_completed
from .comment_routes import get_comments
from .comment_routes import post_comments
from .comment_routes import put_comments
from .flag_routes import create_flag_for_application
from .flag_routes import get_all_flags_for_application
from .flag_routes import get_flag
from .flag_routes import get_team_flag_stats
from .flag_routes import update_flag_for_application
from .progress_routes import get_progress_for_applications
from .progress_routes import post_progress_for_applications
from .qa_complete_routes import post_qa_complete_for_application
from .qa_complete_routes import qa_complete_record_for_application
from .score_routes import get_score_for_application_sub_criteria
from .score_routes import get_scoring_system_name_for_round_id
from .score_routes import post_score_for_application_sub_criteria
from .tag_routes import add_tag_for_fund_round
from .tag_routes import associate_tags_with_assessment
from .tag_routes import get_active_tags_associated_with_assessment
from .tag_routes import get_tag
from .tag_routes import get_tags_for_fund_round
from .user_routes import add_user_application_association
from .user_routes import get_all_users_associated_with_application
from .user_routes import get_user_application_association
from .user_routes import update_user_application_association

from .assessment_routes import (
all_assessments_for_fund_round_id,
assessment_metadata_for_application_id,
assessment_stats_for_fund_round_id,
assessment_stats_for_multiple_round_ids,
get_application_data_for_export,
get_application_json,
get_application_json_and_sub_criterias,
get_assessor_task_list_state,
get_banner_state,
sub_criteria,
update_ar_status_to_completed,
)
from .comment_routes import get_comments, post_comments, put_comments
from .flag_routes import (
create_flag_for_application,
get_all_flags_for_application,
get_flag,
get_team_flag_stats,
update_flag_for_application,
)
from .progress_routes import get_progress_for_applications, post_progress_for_applications
from .qa_complete_routes import post_qa_complete_for_application, qa_complete_record_for_application
from .score_routes import (
get_score_for_application_sub_criteria,
get_scoring_system_name_for_round_id,
post_score_for_application_sub_criteria,
)
from .tag_routes import (
add_tag_for_fund_round,
associate_tags_with_assessment,
get_active_tags_associated_with_assessment,
get_tag,
get_tags_for_fund_round,
)
from .user_routes import (
add_user_application_association,
get_all_users_associated_with_application,
get_user_application_association,
update_user_application_association,
)

__all__ = [
"all_assessments_for_fund_round_id",
Expand Down
4 changes: 2 additions & 2 deletions api/routes/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
import json
from uuid import UUID

from flask import current_app, make_response

from config import Config
from db.models.assessment_record.enums import Status
from flask import current_app
from flask import make_response


def compress_response(data):
Expand Down
31 changes: 14 additions & 17 deletions api/routes/assessment_routes.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
# flake8: noqa
import copy
from typing import Dict
from typing import List
from typing import Dict, List

from flask import current_app, request

from api.models.sub_criteria import SubCriteria
from api.routes._helpers import compress_response
from api.routes._helpers import transform_to_assessor_task_list_metadata
from api.routes._helpers import compress_response, transform_to_assessor_task_list_metadata
from api.routes.subcriterias.get_sub_criteria import (
get_all_subcriteria,
)
from api.routes.subcriterias.get_sub_criteria import (
return_subcriteria_from_mapping,
)
from config import Config
Expand All @@ -18,22 +15,19 @@
)
from db.models.flags.flag_update import FlagStatus
from db.queries import get_metadata_for_fund_round_id
from db.queries.assessment_records.queries import find_assessor_task_list_state
from db.queries.assessment_records.queries import get_application_jsonb_blob
from db.queries.assessment_records.queries import get_assessment_export_data
from db.queries.assessment_records.queries import (
find_assessor_task_list_state,
get_application_jsonb_blob,
get_assessment_export_data,
get_assessment_sub_critera_state,
get_metadata_for_application,
update_status_to_completed,
)
from db.queries.assessment_records.queries import get_metadata_for_application
from db.queries.assessment_records.queries import update_status_to_completed
from db.queries.comments.queries import get_sub_criteria_to_has_comment_map
from db.queries.qa_complete.queries import (
get_qa_complete_record_for_application,
)
from db.queries.scores.queries import get_scoring_system_for_round_id
from db.queries.scores.queries import get_sub_criteria_to_latest_score_map
from flask import current_app
from flask import request
from db.queries.scores.queries import get_scoring_system_for_round_id, get_sub_criteria_to_latest_score_map


def calculate_overall_score_percentage_for_application(application):
Expand All @@ -45,7 +39,10 @@ def calculate_overall_score_percentage_for_application(application):
highest_possible_weighted_score_for_round = 0
if mapping["scored_criteria"] == []:
# We have no scoring config for this round (possibly an EOI)
current_app.logger.info(f"No scoring config found for {application['fund_id']}:{application['round_id']}")
current_app.logger.info(
"No scoring config found for {fund_id}:{round_id}",
extra=dict(fund_id=application["fund_id"], round_id=application["round_id"]),
)
return None

# Combine mapping and highest possible score calculation
Expand Down
5 changes: 2 additions & 3 deletions api/routes/comment_routes.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from typing import Dict

from db.queries.comments import create_comment
from db.queries.comments import get_comments_from_db
from db.queries.comments import update_comment
from flask import request

from db.queries.comments import create_comment, get_comments_from_db, update_comment


def get_comments(
application_id: str = None,
Expand Down
Loading

0 comments on commit 49e31cb

Please sign in to comment.