-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[To Feature ] DESENG-511: Survey translation model #2406
Merged
ratheesh-aot
merged 1 commit into
bcgov:DESENG-511-SurveyTranslation-Final
from
ratheesh-aot:DESENG-511-SurveyTranslation
Mar 7, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be
survey_translation.save()
instead, a method inherited from the BaseModelThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @NatSquared . Will follow that.