Skip to content

Commit

Permalink
Merge pull request #2580 from bcgov/task/DESENG-676-combine-custom-an…
Browse files Browse the repository at this point in the history
…d-summary-content

[To Main] Task - DESENG-676: Combine custom and summary content
  • Loading branch information
NatSquared authored Sep 3, 2024
2 parents b0cb3b1 + a3a5461 commit f8bd855
Show file tree
Hide file tree
Showing 52 changed files with 362 additions and 1,553 deletions.
24 changes: 24 additions & 0 deletions met-api/migrations/versions/42641011576a_.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""Merge revision heads
Revision ID: 42641011576a
Revises: c2a384ddfe6a, bd493dbd9e0e
Create Date: 2024-08-22 17:53:24.806016
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '42641011576a'
down_revision = ('c2a384ddfe6a', 'bd493dbd9e0e')
branch_labels = None
depends_on = None


def upgrade():
pass


def downgrade():
pass
112 changes: 112 additions & 0 deletions met-api/migrations/versions/bd493dbd9e0e_merge_engagement_content.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
"""Merge the engagement_summary_content and engagement_custom_content tables into the engagement_content table
Revision ID: bd493dbd9e0e
Revises: 901a6724bca2
Create Date: 2024-08-21 10:06:44.377763
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
from sqlalchemy.sql import table, column
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = 'bd493dbd9e0e'
down_revision = '901a6724bca2'
branch_labels = None
depends_on = None


def upgrade():
# Create new columns in the engagement_content table
op.add_column('engagement_content', sa.Column('text_content', sa.Text(), nullable=True))
op.add_column('engagement_content', sa.Column('json_content', sa.JSON(), nullable=True))

# Reference the existing tables

engagement_content = table('engagement_content',
column('id', sa.Integer),
column('text_content', sa.Text),
column('json_content', sa.JSON)
)

engagement_summary_content = table('engagement_summary_content',
column('engagement_content_id', sa.Integer),
column('content', sa.Text),
column('rich_content', sa.JSON)
)

engagement_custom_content = table('engagement_custom_content',
column('engagement_content_id', sa.Integer),
column('custom_text_content', sa.Text),
column('custom_json_content', sa.JSON)
)

# Copy data from the old tables to the new columns in engagement_content
op.execute(
engagement_content.update()
.where(engagement_content.c.id == engagement_summary_content.c.engagement_content_id)
.values({
'text_content': engagement_summary_content.c.content,
'json_content': engagement_summary_content.c.rich_content
})
)

op.execute(
engagement_content.update()
.where(engagement_content.c.id == engagement_custom_content.c.engagement_content_id)
.values({
'text_content': engagement_custom_content.c.custom_text_content,
'json_content': engagement_custom_content.c.custom_json_content
})
)

# Drop old tables
op.drop_table('engagement_custom_content')
op.drop_table('engagement_summary_content')

# Drop the content_type column and icon name as they're no longer needed
op.drop_column('engagement_content', 'content_type')
op.drop_column('engagement_content', 'icon_name')


def downgrade():
# Recreate the old tables
op.create_table('engagement_summary_content',
sa.Column('created_date', postgresql.TIMESTAMP(), autoincrement=False, nullable=False),
sa.Column('updated_date', postgresql.TIMESTAMP(), autoincrement=False, nullable=True),
sa.Column('id', sa.INTEGER(), autoincrement=True, nullable=False),
sa.Column('content', sa.TEXT(), autoincrement=False, nullable=False),
sa.Column('rich_content', postgresql.JSON(astext_type=sa.Text()), autoincrement=False, nullable=False),
sa.Column('engagement_content_id', sa.INTEGER(), autoincrement=False, nullable=True),
sa.Column('engagement_id', sa.INTEGER(), autoincrement=False, nullable=True),
sa.Column('created_by', sa.VARCHAR(length=50), autoincrement=False, nullable=True),
sa.Column('updated_by', sa.VARCHAR(length=50), autoincrement=False, nullable=True),
sa.ForeignKeyConstraint(['engagement_content_id'], ['engagement_content.id'], name='engagement_summary_content_engagement_content_id_fkey', ondelete='CASCADE'),
sa.ForeignKeyConstraint(['engagement_id'], ['engagement.id'], name='engagement_summary_content_engagement_id_fkey', ondelete='CASCADE'),
sa.PrimaryKeyConstraint('id', name='engagement_summary_content_pkey')
)

op.create_table('engagement_custom_content',
sa.Column('created_date', postgresql.TIMESTAMP(), autoincrement=False, nullable=False),
sa.Column('updated_date', postgresql.TIMESTAMP(), autoincrement=False, nullable=True),
sa.Column('id', sa.INTEGER(), autoincrement=True, nullable=False),
sa.Column('custom_text_content', sa.TEXT(), autoincrement=False, nullable=True),
sa.Column('custom_json_content', postgresql.JSON(astext_type=sa.Text()), autoincrement=False, nullable=True),
sa.Column('engagement_content_id', sa.INTEGER(), autoincrement=False, nullable=True),
sa.Column('engagement_id', sa.INTEGER(), autoincrement=False, nullable=True),
sa.Column('created_by', sa.VARCHAR(length=50), autoincrement=False, nullable=True),
sa.Column('updated_by', sa.VARCHAR(length=50), autoincrement=False, nullable=True),
sa.ForeignKeyConstraint(['engagement_content_id'], ['engagement_content.id'], name='engagement_custom_content_engagement_content_id_fkey', ondelete='CASCADE'),
sa.ForeignKeyConstraint(['engagement_id'], ['engagement.id'], name='engagement_custom_content_engagement_id_fkey', ondelete='CASCADE'),
sa.PrimaryKeyConstraint('id', name='engagement_custom_content_pkey')
)

# Drop new columns in engagement_content table
op.drop_column('engagement_content', 'json_content')
op.drop_column('engagement_content', 'text_content')

# Re-add the content_type and icon_name columns
op.add_column('engagement_content', sa.Column('icon_name', sa.Text(), autoincrement=False, nullable=True))
op.add_column('engagement_content', sa.Column('content_type', postgresql.ENUM('Summary', 'Custom', name='engagementcontenttype'), autoincrement=False))
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
from sqlalchemy import text
from sqlalchemy.dialects import postgresql

from met_api.constants.engagement_content_type import EngagementContentType
from met_api.utils.enums import ContentTitle

# revision identifiers, used by Alembic.
revision = 'e2625b0d07ab'
down_revision = '37176ea4708d'
Expand Down Expand Up @@ -90,9 +87,9 @@ def upgrade():
"""
),
{
'title': ContentTitle.DEFAULT.value,
'icon_name': ContentTitle.DEFAULT_ICON.value,
'content_type': EngagementContentType(1).name,
'title': "Summary",
'icon_name': "n/a",
'content_type': "n/a",
'engagement_id': eng_id,
},
)
Expand Down
33 changes: 0 additions & 33 deletions met-api/src/met_api/constants/engagement_content_type.py

