diff --git a/compendium_v2/db/survey_model.py b/compendium_v2/db/survey_model.py index 1b388fb24b04f049ddccfb6bd29a79e7bd14383a..420312ee97ee36aaba31a37adf7218ff3732d945 100644 --- a/compendium_v2/db/survey_model.py +++ b/compendium_v2/db/survey_model.py @@ -17,7 +17,7 @@ logger = logging.getLogger(__name__) int_pk = Annotated[int, mapped_column(primary_key=True)] int_pk_fkNREN = Annotated[int, mapped_column(ForeignKey("nren.id"), primary_key=True)] -int_pk_fkQuestions = Annotated[int, mapped_column(ForeignKey("questions.year"), primary_key=True)] +int_pk_fkSurvey = Annotated[int, mapped_column(ForeignKey("survey.year"), primary_key=True)] json = Annotated[Dict[str, Any], mapped_column(JSON)] @@ -25,18 +25,18 @@ json = Annotated[Dict[str, Any], mapped_column(JSON)] # See https://github.com/pallets-eco/flask-sqlalchemy/issues/1140 # mypy: disable-error-code="name-defined" -class Questions(db.Model): - __tablename__ = 'questions' +class Survey(db.Model): + __tablename__ = 'survey' year: Mapped[int_pk] survey: Mapped[json] # status column? -class Answers(db.Model): - __tablename__ = 'answers' +class SurveyResponse(db.Model): + __tablename__ = 'survey_response' nren_id: Mapped[int_pk_fkNREN] nren: Mapped[NREN] = relationship(lazy='joined') - question_year: Mapped[int_pk_fkQuestions] - question: Mapped[Questions] = relationship(lazy='joined') + survey_year: Mapped[int_pk_fkSurvey] + survey: Mapped[Survey] = relationship(lazy='joined') answers: Mapped[json] # completed column?? I think we need that.. diff --git a/compendium_v2/migrations/versions/66a385dbb1fc_add_tables_for_questions_and_answers.py b/compendium_v2/migrations/versions/366171c7ba9e_add_survey_and_response_tables.py similarity index 55% rename from compendium_v2/migrations/versions/66a385dbb1fc_add_tables_for_questions_and_answers.py rename to compendium_v2/migrations/versions/366171c7ba9e_add_survey_and_response_tables.py index 101853bb82b1d359a4e852be87232ec75221bb52..24247ad8d77cccc69940b4c9b5931eae20942828 100644 --- a/compendium_v2/migrations/versions/66a385dbb1fc_add_tables_for_questions_and_answers.py +++ b/compendium_v2/migrations/versions/366171c7ba9e_add_survey_and_response_tables.py @@ -1,8 +1,8 @@ -"""add tables for questions and answers +"""Add survey and response tables -Revision ID: 66a385dbb1fc +Revision ID: 366171c7ba9e Revises: 42a826af0431 -Create Date: 2023-06-13 14:59:59.339036 +Create Date: 2023-06-15 11:33:28.549679 """ from alembic import op @@ -10,7 +10,7 @@ import sqlalchemy as sa # revision identifiers, used by Alembic. -revision = '66a385dbb1fc' +revision = '366171c7ba9e' down_revision = '42a826af0431' branch_labels = None depends_on = None @@ -19,25 +19,25 @@ depends_on = None def upgrade(): # ### commands auto generated by Alembic - please adjust! ### op.create_table( - 'questions', + 'survey', sa.Column('year', sa.Integer(), nullable=False), sa.Column('survey', sa.JSON(), nullable=False), - sa.PrimaryKeyConstraint('year', name=op.f('pk_questions')) + sa.PrimaryKeyConstraint('year', name=op.f('pk_survey')) ) op.create_table( - 'answers', + 'survey_response', sa.Column('nren_id', sa.Integer(), nullable=False), - sa.Column('question_year', sa.Integer(), nullable=False), + sa.Column('survey_year', sa.Integer(), nullable=False), sa.Column('answers', sa.JSON(), nullable=False), - sa.ForeignKeyConstraint(['nren_id'], ['nren.id'], name=op.f('fk_answers_nren_id_nren')), - sa.ForeignKeyConstraint(['question_year'], ['questions.year'], name=op.f('fk_answers_question_year_questions')), - sa.PrimaryKeyConstraint('nren_id', 'question_year', name=op.f('pk_answers')) + sa.ForeignKeyConstraint(['nren_id'], ['nren.id'], name=op.f('fk_survey_response_nren_id_nren')), + sa.ForeignKeyConstraint(['survey_year'], ['survey.year'], name=op.f('fk_survey_response_survey_year_survey')), + sa.PrimaryKeyConstraint('nren_id', 'survey_year', name=op.f('pk_survey_response')) ) # ### end Alembic commands ### def downgrade(): # ### commands auto generated by Alembic - please adjust! ### - op.drop_table('answers') - op.drop_table('questions') + op.drop_table('survey_response') + op.drop_table('survey') # ### end Alembic commands ### diff --git a/compendium_v2/routes/survey.py b/compendium_v2/routes/survey.py index ec807a6769b5a7520aac4ca5d76b0c393f8e542b..cff542f0db83dbb68de8ea1b69fd01e495be1014 100644 --- a/compendium_v2/routes/survey.py +++ b/compendium_v2/routes/survey.py @@ -8,7 +8,7 @@ from sqlalchemy import select from compendium_v2.db import db from compendium_v2.db.model import NREN -from compendium_v2.db.survey_model import Questions, Answers +from compendium_v2.db.survey_model import Survey, SurveyResponse from compendium_v2.routes import common @@ -27,12 +27,12 @@ def open_survey() -> Any: nren = db.session.execute(select(NREN).order_by(NREN.id).limit(1)).scalar_one() year = 1988 - questions = db.session.scalar(select(Questions).where(Questions.year == year)) - if questions is None or questions.survey == {}: + survey = db.session.scalar(select(Survey).where(Survey.year == year)) + if survey is None or survey.survey == {}: # TODO remove this at some point, its just convenient for now while we are changing the survey model a lot p = Path(__file__).with_name('survey_model.json') with p.open('r') as f: - questions = json.load(f) + survey = json.load(f) # TODO add some magic strings in the json (like the year) and interpolate them here @@ -40,23 +40,23 @@ def open_survey() -> Any: page = 0 unvalidated: List[str] = [] # or should we keep track of what _was_ validated? - answers = db.session.scalar( - select(Answers).where(Answers.question_year == year).where(Answers.nren_id == nren.id) + response = db.session.scalar( + select(SurveyResponse).where(SurveyResponse.survey_year == year).where(SurveyResponse.nren_id == nren.id) ) - previous_answers = db.session.scalar( - select(Answers).where(Answers.question_year == year - 1).where(Answers.nren_id == nren.id) + previous_response = db.session.scalar( + select(SurveyResponse).where(SurveyResponse.survey_year == year - 1).where(SurveyResponse.nren_id == nren.id) ) - if answers: - data = answers.answers["data"] - page = answers.answers["page"] - unvalidated = answers.answers["unvalidated"] - elif previous_answers: - data = previous_answers.answers["data"] + if response: + data = response.answers["data"] + page = response.answers["page"] + unvalidated = response.answers["unvalidated"] + elif previous_response: + data = previous_response.answers["data"] unvalidated = ["TODO everything?"] open_survey: dict = { - "model": questions, + "model": survey, "data": data, "page": page, "unvalidated": unvalidated @@ -72,23 +72,23 @@ def save_survey() -> Any: # just a hardcoded year and nren for development for now nren = db.session.execute(select(NREN).order_by(NREN.id).limit(1)).scalar_one() year = 1988 - questions = db.session.scalar(select(Questions).where(Questions.year == year)) - if questions is None: - questions = Questions(year=year, survey={}) - db.session.add(questions) + survey = db.session.scalar(select(Survey).where(Survey.year == year)) + if survey is None: + survey = Survey(year=year, survey={}) + db.session.add(survey) - answers = db.session.scalar( - select(Answers).where(Answers.question_year == year).where(Answers.nren_id == nren.id) + response = db.session.scalar( + select(SurveyResponse).where(SurveyResponse.survey_year == year).where(SurveyResponse.nren_id == nren.id) ) - if answers is None: - answers = Answers(question_year=year, nren_id=nren.id) - db.session.add(answers) + if response is None: + response = SurveyResponse(survey_year=year, nren_id=nren.id) + db.session.add(response) save_survey = request.json if not save_survey: raise Exception("Invalid format") - answers.answers = { + response.answers = { "data": save_survey["data"], "page": save_survey["page"], "unvalidated": save_survey["unvalidated"]