-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feature/deseng-444-engagement-metadata-api
- Loading branch information
Showing
35 changed files
with
1,640 additions
and
97 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
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
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
83 changes: 83 additions & 0 deletions
83
met-api/migrations/versions/08f69642b7ae_adding_widget_poll.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,83 @@ | ||
"""adding_widget_poll | ||
Revision ID: 08f69642b7ae | ||
Revises: bd0eb0d25caf | ||
Create Date: 2024-01-16 14:25:07.611485 | ||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
from sqlalchemy.dialects import postgresql | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = '08f69642b7ae' | ||
down_revision = 'bd0eb0d25caf' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.create_table('widget_polls', | ||
sa.Column('created_date', sa.DateTime(), nullable=False), | ||
sa.Column('updated_date', sa.DateTime(), nullable=True), | ||
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False), | ||
sa.Column('title', sa.String(length=255), nullable=False), | ||
sa.Column('description', sa.String(length=2048), nullable=True), | ||
sa.Column('status', sa.Enum('active', 'inactive', name='poll_status'), nullable=True), | ||
sa.Column('widget_id', sa.Integer(), nullable=False), | ||
sa.Column('engagement_id', sa.Integer(), nullable=False), | ||
sa.Column('created_by', sa.String(length=50), nullable=True), | ||
sa.Column('updated_by', sa.String(length=50), nullable=True), | ||
sa.ForeignKeyConstraint(['engagement_id'], ['engagement.id'], ondelete='CASCADE'), | ||
sa.ForeignKeyConstraint(['widget_id'], ['widget.id'], ondelete='CASCADE'), | ||
sa.PrimaryKeyConstraint('id') | ||
) | ||
op.create_table('poll_answers', | ||
sa.Column('created_date', sa.DateTime(), nullable=False), | ||
sa.Column('updated_date', sa.DateTime(), nullable=True), | ||
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False), | ||
sa.Column('answer_text', sa.String(length=255), nullable=False), | ||
sa.Column('poll_id', sa.Integer(), nullable=False), | ||
sa.Column('created_by', sa.String(length=50), nullable=True), | ||
sa.Column('updated_by', sa.String(length=50), nullable=True), | ||
sa.ForeignKeyConstraint(['poll_id'], ['widget_polls.id'], ondelete='CASCADE'), | ||
sa.PrimaryKeyConstraint('id') | ||
) | ||
op.create_table('poll_responses', | ||
sa.Column('created_date', sa.DateTime(), nullable=False), | ||
sa.Column('updated_date', sa.DateTime(), nullable=True), | ||
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False), | ||
sa.Column('participant_id', sa.String(length=255), nullable=False), | ||
sa.Column('selected_answer_id', sa.Integer(), nullable=False), | ||
sa.Column('poll_id', sa.Integer(), nullable=False), | ||
sa.Column('widget_id', sa.Integer(), nullable=False), | ||
sa.Column('is_deleted', sa.Boolean(), nullable=True), | ||
sa.Column('created_by', sa.String(length=50), nullable=True), | ||
sa.Column('updated_by', sa.String(length=50), nullable=True), | ||
sa.ForeignKeyConstraint(['poll_id'], ['widget_polls.id'], ondelete='CASCADE'), | ||
sa.ForeignKeyConstraint(['selected_answer_id'], ['poll_answers.id'], ondelete='CASCADE'), | ||
sa.ForeignKeyConstraint(['widget_id'], ['widget.id'], ondelete='CASCADE'), | ||
sa.PrimaryKeyConstraint('id') | ||
) | ||
widget_type_table = sa.table('widget_type', | ||
sa.Column('id', sa.Integer), | ||
sa.Column('name', sa.String), | ||
sa.Column('description', sa.String)) | ||
|
||
op.bulk_insert(widget_type_table, [ | ||
{'id': 10, 'name': 'Poll', 'description': 'The Poll Widget enables real-time polling and feedback collection from public.'} | ||
]) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_table('poll_responses') | ||
op.drop_table('poll_answers') | ||
op.drop_table('widget_polls') | ||
|
||
conn = op.get_bind() | ||
|
||
conn.execute('DELETE FROM widget_type WHERE id=10') | ||
# ### 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
""" | ||
PollAnswers model class. | ||
Manages the Poll answers | ||
""" | ||
from __future__ import annotations | ||
|
||
from sqlalchemy.sql.schema import ForeignKey | ||
|
||
from .base_model import BaseModel | ||
from .db import db | ||
|
||
|
||
class PollAnswer(BaseModel): | ||
"""Definition of the PollAnswer entity.""" | ||
|
||
__tablename__ = 'poll_answers' | ||
id = db.Column(db.Integer, primary_key=True, autoincrement=True) | ||
answer_text = db.Column(db.String(255), nullable=False) | ||
poll_id = db.Column(db.Integer, ForeignKey('widget_polls.id', | ||
ondelete='CASCADE'), nullable=False) | ||
|
||
@classmethod | ||
def get_answers(cls, poll_id) -> list[PollAnswer]: | ||
"""Get answers for a poll.""" | ||
session = db.session.query(PollAnswer) | ||
return session.filter(PollAnswer.poll_id == poll_id).all() | ||
|
||
@classmethod | ||
def update_answer(cls, answer_id, answer_data: dict) -> PollAnswer: | ||
"""Update an answer.""" | ||
answer = PollAnswer.query.get(answer_id) | ||
if answer: | ||
for key, value in answer_data.items(): | ||
setattr(answer, key, value) | ||
answer.save() | ||
return answer | ||
|
||
@classmethod | ||
def delete_answers_by_poll_id(cls, poll_id): | ||
"""Delete answers.""" | ||
poll_answers = db.session.query(PollAnswer).filter( | ||
PollAnswer.poll_id == poll_id | ||
) | ||
poll_answers.delete() | ||
db.session.commit() | ||
|
||
@classmethod | ||
def bulk_insert_answers(cls, poll_id, answers): | ||
"""Bulk insert answers for a poll.""" | ||
answer_data = [ | ||
{'poll_id': poll_id, 'answer_text': answer['answer_text']} | ||
for answer in answers | ||
] | ||
db.session.bulk_insert_mappings(PollAnswer, answer_data) | ||
db.session.commit() |
Oops, something went wrong.