Skip to content

Commit 47b4f12

Browse files
authored
Merge pull request #187 from dinesh-aot/document_type
DocumentTypes GET
2 parents eb270ab + bdbf0f1 commit 47b4f12

File tree

5 files changed

+80
-0
lines changed

5 files changed

+80
-0
lines changed

compliance-api/src/compliance_api/resources/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from .complaint import API as COMPLAINT_API
3030
from .compliance_finding import API as COMPLIANCE_FINDING_API
3131
from .continuation_report import API as CONTINUATION_REPORT_API
32+
from .document_type import API as DOCUMENT_TYPE_API
3233
from .enforcement_action import API as ENFORCEMENT_ACTION_API
3334
from .inspection import API as INSPECTION_API
3435
from .inspection_requirement import API as INSPECTION_REQUIREMENT_API
@@ -87,3 +88,4 @@
8788
API.add_namespace(ENFORCEMENT_ACTION_API)
8889
API.add_namespace(COMPLIANCE_FINDING_API)
8990
API.add_namespace(INSPECTION_REQUIREMENT_API, path="inspections/<int:inspection_id>/requirements")
91+
API.add_namespace(DOCUMENT_TYPE_API)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Copyright © 2024 Province of British Columbia
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the 'License');
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an 'AS IS' BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
"""API endpoints for managing document type resource."""
15+
16+
from http import HTTPStatus
17+
18+
from flask_restx import Namespace, Resource
19+
20+
from compliance_api.auth import auth
21+
from compliance_api.schemas import KeyValueSchema
22+
from compliance_api.services import DocumentTypeService
23+
from compliance_api.utils.util import cors_preflight
24+
25+
from .apihelper import Api as ApiHelper
26+
27+
28+
API = Namespace(
29+
"document-types",
30+
description="Endpoints for Document Type Management",
31+
)
32+
33+
key_value_list_model = ApiHelper.convert_ma_schema_to_restx_model(
34+
API, KeyValueSchema(), "List"
35+
)
36+
37+
38+
@cors_preflight("GET, OPTIONS")
39+
@API.route("", methods=["GET", "OPTIONS"])
40+
class DocumentTypes(Resource):
41+
"""Resource for managing document types."""
42+
43+
@staticmethod
44+
@API.response(code=200, description="Success", model=[key_value_list_model])
45+
@ApiHelper.swagger_decorators(API, endpoint_description="Fetch all document types")
46+
@auth.require
47+
def get():
48+
"""Fetch all document types."""
49+
document_types = DocumentTypeService.get_all()
50+
list_schema = KeyValueSchema(many=True)
51+
return list_schema.dump(document_types), HTTPStatus.OK

compliance-api/src/compliance_api/services/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from .complaint import ComplaintService
1818
from .compliance_finding import ComplianceFindingService
1919
from .continuation_report import ContinuationReportService
20+
from .document_type import DocumentTypeService
2021
from .enforcement_action import EnforcementActionService
2122
from .inspection import InspectionService
2223
from .inspection_requirement import InspectionRequirementService
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""Document Type service."""
2+
3+
from compliance_api.models import DocumentType as DocumentTypeModel
4+
5+
6+
class DocumentTypeService:
7+
"""DocumentTypeService."""
8+
9+
@classmethod
10+
def get_all(cls):
11+
"""Get enforcement actions."""
12+
return DocumentTypeModel.get_all(sort_by="sort_order")
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""test suit for document type."""
2+
from http import HTTPStatus
3+
from urllib.parse import urljoin
4+
5+
6+
API_BASE_URL = "/api/"
7+
8+
9+
def test_get_document_type(client, auth_header):
10+
"""Get document type."""
11+
url = urljoin(API_BASE_URL, "document-types")
12+
result = client.get(url, headers=auth_header)
13+
assert len(result.json) == 2
14+
assert result.status_code == HTTPStatus.OK

0 commit comments

Comments
 (0)