This file was deleted.

2 changes: 0 additions & 2 deletions met-api/src/met_api/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
from .engagement_status import EngagementStatus
from .engagement_status_block import EngagementStatusBlock
from .engagement_settings import EngagementSettingsModel
from .engagement_custom_content import EngagementCustom
from .engagement_summary_content import EngagementSummary
from .event_item import EventItem
from .subscribe_item import SubscribeItem
from .feedback import Feedback
Expand Down
16 changes: 7 additions & 9 deletions met-api/src/met_api/models/engagement_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from typing import Optional

from sqlalchemy.sql.schema import ForeignKey
from met_api.constants.engagement_content_type import EngagementContentType

from .base_model import BaseModel
from .db import db
Expand All @@ -21,24 +20,23 @@ class EngagementContent(BaseModel):
__tablename__ = 'engagement_content'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
title = db.Column(db.String(50), unique=False, nullable=False)
icon_name = db.Column(db.Text, unique=False, nullable=True)
content_type = db.Column(db.Enum(EngagementContentType), nullable=False,
default=EngagementContentType.Summary)
text_content = db.Column(db.Text, unique=False, nullable=True)
json_content = db.Column(db.JSON, unique=False, nullable=True)
engagement_id = db.Column(db.Integer, ForeignKey('engagement.id', ondelete='CASCADE'))
sort_index = db.Column(db.Integer, nullable=False, default=1)
is_internal = db.Column(db.Boolean, nullable=False)

