Skip to content
Snippets Groups Projects
Commit 02268529 authored by Remco Tukker's avatar Remco Tukker
Browse files

review comments

parent 2c5d6487
No related branches found
No related tags found
1 merge request!43Feature/comp 205 create survey datamodel
...@@ -17,7 +17,7 @@ logger = logging.getLogger(__name__) ...@@ -17,7 +17,7 @@ logger = logging.getLogger(__name__)
int_pk = Annotated[int, mapped_column(primary_key=True)] int_pk = Annotated[int, mapped_column(primary_key=True)]
int_pk_fkNREN = Annotated[int, mapped_column(ForeignKey("nren.id"), 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)] json = Annotated[Dict[str, Any], mapped_column(JSON)]
...@@ -25,18 +25,18 @@ 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 # See https://github.com/pallets-eco/flask-sqlalchemy/issues/1140
# mypy: disable-error-code="name-defined" # mypy: disable-error-code="name-defined"
class Questions(db.Model): class Survey(db.Model):
__tablename__ = 'questions' __tablename__ = 'survey'
year: Mapped[int_pk] year: Mapped[int_pk]
survey: Mapped[json] survey: Mapped[json]
# status column? # status column?
class Answers(db.Model): class SurveyResponse(db.Model):
__tablename__ = 'answers' __tablename__ = 'survey_response'
nren_id: Mapped[int_pk_fkNREN] nren_id: Mapped[int_pk_fkNREN]
nren: Mapped[NREN] = relationship(lazy='joined') nren: Mapped[NREN] = relationship(lazy='joined')
question_year: Mapped[int_pk_fkQuestions] survey_year: Mapped[int_pk_fkSurvey]
question: Mapped[Questions] = relationship(lazy='joined') survey: Mapped[Survey] = relationship(lazy='joined')
answers: Mapped[json] answers: Mapped[json]
# completed column?? I think we need that.. # completed column?? I think we need that..
"""add tables for questions and answers """Add survey and response tables
Revision ID: 66a385dbb1fc Revision ID: 366171c7ba9e
Revises: 42a826af0431 Revises: 42a826af0431
Create Date: 2023-06-13 14:59:59.339036 Create Date: 2023-06-15 11:33:28.549679
""" """
from alembic import op from alembic import op
...@@ -10,7 +10,7 @@ import sqlalchemy as sa ...@@ -10,7 +10,7 @@ import sqlalchemy as sa
# revision identifiers, used by Alembic. # revision identifiers, used by Alembic.
revision = '66a385dbb1fc' revision = '366171c7ba9e'
down_revision = '42a826af0431' down_revision = '42a826af0431'
branch_labels = None branch_labels = None
depends_on = None depends_on = None
...@@ -19,25 +19,25 @@ depends_on = None ...@@ -19,25 +19,25 @@ depends_on = None
def upgrade(): def upgrade():
# ### commands auto generated by Alembic - please adjust! ### # ### commands auto generated by Alembic - please adjust! ###
op.create_table( op.create_table(
'questions', 'survey',
sa.Column('year', sa.Integer(), nullable=False), sa.Column('year', sa.Integer(), nullable=False),
sa.Column('survey', sa.JSON(), 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( op.create_table(
'answers', 'survey_response',
sa.Column('nren_id', sa.Integer(), nullable=False), 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.Column('answers', sa.JSON(), nullable=False),
sa.ForeignKeyConstraint(['nren_id'], ['nren.id'], name=op.f('fk_answers_nren_id_nren')), sa.ForeignKeyConstraint(['nren_id'], ['nren.id'], name=op.f('fk_survey_response_nren_id_nren')),
sa.ForeignKeyConstraint(['question_year'], ['questions.year'], name=op.f('fk_answers_question_year_questions')), sa.ForeignKeyConstraint(['survey_year'], ['survey.year'], name=op.f('fk_survey_response_survey_year_survey')),
sa.PrimaryKeyConstraint('nren_id', 'question_year', name=op.f('pk_answers')) sa.PrimaryKeyConstraint('nren_id', 'survey_year', name=op.f('pk_survey_response'))
) )
# ### end Alembic commands ### # ### end Alembic commands ###
def downgrade(): def downgrade():
# ### commands auto generated by Alembic - please adjust! ### # ### commands auto generated by Alembic - please adjust! ###
op.drop_table('answers') op.drop_table('survey_response')
op.drop_table('questions') op.drop_table('survey')
# ### end Alembic commands ### # ### end Alembic commands ###
...@@ -8,7 +8,7 @@ from sqlalchemy import select ...@@ -8,7 +8,7 @@ from sqlalchemy import select
from compendium_v2.db import db from compendium_v2.db import db
from compendium_v2.db.model import NREN 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 from compendium_v2.routes import common
...@@ -27,12 +27,12 @@ def open_survey() -> Any: ...@@ -27,12 +27,12 @@ def open_survey() -> Any:
nren = db.session.execute(select(NREN).order_by(NREN.id).limit(1)).scalar_one() nren = db.session.execute(select(NREN).order_by(NREN.id).limit(1)).scalar_one()
year = 1988 year = 1988
questions = db.session.scalar(select(Questions).where(Questions.year == year)) survey = db.session.scalar(select(Survey).where(Survey.year == year))
if questions is None or questions.survey == {}: 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 # 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') p = Path(__file__).with_name('survey_model.json')
with p.open('r') as f: 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 # TODO add some magic strings in the json (like the year) and interpolate them here
...@@ -40,23 +40,23 @@ def open_survey() -> Any: ...@@ -40,23 +40,23 @@ def open_survey() -> Any:
page = 0 page = 0
unvalidated: List[str] = [] # or should we keep track of what _was_ validated? unvalidated: List[str] = [] # or should we keep track of what _was_ validated?
answers = db.session.scalar( response = db.session.scalar(
select(Answers).where(Answers.question_year == year).where(Answers.nren_id == nren.id) select(SurveyResponse).where(SurveyResponse.survey_year == year).where(SurveyResponse.nren_id == nren.id)
) )
previous_answers = db.session.scalar( previous_response = db.session.scalar(
select(Answers).where(Answers.question_year == year - 1).where(Answers.nren_id == nren.id) select(SurveyResponse).where(SurveyResponse.survey_year == year - 1).where(SurveyResponse.nren_id == nren.id)
) )
if answers: if response:
data = answers.answers["data"] data = response.answers["data"]
page = answers.answers["page"] page = response.answers["page"]
unvalidated = answers.answers["unvalidated"] unvalidated = response.answers["unvalidated"]
elif previous_answers: elif previous_response:
data = previous_answers.answers["data"] data = previous_response.answers["data"]
unvalidated = ["TODO everything?"] unvalidated = ["TODO everything?"]
open_survey: dict = { open_survey: dict = {
"model": questions, "model": survey,
"data": data, "data": data,
"page": page, "page": page,
"unvalidated": unvalidated "unvalidated": unvalidated
...@@ -72,23 +72,23 @@ def save_survey() -> Any: ...@@ -72,23 +72,23 @@ def save_survey() -> Any:
# just a hardcoded year and nren for development for now # just a hardcoded year and nren for development for now
nren = db.session.execute(select(NREN).order_by(NREN.id).limit(1)).scalar_one() nren = db.session.execute(select(NREN).order_by(NREN.id).limit(1)).scalar_one()
year = 1988 year = 1988
questions = db.session.scalar(select(Questions).where(Questions.year == year)) survey = db.session.scalar(select(Survey).where(Survey.year == year))
if questions is None: if survey is None:
questions = Questions(year=year, survey={}) survey = Survey(year=year, survey={})
db.session.add(questions) db.session.add(survey)
answers = db.session.scalar( response = db.session.scalar(
select(Answers).where(Answers.question_year == year).where(Answers.nren_id == nren.id) select(SurveyResponse).where(SurveyResponse.survey_year == year).where(SurveyResponse.nren_id == nren.id)
) )
if answers is None: if response is None:
answers = Answers(question_year=year, nren_id=nren.id) response = SurveyResponse(survey_year=year, nren_id=nren.id)
db.session.add(answers) db.session.add(response)
save_survey = request.json save_survey = request.json
if not save_survey: if not save_survey:
raise Exception("Invalid format") raise Exception("Invalid format")
answers.answers = { response.answers = {
"data": save_survey["data"], "data": save_survey["data"],
"page": save_survey["page"], "page": save_survey["page"],
"unvalidated": save_survey["unvalidated"] "unvalidated": save_survey["unvalidated"]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment