Skip to content

Commit

Permalink
DESENG-670: Admin authoring header page
Browse files Browse the repository at this point in the history
  • Loading branch information
NatSquared committed Sep 24, 2024
1 parent f51812c commit 8874c5f
Show file tree
Hide file tree
Showing 39 changed files with 1,260 additions and 695 deletions.
25 changes: 25 additions & 0 deletions met-api/migrations/versions/63890bdab166_merge_heads.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""Merge heads
Revision ID: 63890bdab166
Revises: df693f5ddaf9, 828b4f34734a
Create Date: 2024-09-20 13:55:27.230188
"""

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = "63890bdab166"
down_revision = ("df693f5ddaf9", "828b4f34734a")
branch_labels = None
depends_on = None


def upgrade():
pass


def downgrade():
pass
174 changes: 174 additions & 0 deletions met-api/migrations/versions/828b4f34734a_status_block_updates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
"""Move cta_message and cta_url from engagement to engagement_status_block
Revision ID: 828b4f34734a
Revises: e706db763790
Create Date: 2024-09-16 10:18:49.858066
"""

from enum import IntEnum, Enum
from alembic import op
import sqlalchemy as sa


class SubmissionStatus(IntEnum):
"""Enum of engagement submission status."""

Upcoming = 1
Open = 2
Closed = 3
ViewResults = 4


# revision identifiers, used by Alembic.
revision = "828b4f34734a"
down_revision = "e706db763790"
branch_labels = None
depends_on = None


def upgrade():
connection = op.get_bind()
# Create a new enum type with the 'ViewResults' value
op.execute("ALTER TYPE submissionstatus RENAME TO submissionstatus_old")
op.execute(
"CREATE TYPE submissionstatus AS ENUM ('Upcoming', 'Open', 'Closed', 'ViewResults')"
)
# Commit the enum change before dropping the old one
op.execute("COMMIT")
# Convert the existing data to the new enum type
connection.execute(
"ALTER TABLE engagement_status_block ALTER COLUMN survey_status TYPE submissionstatus USING survey_status::text::submissionstatus"
)
# Drop the old enum type
op.execute("DROP TYPE submissionstatus_old")

op.add_column(
"engagement_status_block",
sa.Column("button_text", sa.String(length=20), nullable=True),
)
op.add_column(
"engagement_status_block",
sa.Column(
"link_type", sa.String(length=20), nullable=False, server_default="internal"
),
)
op.add_column(
"engagement_status_block",
sa.Column("internal_link", sa.String(length=50), nullable=True),
)
op.add_column(
"engagement_status_block",
sa.Column("external_link", sa.String(length=300), nullable=True),
)
# Migrate data from engagement to engagement_status_block
# Ad-hoc table definition for engagement
engagement_table = sa.Table(
"engagement",
sa.MetaData(),
sa.Column("id", sa.Integer),
sa.Column("cta_message", sa.String(50)),
sa.Column("cta_url", sa.String(500)),
)
# Ad-hoc table definition for engagement_status_block
engagement_status_block_table = sa.Table(
"engagement_status_block",
sa.MetaData(),
sa.Column("id", sa.Integer),
sa.Column("engagement_id", sa.Integer),
sa.Column(
"survey_status",
sa.Enum(
SubmissionStatus,
name="submissionstatus",
metadata=sa.MetaData(),
create_constraint=True,
),
),
sa.Column("block_text", sa.JSON),
sa.Column("button_text", sa.String(20)),
sa.Column("link_type", sa.String(20)),
sa.Column("internal_link", sa.String(50)),
sa.Column("external_link", sa.String(300)),
sa.Column("created_date", sa.DateTime),
sa.Column("updated_date", sa.DateTime),
)
# For each engagement...
for engagement in connection.execute(engagement_table.select()):
# Update existing engagement_status_blocks...
# If the URL starts with "http", it's an external link.
if engagement.cta_url and engagement.cta_url.startswith("http"):
link_type = "external"
external_link = engagement.cta_url
internal_link = None
else:
valid_statuses = ["hero", "description", "contentTabs", "provideFeedback"]
link_type = "internal"
external_link = None
internal_link = None
if engagement.cta_url in valid_statuses:
internal_link = engagement.cta_url
else:
internal_link = "provideFeedback"
connection.execute(
engagement_status_block_table.update()
.where(engagement_status_block_table.c.engagement_id == engagement.id)
.where(engagement_status_block_table.c.survey_status == "Open")
.values(
button_text=engagement.cta_message or "Provide Feedback",
link_type=link_type,
external_link=external_link,
internal_link=internal_link,
)
)
# And add the "View Results" block type.
connection.execute(
engagement_status_block_table.insert().values(
engagement_id=engagement.id,
survey_status=SubmissionStatus.ViewResults,
block_text={},
button_text="View Results",
link_type="internal",
internal_link="provideFeedback",
external_link=None,
created_date=sa.func.now(),
updated_date=sa.func.now(),
)
)
# Drop the columns from the engagement table
op.drop_column("engagement", "cta_message")
op.drop_column("engagement", "cta_url")


def downgrade():
op.drop_column("engagement_status_block", "external_link")
op.drop_column("engagement_status_block", "internal_link")
op.drop_column("engagement_status_block", "link_type")
op.drop_column("engagement_status_block", "button_text")
op.add_column(
"engagement",
sa.Column(
"cta_url", sa.VARCHAR(length=500), autoincrement=False, nullable=True
),
)
op.add_column(
"engagement",
sa.Column(
"cta_message", sa.VARCHAR(length=50), autoincrement=False, nullable=True
),
)
# Create a new enum type without the 'ViewResults' value
op.execute("ALTER TYPE submissionstatus RENAME TO submissionstatus_old")
op.execute("CREATE TYPE submissionstatus AS ENUM ('Upcoming', 'Open', 'Closed')")
# Commit the enum change before dropping the old one
op.execute("COMMIT")
# Convert the existing data to the new enum type
connection = op.get_bind()
connection.execute(
"UPDATE engagement_status_block SET survey_status = 'Closed' WHERE survey_status = 'ViewResults'"
)
connection.execute(
"ALTER TABLE engagement_status_block ALTER COLUMN survey_status TYPE submissionstatus USING survey_status::text::submissionstatus"
)
# Drop the old enum type
op.execute("DROP TYPE submissionstatus_old")
12 changes: 11 additions & 1 deletion met-api/src/met_api/constants/engagement_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
"""Constants of engagement status."""
from enum import IntEnum
from enum import Enum, IntEnum


class Status(IntEnum):
Expand Down Expand Up @@ -43,3 +43,13 @@ class SubmissionStatus(IntEnum):
Upcoming = 1
Open = 2
Closed = 3
ViewResults = 4


class EngagementViewSections(Enum):
"""Enum of sections that can be displayed in the engagement view."""

HERO = 'hero'
DESCRIPTION = 'description'
CONTENT_TABS = 'contentTabs'
PROVIDE_FEEDBACK = 'provideFeedback'
2 changes: 0 additions & 2 deletions met-api/src/met_api/models/engagement.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ class Engagement(BaseModel):
is_internal = db.Column(db.Boolean, nullable=False)
consent_message = db.Column(JSON, unique=False, nullable=True)
sponsor_name = db.Column(db.String(50), nullable=True)
cta_message = db.Column(db.String(50), nullable=True)
cta_url = db.Column(db.String(500), nullable=True)

@classmethod
def get_engagements_paginated(
Expand Down
23 changes: 17 additions & 6 deletions met-api/src/met_api/models/engagement_status_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,33 @@ class EngagementStatusBlock(BaseModel):

__tablename__ = 'engagement_status_block'
__table_args__ = (
db.UniqueConstraint('engagement_id', 'survey_status', name='unique_engagement_status_block'),
db.UniqueConstraint(
'engagement_id', 'survey_status', name='unique_engagement_status_block'
),
)

id = db.Column(db.Integer, primary_key=True, autoincrement=True)
engagement_id = db.Column(db.Integer, ForeignKey('engagement.id', ondelete='CASCADE'))
engagement_id = db.Column(
db.Integer, ForeignKey('engagement.id', ondelete='CASCADE')
)
survey_status = db.Column(db.Enum(SubmissionStatus), nullable=False)
block_text = db.Column(JSON, unique=False, nullable=False)
button_text = db.Column(db.String(20), nullable=True)
link_type = db.Column(db.String(20), nullable=False)
internal_link = db.Column(db.String(50), nullable=True)
external_link = db.Column(db.String(300), nullable=True)

@classmethod
def get_by_status(cls, engagement_id, survey_status):
"""Get Engagement Status by survey status."""
return db.session.query(EngagementStatusBlock) \
.filter(EngagementStatusBlock.survey_status == survey_status,
EngagementStatusBlock.engagement_id == engagement_id
) \
return (
db.session.query(EngagementStatusBlock)
.filter(
EngagementStatusBlock.survey_status == survey_status,
EngagementStatusBlock.engagement_id == engagement_id,
)
.first()
)

@classmethod
def save_status_blocks(cls, status_blocks: list) -> None:
Expand Down
5 changes: 5 additions & 0 deletions met-api/src/met_api/schemas/engagement_status_block.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Engagement status schema class."""

from marshmallow import EXCLUDE, Schema, fields
from marshmallow_enum import EnumField

Expand All @@ -15,3 +16,7 @@ class Meta: # pylint: disable=too-few-public-methods

survey_status = EnumField(SubmissionStatus, by_value=False)
block_text = fields.Str(data_key='block_text')
button_text = fields.Str(data_key='button_text')
link_type = fields.Str(data_key='link_type')
internal_link = fields.Str(data_key='internal_link', allow_none=True)
external_link = fields.Str(data_key='external_link', allow_none=True)
Loading

0 comments on commit 8874c5f

Please sign in to comment.