-
Notifications
You must be signed in to change notification settings - Fork 5
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 #38 from jadmsaadaot/SUBMIT-task#30
Handle inserting projects to account and getting projects by account
- Loading branch information
Showing
32 changed files
with
331 additions
and
73 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
""" | ||
"""Create staff_users table | ||
Revision ID: 20bcb68bc2ac | ||
Revises: | ||
|
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
"""empty message | ||
"""Add description column to roles table | ||
Revision ID: 6dfc3501adbb | ||
Revises: 0223afc14044 | ||
|
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
"""empty message | ||
"""Add ACCOUNT_PRIMARY_ADMIN role | ||
Revision ID: 97de805275ec | ||
Revises: 6dfc3501adbb | ||
|
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,43 @@ | ||
"""Add projects table | ||
Revision ID: a4785881834c | ||
Revises: 5a6eda61c0c4 | ||
Create Date: 2024-08-19 17:45:56.220026 | ||
""" | ||
import sqlalchemy as sa | ||
from alembic import op | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision = 'a4785881834c' | ||
down_revision = '5a6eda61c0c4' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.create_table('projects', | ||
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False), | ||
sa.Column('name', sa.String(), nullable=False), | ||
sa.Column('proponent_id', sa.String(), nullable=False), | ||
sa.Column('proponent_name', sa.String(), nullable=False), | ||
sa.PrimaryKeyConstraint('id'), | ||
sa.UniqueConstraint('proponent_id') | ||
) | ||
with op.batch_alter_table('account_projects', schema=None) as batch_op: | ||
batch_op.create_foreign_key(None, 'projects', ['project_id'], ['id']) | ||
batch_op.drop_column('name') | ||
|
||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
with op.batch_alter_table('account_projects', schema=None) as batch_op: | ||
batch_op.add_column(sa.Column('name', sa.VARCHAR(), autoincrement=False, nullable=False)) | ||
batch_op.drop_constraint(None, type_='foreignkey') | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
"""Change proponent_id column type to integer | ||
Revision ID: b79603b76ea0 | ||
Revises: a4785881834c | ||
Create Date: 2024-08-20 15:17:44.896108 | ||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision = 'b79603b76ea0' | ||
down_revision = 'a4785881834c' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
with op.batch_alter_table('accounts', schema=None) as batch_op: | ||
batch_op.alter_column('proponent_id', | ||
existing_type=sa.VARCHAR(), | ||
type_=sa.Integer(), | ||
existing_nullable=False, | ||
postgresql_using='proponent_id::integer') | ||
|
||
with op.batch_alter_table('projects', schema=None) as batch_op: | ||
batch_op.alter_column('proponent_id', | ||
existing_type=sa.VARCHAR(), | ||
type_=sa.Integer(), | ||
existing_nullable=False, | ||
postgresql_using='proponent_id::integer') | ||
|
||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
pass | ||
|
||
# ### 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
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,62 @@ | ||
"""Account Project model class. | ||
Manages the account project | ||
""" | ||
from __future__ import annotations | ||
|
||
from sqlalchemy import Column, event | ||
from sqlalchemy.exc import ArgumentError, IntegrityError | ||
|
||
from submit_api.exceptions import PermissionDeniedError | ||
|
||
from .db import db | ||
|
||
|
||
class Project(db.Model): | ||
"""Definition of the Project entity.""" | ||
|
||
__tablename__ = 'projects' | ||
|
||
id = Column(db.Integer, primary_key=True, autoincrement=True) | ||
name = Column(db.String(), nullable=False) | ||
proponent_id = Column(db.Integer(), nullable=False, unique=True) | ||
proponent_name = Column(db.String(), nullable=False) | ||
|
||
def __init__(self, **kwargs): | ||
"""Initialize the Project entity.""" | ||
raise ArgumentError("Project is read-only, cannot create new instances.") | ||
|
||
@classmethod | ||
def get_all_projects_in_ids(cls, project_ids): | ||
"""Get all projects in the given project ids.""" | ||
return cls.query.filter(cls.id.in_(project_ids)).all() | ||
|
||
|
||
@event.listens_for(Project, 'before_insert') | ||
def before_insert(): | ||
"""Raise an error when trying to insert into the Project table.""" | ||
raise IntegrityError( | ||
"Insertions are not allowed on this table", | ||
params=None, | ||
orig=PermissionDeniedError('Insertions are not allowed on this table') | ||
) | ||
|
||
|
||
@event.listens_for(Project, 'before_update') | ||
def before_update(): | ||
"""Raise an error when trying to update the Project table.""" | ||
raise IntegrityError( | ||
"Updates are not allowed on this table", | ||
params=None, | ||
orig=PermissionDeniedError('Updates are not allowed on this table') | ||
) | ||
|
||
|
||
@event.listens_for(Project, 'before_delete') | ||
def before_delete(): | ||
"""Raise an error when trying to delete from the Project table.""" | ||
raise IntegrityError( | ||
"Deletions are not allowed on this table", | ||
params=None, | ||
orig=PermissionDeniedError('Deletions are not allowed on this table') | ||
) |
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,36 @@ | ||
# 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. | ||
"""Model to handle all complex operations related to User.""" | ||
from submit_api.models import db, AccountProject, Project | ||
|
||
|
||
# pylint: disable=too-few-public-methods | ||
class ProjectQueries: | ||
"""Query module for complex projects queries""" | ||
|
||
@classmethod | ||
def get_projects_by_account_id(cls, account_id: int): | ||
"""Find projects by account_id""" | ||
result = (db.session.query(AccountProject) | ||
.filter(AccountProject.account_id == account_id) | ||
.join(Project) | ||
.first()) | ||
return result | ||
|
||
@classmethod | ||
def get_projects_by_proponent_id(cls, proponent_id: int): | ||
"""Find projects by proponent_id""" | ||
result = (db.session.query(Project) | ||
.filter(Project.proponent_id == proponent_id)) | ||
return result |
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
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
Oops, something went wrong.