generated from bcgov/EPIC.scaffold
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from dinesh-aot/project_table
project table created
- Loading branch information
Showing
13 changed files
with
3,358 additions
and
5 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
63 changes: 63 additions & 0 deletions
63
compliance-api/migrations/versions/d36894553777_project_table_creation.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
"""project table creation | ||
Revision ID: d36894553777 | ||
Revises: 774870c99c95 | ||
Create Date: 2024-08-21 12:57:47.220758 | ||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
import json | ||
import os | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision = 'd36894553777' | ||
down_revision = '774870c99c95' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
def load_json_file(file_name): | ||
"""Load JSON data from a file.""" | ||
file_path = os.path.join(os.path.dirname(__file__), file_name) | ||
with open(file_path, 'r', encoding='utf-8') as file: | ||
return json.load(file) | ||
|
||
def upgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
projects_table = op.create_table('projects', | ||
sa.Column('id', sa.Integer(), nullable=False), | ||
sa.Column('name', sa.String(), nullable=False), | ||
sa.Column('description', sa.String(), nullable=True), | ||
sa.Column('ea_certificate', sa.String(length=255), nullable=True), | ||
sa.Column('proponent_name', sa.String(), nullable=False), | ||
sa.Column('created_date', sa.DateTime(), nullable=False), | ||
sa.Column('updated_date', sa.DateTime(), nullable=True), | ||
sa.Column('created_by', sa.String(length=100), nullable=False), | ||
sa.Column('updated_by', sa.String(length=100), nullable=True), | ||
sa.Column('is_active', sa.Boolean(), server_default='t', nullable=False), | ||
sa.Column('is_deleted', sa.Boolean(), server_default='f', nullable=False), | ||
sa.PrimaryKeyConstraint('id') | ||
) | ||
with op.batch_alter_table('case_files', schema=None) as batch_op: | ||
batch_op.alter_column('lead_officer_id', | ||
existing_type=sa.INTEGER(), | ||
comment='The lead officer who created the case file', | ||
existing_nullable=True) | ||
batch_op.create_foreign_key('case_files_project_id_projects_id_fkey', 'projects', ['project_id'], ['id']) | ||
projects_data = load_json_file("../seed_data/project.json") | ||
op.bulk_insert(projects_table, projects_data) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
with op.batch_alter_table('case_files', schema=None) as batch_op: | ||
batch_op.drop_constraint('case_files_project_id_projects_id_fkey', type_='foreignkey') | ||
batch_op.alter_column('lead_officer_id', | ||
existing_type=sa.INTEGER(), | ||
comment=None, | ||
existing_comment='The lead officer who created the case file', | ||
existing_nullable=True) | ||
op.drop_table('projects') | ||
# ### end Alembic commands ### |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
"""Project Model.""" | ||
|
||
from sqlalchemy import Column, Integer, String | ||
|
||
from .base_model import BaseModel | ||
|
||
|
||
class Project(BaseModel): | ||
"""Project Model Class.""" | ||
|
||
__tablename__ = "projects" | ||
|
||
id = Column(Integer, primary_key=True) | ||
name = Column(String, nullable=False) | ||
description = Column(String, nullable=True) | ||
ea_certificate = Column(String(255), nullable=True, default=None) | ||
proponent_name = Column(String, nullable=False) | ||
|
||
def __setattr__(self, key, value): | ||
"""Set attribute value.""" | ||
if hasattr(self, key): | ||
raise AttributeError(f"Cannot modify {key}. This class is read-only.") | ||
super().__setattr__(key, value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# 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 project resource.""" | ||
|
||
from http import HTTPStatus | ||
|
||
from flask_restx import Namespace, Resource | ||
|
||
from compliance_api.auth import auth | ||
from compliance_api.exceptions import ResourceNotFoundError | ||
from compliance_api.schemas import ProjectSchema | ||
from compliance_api.services import ProjectService | ||
from compliance_api.utils.util import cors_preflight | ||
|
||
from .apihelper import Api as ApiHelper | ||
|
||
|
||
API = Namespace("projects", description="Endpoints for Project Management") | ||
project_list_model = ApiHelper.convert_ma_schema_to_restx_model( | ||
API, ProjectSchema(), "ProjectListSchema" | ||
) | ||
|
||
|
||
@cors_preflight("GET, OPTIONS") | ||
@API.route("", methods=["POST", "GET", "OPTIONS"]) | ||
class Projects(Resource): | ||
"""Resource for managing projects.""" | ||
|
||
@staticmethod | ||
@API.response(code=200, description="Success", model=[project_list_model]) | ||
@ApiHelper.swagger_decorators(API, endpoint_description="Fetch all agencies") | ||
@auth.require | ||
def get(): | ||
"""Fetch all projects.""" | ||
projects = ProjectService.get_all_projects() | ||
project_list_schema = ProjectSchema(many=True) | ||
return project_list_schema.dump(projects), HTTPStatus.OK | ||
|
||
|
||
@cors_preflight("GET, OPTIONS") | ||
@API.route("/<int:project_id>", methods=["GET", "OPTIONS"]) | ||
@API.doc(params={"project_id": "The unique identifier of project"}) | ||
class Project(Resource): | ||
"""Resource for managing a single project.""" | ||
|
||
@staticmethod | ||
@auth.require | ||
@ApiHelper.swagger_decorators(API, endpoint_description="Fetch a project by id") | ||
@API.response(code=200, model=project_list_model, description="Success") | ||
@API.response(404, "Not Found") | ||
def get(project_id): | ||
"""Fetch an project by id.""" | ||
project = ProjectService.get_project_by_id(project_id) | ||
if not project: | ||
raise ResourceNotFoundError(f"Project with {project_id} not found") | ||
return ProjectSchema().dump(project), HTTPStatus.OK |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# 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. | ||
"""Project Schema.""" | ||
from marshmallow import EXCLUDE | ||
|
||
from compliance_api.models.project import Project as ProjectModel | ||
|
||
from .base_schema import AutoSchemaBase | ||
|
||
|
||
class ProjectSchema(AutoSchemaBase): # pylint: disable=too-many-ancestors | ||
"""Project schema.""" | ||
|
||
class Meta(AutoSchemaBase.Meta): # pylint: disable=too-few-public-methods | ||
"""Exclude unknown fields in the deserialized output.""" | ||
|
||
unknown = EXCLUDE | ||
model = ProjectModel | ||
include_fk = True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
"""Service for project resource management.""" | ||
from compliance_api.models import Project as ProjectModel | ||
|
||
|
||
class ProjectService: | ||
"""Project management service.""" | ||
|
||
@classmethod | ||
def get_project_by_id(cls, project_id): | ||
"""Get project by id.""" | ||
project = ProjectModel.find_by_id(project_id) | ||
return project | ||
|
||
@classmethod | ||
def get_all_projects(cls): | ||
"""Get all projects.""" | ||
projects = ProjectModel.get_all() | ||
return projects |