From dd9b5725da5f37b0a32bc6e093a12ff085b735fd Mon Sep 17 00:00:00 2001 From: dinesh Date: Thu, 26 Dec 2024 15:43:22 -0800 Subject: [PATCH] options apis for requirement --- .../src/compliance_api/resources/__init__.py | 4 ++ .../resources/compliance_finding.py | 53 +++++++++++++++++++ .../resources/enforcement_action.py | 53 +++++++++++++++++++ .../resources/requirement_source.py | 4 +- .../src/compliance_api/services/__init__.py | 2 + .../services/compliance_finding.py | 12 +++++ .../services/enforcement_action.py | 12 +++++ .../api/test_compliance_finding.py | 14 +++++ .../api/test_enforcement_action.py | 14 +++++ 9 files changed, 166 insertions(+), 2 deletions(-) create mode 100644 compliance-api/src/compliance_api/resources/compliance_finding.py create mode 100644 compliance-api/src/compliance_api/resources/enforcement_action.py create mode 100644 compliance-api/src/compliance_api/services/compliance_finding.py create mode 100644 compliance-api/src/compliance_api/services/enforcement_action.py create mode 100644 compliance-api/tests/integration/api/test_compliance_finding.py create mode 100644 compliance-api/tests/integration/api/test_enforcement_action.py diff --git a/compliance-api/src/compliance_api/resources/__init__.py b/compliance-api/src/compliance_api/resources/__init__.py index 0fdedb9f..71bc6dea 100644 --- a/compliance-api/src/compliance_api/resources/__init__.py +++ b/compliance-api/src/compliance_api/resources/__init__.py @@ -36,6 +36,8 @@ from .requirement_source import API as REQUIREMENT_SOURCE_API from .staff_user import API as USER_API from .topic import API as TOPIC_API +from .enforcement_action import API as ENFORCEMENT_ACTION_API +from .compliance_finding import API as COMPLIANCE_FINDING_API __all__ = ("API_BLUEPRINT", "OPS_BLUEPRINT") @@ -81,3 +83,5 @@ API.add_namespace(COMPLAINT_API) API.add_namespace(REQUIREMENT_SOURCE_API) API.add_namespace(CONTINUATION_REPORT_API) +API.add_namespace(ENFORCEMENT_ACTION_API) +API.add_namespace(COMPLIANCE_FINDING_API) diff --git a/compliance-api/src/compliance_api/resources/compliance_finding.py b/compliance-api/src/compliance_api/resources/compliance_finding.py new file mode 100644 index 00000000..8fb50b95 --- /dev/null +++ b/compliance-api/src/compliance_api/resources/compliance_finding.py @@ -0,0 +1,53 @@ +# Copyright © 2024 Province of British Columbia +# +# Licensed under the Apache License, Version 2.0 (the 'License'); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an 'AS IS' BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +"""API endpoints for managing compliance finding resource.""" + +from http import HTTPStatus + +from flask_restx import Namespace, Resource + +from compliance_api.auth import auth +from compliance_api.schemas import KeyValueSchema +from compliance_api.services import ComplianceFindingService +from compliance_api.utils.util import cors_preflight + +from .apihelper import Api as ApiHelper + + +API = Namespace( + "compliance-findings", + description="Endpoints for compliance finding Management", +) + +key_value_list_model = ApiHelper.convert_ma_schema_to_restx_model( + API, KeyValueSchema(), "List" +) + + +@cors_preflight("GET, OPTIONS") +@API.route("", methods=["GET", "OPTIONS"]) +class ComplianceFindings(Resource): + """Resource for managing compliance findings.""" + + @staticmethod + @API.response(code=200, description="Success", model=[key_value_list_model]) + @ApiHelper.swagger_decorators( + API, endpoint_description="Fetch all compliance findings" + ) + @auth.require + def get(): + """Fetch all compliance findings.""" + compliance_findings = ComplianceFindingService.get_compliance_findings() + list_schema = KeyValueSchema(many=True) + return list_schema.dump(compliance_findings), HTTPStatus.OK diff --git a/compliance-api/src/compliance_api/resources/enforcement_action.py b/compliance-api/src/compliance_api/resources/enforcement_action.py new file mode 100644 index 00000000..453d38fd --- /dev/null +++ b/compliance-api/src/compliance_api/resources/enforcement_action.py @@ -0,0 +1,53 @@ +# Copyright © 2024 Province of British Columbia +# +# Licensed under the Apache License, Version 2.0 (the 'License'); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an 'AS IS' BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +"""API endpoints for managing enforcement action resource.""" + +from http import HTTPStatus + +from flask_restx import Namespace, Resource + +from compliance_api.auth import auth +from compliance_api.schemas import KeyValueSchema +from compliance_api.services import EnforcementActionService +from compliance_api.utils.util import cors_preflight + +from .apihelper import Api as ApiHelper + + +API = Namespace( + "enforcement-actions", + description="Endpoints for Enforcement Action Management", +) + +key_value_list_model = ApiHelper.convert_ma_schema_to_restx_model( + API, KeyValueSchema(), "List" +) + + +@cors_preflight("GET, OPTIONS") +@API.route("", methods=["GET", "OPTIONS"]) +class EnforcementActions(Resource): + """Resource for managing enforcement actions.""" + + @staticmethod + @API.response(code=200, description="Success", model=[key_value_list_model]) + @ApiHelper.swagger_decorators( + API, endpoint_description="Fetch all enforcement actions" + ) + @auth.require + def get(): + """Fetch all enforcement actions.""" + enforcement_actions = EnforcementActionService.get_enforcement_actions() + list_schema = KeyValueSchema(many=True) + return list_schema.dump(enforcement_actions), HTTPStatus.OK diff --git a/compliance-api/src/compliance_api/resources/requirement_source.py b/compliance-api/src/compliance_api/resources/requirement_source.py index 527cba62..da47fdb5 100644 --- a/compliance-api/src/compliance_api/resources/requirement_source.py +++ b/compliance-api/src/compliance_api/resources/requirement_source.py @@ -35,8 +35,8 @@ ) -@cors_preflight("GET, OPTIONS, POST") -@API.route("", methods=["POST", "GET", "OPTIONS"]) +@cors_preflight("GET, OPTIONS") +@API.route("", methods=["GET", "OPTIONS"]) class RequirementSource(Resource): """Resource for managing requirement source.""" diff --git a/compliance-api/src/compliance_api/services/__init__.py b/compliance-api/src/compliance_api/services/__init__.py index 35f21b0f..c5622147 100644 --- a/compliance-api/src/compliance_api/services/__init__.py +++ b/compliance-api/src/compliance_api/services/__init__.py @@ -23,3 +23,5 @@ from .requirement_source import RequirementSourceService from .staff_user import StaffUserService from .topic import TopicService +from .enforcement_action import EnforcementActionService +from .compliance_finding import ComplianceFindingService diff --git a/compliance-api/src/compliance_api/services/compliance_finding.py b/compliance-api/src/compliance_api/services/compliance_finding.py new file mode 100644 index 00000000..3bb890c4 --- /dev/null +++ b/compliance-api/src/compliance_api/services/compliance_finding.py @@ -0,0 +1,12 @@ +"""Compliance finding service.""" + +from compliance_api.models import ComplianceFindingOption as ComplianceFindingOptionModel + + +class ComplianceFindingService: + """Compliance finding service.""" + + @classmethod + def get_compliance_findings(cls): + """Get compliance finding options.""" + return ComplianceFindingOptionModel.get_all(sort_by="sort_order") diff --git a/compliance-api/src/compliance_api/services/enforcement_action.py b/compliance-api/src/compliance_api/services/enforcement_action.py new file mode 100644 index 00000000..5016fe75 --- /dev/null +++ b/compliance-api/src/compliance_api/services/enforcement_action.py @@ -0,0 +1,12 @@ +"""Enforcement Action service.""" + +from compliance_api.models import EnforcementActionOption as EnforcementActionOptionModel + + +class EnforcementActionService: + """Enforcement action service.""" + + @classmethod + def get_enforcement_actions(cls): + """Get enforcement actions.""" + return EnforcementActionOptionModel.get_all(sort_by="sort_order") diff --git a/compliance-api/tests/integration/api/test_compliance_finding.py b/compliance-api/tests/integration/api/test_compliance_finding.py new file mode 100644 index 00000000..73f78806 --- /dev/null +++ b/compliance-api/tests/integration/api/test_compliance_finding.py @@ -0,0 +1,14 @@ +"""test suit for compliance findings.""" +from http import HTTPStatus +from urllib.parse import urljoin + + +API_BASE_URL = "/api/" + + +def test_get_compliance_findings(client, auth_header): + """Get compliance findings.""" + url = urljoin(API_BASE_URL, "compliance-findings") + result = client.get(url, headers=auth_header) + assert len(result.json) == 3 + assert result.status_code == HTTPStatus.OK diff --git a/compliance-api/tests/integration/api/test_enforcement_action.py b/compliance-api/tests/integration/api/test_enforcement_action.py new file mode 100644 index 00000000..7d0f3a87 --- /dev/null +++ b/compliance-api/tests/integration/api/test_enforcement_action.py @@ -0,0 +1,14 @@ +"""test suit for enforcement actions.""" +from http import HTTPStatus +from urllib.parse import urljoin + + +API_BASE_URL = "/api/" + + +def test_get_enforcement_actions(client, auth_header): + """Get enforcement actions.""" + url = urljoin(API_BASE_URL, "enforcement-actions") + result = client.get(url, headers=auth_header) + assert len(result.json) == 9 + assert result.status_code == HTTPStatus.OK