forked from bcgov/met-public
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[EPICSYSTEM-20] Add new Hidden visibility to engagements (#59)
* [EPICSYSTEM-20] Add new Hidden visibility to engagements
- Loading branch information
Showing
27 changed files
with
483 additions
and
105 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
63 changes: 63 additions & 0 deletions
63
met-api/migrations/versions/a3e6dae331ab_add_visibility_column_to_engagment.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 @@ | ||
"""Add visibility column to engagment | ||
Revision ID: a3e6dae331ab | ||
Revises: 9a93fda677eb | ||
Create Date: 2024-05-28 08:26:11.155679 | ||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
from sqlalchemy.dialects import postgresql | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = 'a3e6dae331ab' | ||
down_revision = '9a93fda677eb' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
def upgrade(): | ||
# Create the engagement_visibility table | ||
engagement_visiblity_table = op.create_table( | ||
'engagement_visibility', | ||
sa.Column('id', sa.Integer(), nullable=False, autoincrement=True), | ||
sa.Column('visibility_name', sa.String(length=50), nullable=False), | ||
sa.Column('description', sa.String(length=100), nullable=False), | ||
sa.Column('created_date', sa.TIMESTAMP(timezone=False), nullable=False, server_default=sa.text('CURRENT_TIMESTAMP')), | ||
sa.Column('updated_date', sa.TIMESTAMP(timezone=False), nullable=True), | ||
sa.PrimaryKeyConstraint('id') | ||
) | ||
# Insert the initial data into the engagement_visibility table | ||
op.bulk_insert( | ||
engagement_visiblity_table, | ||
[ | ||
{'id': 1, 'visibility_name': 'Public', 'description': 'Visible to all users'}, | ||
{'id': 2, 'visibility_name': 'Slug', 'description': 'Accessible to users with the direct link'}, | ||
{'id': 3, 'visibility_name': 'AuthToken', 'description': 'Visible to authenticated users'} | ||
] | ||
) | ||
# Add the visibility column to the engagement table | ||
op.add_column('engagement', sa.Column('visibility', sa.Integer(), nullable=False, server_default='1')) | ||
op.create_foreign_key("engagement_visibility_fkey", 'engagement', 'engagement_visibility', ['visibility'], ['id']) | ||
# Update the visibility column based on the is_internal column | ||
op.execute(""" | ||
UPDATE engagement e | ||
SET visibility = ev.id | ||
FROM engagement_visibility ev | ||
WHERE (e.is_internal AND ev.visibility_name = 'AuthToken') | ||
OR (NOT e.is_internal AND ev.visibility_name = 'Public') | ||
""") | ||
op.drop_column('engagement', 'is_internal') | ||
|
||
|
||
def downgrade(): | ||
# Add the is_internal column to the engagement table | ||
op.add_column('engagement', sa.Column('is_internal', sa.BOOLEAN(), nullable=False, server_default='0')) | ||
# Populate the is_internal column based on the visibility column | ||
op.execute(""" | ||
UPDATE engagement e | ||
SET is_internal = (e.visibility = (SELECT id FROM engagement_visibility WHERE visibility_name = 'AuthToken')) | ||
""") | ||
# Drop the foreign key constraint on the visibility column | ||
op.drop_constraint("engagement_visibility_fkey", 'engagement', type_='foreignkey') | ||
op.drop_column('engagement', 'visibility') | ||
op.drop_table('engagement_visibility') |
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 @@ | ||
# 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. | ||
"""Constants of engagement visibility.""" | ||
from enum import IntEnum | ||
|
||
|
||
class Visibility(IntEnum): | ||
"""Enum of engagement visibility.""" | ||
|
||
Public = 1 | ||
Slug = 2 | ||
AuthToken = 3 |
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,27 @@ | ||
"""Engagement Visibility model class. | ||
Manages the engagement visibility | ||
""" | ||
from .base_model import BaseModel | ||
from .db import db, ma | ||
|
||
|
||
class EngagementVisibility(BaseModel): # pylint: disable=too-few-public-methods | ||
"""Definition of the Engagement Visibility entity.""" | ||
|
||
__tablename__ = 'engagement_visibility' | ||
|
||
id = db.Column(db.Integer, primary_key=True, autoincrement=True) | ||
visibility_name = db.Column(db.String(50), nullable=False) | ||
description = db.Column(db.String(50)) | ||
created_date = db.Column(db.DateTime, nullable=False, default=db.func.current_timestamp()) | ||
updated_date = db.Column(db.DateTime, nullable=True) | ||
|
||
|
||
class EngagementVisibilitySchema(ma.Schema): | ||
"""Engagement visibility schema.""" | ||
|
||
class Meta: # pylint: disable=too-few-public-methods | ||
"""Meta class.""" | ||
|
||
fields = ('id', 'visibility_name', 'description', 'created_date', 'updated_date') |
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,17 @@ | ||
"""Engagement visibility schema class.""" | ||
from marshmallow import EXCLUDE, Schema, fields | ||
|
||
|
||
class EngagementVisibilitySchema(Schema): | ||
"""Schema for engagement visibility.""" | ||
|
||
class Meta: # pylint: disable=too-few-public-methods | ||
"""Exclude unknown fields in the deserialized output.""" | ||
|
||
unknown = EXCLUDE | ||
|
||
id = fields.Int(data_key='id') | ||
visibility_name = fields.Str(data_key='visibility_name') | ||
description = fields.Str(data_key='description') | ||
created_date = fields.Str(data_key='created_date') | ||
updated_date = fields.Str(data_key='updated_date') |
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.