@classmethod
def get_contents_by_engagement_id(cls, engagement_id):
"""Get contents by engagement id."""
def find_by_engagement_id(cls, engagement_id):
"""Get content by engagement id."""
return db.session.query(EngagementContent)\
.filter(EngagementContent.engagement_id == engagement_id)\
.order_by(EngagementContent.sort_index.asc())\
.all()

@classmethod
def update_engagement_contents(cls, update_mappings: list) -> None:
"""Update contents."""
def bulk_update_engagement_content(cls, update_mappings: list) -> None:
"""Update content."""
db.session.bulk_update_mappings(EngagementContent, update_mappings)
db.session.commit()

Expand All @@ -49,7 +47,7 @@ def save_engagement_content(cls, content: list) -> None:

@classmethod
def remove_engagement_content(cls, engagement_id, engagement_content_id,) -> EngagementContent:
"""Remove engagement content from engagement."""
"""Remove content from an engagement."""
engagement_content = EngagementContent.query.filter_by(id=engagement_content_id,
engagement_id=engagement_id).delete()
db.session.commit()
Expand Down
47 changes: 0 additions & 47 deletions met-api/src/met_api/models/engagement_custom_content.py

This file was deleted.

55 changes: 0 additions & 55 deletions met-api/src/met_api/models/engagement_summary_content.py

This file was deleted.

4 changes: 0 additions & 4 deletions met-api/src/met_api/resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
from .document import API as DOCUMENT_API
from .email_verification import API as EMAIL_VERIFICATION_API
from .engagement_content import API as ENGAGEMENT_CONTENT_API
from .engagement_custom_content import API as ENGAGEMENT_CUSTOM_CONTENT_API
from .engagement_summary_content import API as ENGAGEMENT_SUMMARY_CONTENT_API
from .engagement import API as ENGAGEMENT_API
from .engagement_metadata import API as ENGAGEMENT_METADATA_API
from .metadata_taxon import API as METADATA_TAXON_API
Expand Down Expand Up @@ -84,8 +82,6 @@
API.add_namespace(COMMENT_API)
API.add_namespace(EMAIL_VERIFICATION_API)
API.add_namespace(ENGAGEMENT_CONTENT_API, path='/engagement/<int:engagement_id>/content')
API.add_namespace(ENGAGEMENT_CUSTOM_CONTENT_API, path='/content/<int:content_id>/custom')
API.add_namespace(ENGAGEMENT_SUMMARY_CONTENT_API, path='/content/<int:content_id>/summary')
API.add_namespace(FEEDBACK_API)
API.add_namespace(WIDGET_API)
API.add_namespace(CONTACT_API)
Expand Down
7 changes: 3 additions & 4 deletions met-api/src/met_api/resources/engagement_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
@cors_preflight('GET, POST, OPTIONS')
@API.route('')
class EngagementContent(Resource):
"""Resource for managing a engagement content."""
"""Resource for managing an engagement's contents."""

@staticmethod
@cross_origin(origins=allowedorigins())
Expand Down Expand Up @@ -113,9 +113,8 @@ def patch(engagement_id, engagement_content_id):
try:
user_id = TokenInfo.get_id()
engagement_content_data = request.get_json()
valid_format, errors = schema_utils.validate(engagement_content_data, 'engagement_content_update')
if not valid_format:
return {'message': schema_utils.serialize(errors)}, HTTPStatus.BAD_REQUEST
if not engagement_content_data or not engagement_content_data.get('title'):
return 'No content provided', HTTPStatus.BAD_REQUEST

updated_engagement_content = EngagementContentService().update_engagement_content(engagement_id,
engagement_content_id,
Expand Down
Loading

0 comments on commit f8bd855

Please sign in to comment.