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

start of survey datamodel

parent 980ff6cf
Branches
Tags
1 merge request!43Feature/comp 205 create survey datamodel
import logging
#from typing import Optional
from typing import Dict, Any
from typing_extensions import Annotated
#from sqlalchemy import String
from sqlalchemy.orm import Mapped, mapped_column, relationship
from sqlalchemy.schema import ForeignKey
from sqlalchemy.types import JSON
from compendium_v2.db import db
from compendium_v2.db.model import NREN
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)]
json = Annotated[Dict[str, Any], mapped_column(JSON)]
# Unfortunately flask-sqlalchemy doesnt fully support DeclarativeBase yet.
# See https://github.com/pallets-eco/flask-sqlalchemy/issues/1140
# mypy: disable-error-code="name-defined"
class Questions(db.Model):
__tablename__ = 'questions'
year: Mapped[int_pk]
survey: Mapped[json]
# status column?
class Answers(db.Model):
__tablename__ = 'answers'
nren_id: Mapped[int_pk_fkNREN]
nren: Mapped[NREN] = relationship(lazy='joined')
question_year: Mapped[int_pk_fkQuestions]
question: Mapped[Questions] = relationship(lazy='joined')
answers: Mapped[json]
# completed column?? I think we need that..
\ No newline at end of file
"""add tables for questions and answers
Revision ID: 66a385dbb1fc
Revises: 42a826af0431
Create Date: 2023-06-13 14:59:59.339036
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '66a385dbb1fc'
down_revision = '42a826af0431'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table(
'questions',
sa.Column('year', sa.Integer(), nullable=False),
sa.Column('survey', sa.JSON(), nullable=False),
sa.PrimaryKeyConstraint('year', name=op.f('pk_questions'))
)
op.create_table(
'answers',
sa.Column('nren_id', sa.Integer(), nullable=False),
sa.Column('question_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'))
)
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('answers')
op.drop_table('questions')
# ### end Alembic commands ###
...@@ -10,6 +10,7 @@ from compendium_v2.routes.staff import routes as staff_routes ...@@ -10,6 +10,7 @@ from compendium_v2.routes.staff import routes as staff_routes
from compendium_v2.routes.organization import routes as org_routes from compendium_v2.routes.organization import routes as org_routes
from compendium_v2.routes.ec_projects import routes as ec_routes from compendium_v2.routes.ec_projects import routes as ec_routes
from compendium_v2.routes.policy import routes as policy from compendium_v2.routes.policy import routes as policy
from compendium_v2.routes.survey import routes as survey
routes = Blueprint('compendium-v2-api', __name__) routes = Blueprint('compendium-v2-api', __name__)
routes.register_blueprint(budget_routes, url_prefix='/budget') routes.register_blueprint(budget_routes, url_prefix='/budget')
...@@ -19,6 +20,7 @@ routes.register_blueprint(staff_routes, url_prefix='/staff') ...@@ -19,6 +20,7 @@ routes.register_blueprint(staff_routes, url_prefix='/staff')
routes.register_blueprint(org_routes, url_prefix='/organization') routes.register_blueprint(org_routes, url_prefix='/organization')
routes.register_blueprint(ec_routes, url_prefix='/ec-project') routes.register_blueprint(ec_routes, url_prefix='/ec-project')
routes.register_blueprint(policy, url_prefix='/policy') routes.register_blueprint(policy, url_prefix='/policy')
routes.register_blueprint(survey, url_prefix='/survey')
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
......
import logging
from flask import Blueprint, jsonify, request
from sqlalchemy import select
from compendium_v2.db import db
from compendium_v2.db.survey_model import Questions, Answers
from compendium_v2.routes import common
from typing import Any
routes = Blueprint('survey', __name__)
logger = logging.getLogger(__name__)
# TODO (partial) schemas
@routes.route('/open', methods=['GET'])
@common.require_accepts_json
def open_survey() -> Any:
entries = [entry for entry in db.session.scalars(select(Questions))]
entries2 = [entry for entry in db.session.scalars(select(Answers))]
# TODO check if the survey was already started, if so, continue with that survey:
# survey_continue = {
# "questions": entries,
# "data": "TODO",
# # TODO page number
# "unvalidated": ["TODO also take this from the db"] # TODO or maybe do it in another way..
# }
survey_start = {
"questions": entries,
"data": "TODO take from db last year",
"unvalidated": ["TODO list question names from last_year_data"] # TODO or maybe do it in another way..
}
return jsonify(survey_start)
@routes.route('/save', methods=['POST'])
@common.require_accepts_json
def save_survey() -> Any:
data = request.json
# TODO store stuff in db
return jsonify(data), 201
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment