-
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.
DESENG-511: Survey translation model (#2406)
- Loading branch information
1 parent
122d391
commit 94a53bf
Showing
3 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
met-api/migrations/versions/274a2774607b_survey_translation_migration.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,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 ### |
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,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 |