diff --git a/met-api/migrations/versions/274a2774607b_survey_translation_migration.py b/met-api/migrations/versions/274a2774607b_survey_translation_migration.py new file mode 100644 index 000000000..0624c5058 --- /dev/null +++ b/met-api/migrations/versions/274a2774607b_survey_translation_migration.py @@ -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 ### diff --git a/met-api/src/met_api/models/__init__.py b/met-api/src/met_api/models/__init__.py index 850657c65..566c38d3a 100644 --- a/met-api/src/met_api/models/__init__.py +++ b/met-api/src/met_api/models/__init__.py @@ -59,3 +59,4 @@ from .poll_answers import PollAnswer from .poll_responses import PollResponse from .language import Language +from .survey_translation import SurveyTranslation \ No newline at end of file diff --git a/met-api/src/met_api/models/survey_translation.py b/met-api/src/met_api/models/survey_translation.py new file mode 100644 index 000000000..b6cadf12c --- /dev/null +++ b/met-api/src/met_api/models/survey_translation.py @@ -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