Skip to content

Commit

Permalink
DESENG-511: Survey translation model (#2406)
Browse files Browse the repository at this point in the history
  • Loading branch information
ratheesh-aot authored Mar 7, 2024
1 parent 122d391 commit 94a53bf
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""survey translation migration
Revision ID: 274a2774607b
Revises: e6c320c178fc
Create Date: 2024-03-05 13:41:19.539004
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = '274a2774607b'
down_revision = 'e6c320c178fc'
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('survey_translation',
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('survey_id', sa.Integer(), nullable=False),
sa.Column('language_id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(length=50), nullable=True),
sa.Column('form_json', postgresql.JSONB(astext_type=sa.Text()), server_default='{}', nullable=True),
sa.Column('created_by', sa.String(length=50), nullable=True),
sa.Column('updated_by', sa.String(length=50), nullable=True),
sa.ForeignKeyConstraint(['language_id'], ['language.id'], ),
sa.ForeignKeyConstraint(['survey_id'], ['survey.id'], ondelete='CASCADE'),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('survey_id', 'language_id', name='_survey_language_uc')
)
op.create_index(op.f('ix_survey_translation_name'), 'survey_translation', ['name'], unique=False)
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index(op.f('ix_survey_translation_name'), table_name='survey_translation')
op.drop_table('survey_translation')
# ### end Alembic commands ###
1 change: 1 addition & 0 deletions met-api/src/met_api/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,4 @@
from .poll_answers import PollAnswer
from .poll_responses import PollResponse
from .language import Language
from .survey_translation import SurveyTranslation
94 changes: 94 additions & 0 deletions met-api/src/met_api/models/survey_translation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
"""SurveyTranslation model class.
Manages the Survey Translations.
"""

from __future__ import annotations
from .base_model import BaseModel
from sqlalchemy.dialects import postgresql
from .db import db
from sqlalchemy import UniqueConstraint


class SurveyTranslation(BaseModel):
"""Definition of the SurveyTranslation entity."""

__tablename__ = 'survey_translation'

id = db.Column(db.Integer, primary_key=True, autoincrement=True)
survey_id = db.Column(
db.Integer,
db.ForeignKey('survey.id', ondelete='CASCADE'),
nullable=False,
)
language_id = db.Column(
db.Integer, db.ForeignKey('language.id'), nullable=False
)
name = db.Column(
db.String(50), index=True, nullable=True
) # pre-populate it with the base Survey content is optional so can be nullable
form_json = db.Column(
postgresql.JSONB(astext_type=db.Text()),
nullable=True,
server_default='{}',
) # pre-populate it with the base Survey content is optional so can be nullable

# Add a unique constraint on survey_id and language_id
# A survey has only one version in a particular language
__table_args__ = (
UniqueConstraint(
'survey_id', 'language_id', name='_survey_language_uc'
),
)

@staticmethod
def get_survey_translation_by_survey_and_language(
survey_id=None, language_id=None
):
"""Get survey translation by survey_id and language_id, or by either one."""
query = SurveyTranslation.query
if survey_id is not None:
query = query.filter_by(survey_id=survey_id)
if language_id is not None:
query = query.filter_by(language_id=language_id)

survey_translation_records = query.all()
return survey_translation_records

@staticmethod
def create_survey_translation(data):
"""Create a new survey translation."""
survey_translation = SurveyTranslation(
survey_id=data['survey_id'],
language_id=data['language_id'],
name=data.get(
'name'
), # Returns `None` if 'name' is not in `data` as its optional
form_json=data.get(
'form_json'
), # Returns `None` if 'form_json' is not in `data` as its optional
)
db.session.add(survey_translation)
db.session.commit()
return survey_translation

@staticmethod
def update_survey_translation(survey_translation_id, data):
"""Update an existing survey translation."""
survey_translation = SurveyTranslation.query.get(survey_translation_id)
if survey_translation:
for key, value in data.items():
setattr(survey_translation, key, value)
db.session.commit()
return survey_translation
return None

@staticmethod
def delete_survey_translation(survey_translation_id):
"""Delete a survey translation."""
survey_translation = SurveyTranslation.query.get(survey_translation_id)
if survey_translation:
db.session.delete(survey_translation)
db.session.commit()
return True
return False

0 comments on commit 94a53bf

Please sign in to comment.