diff --git a/dbschema/migrations/versions/dcd3663ba17d_.py b/dbschema/migrations/versions/dcd3663ba17d_.py new file mode 100644 index 0000000..68b50d8 --- /dev/null +++ b/dbschema/migrations/versions/dcd3663ba17d_.py @@ -0,0 +1,67 @@ +"""empty message + +Revision ID: dcd3663ba17d +Revises: 258f980de9f2 +Create Date: 2022-12-05 08:45:44.942657 + +""" +from alembic import op +import sqlalchemy as sa +from sqlalchemy.dialects import postgresql + +# revision identifiers, used by Alembic. +revision = 'dcd3663ba17d' +down_revision = '258f980de9f2' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('closed_answers', + sa.Column('closed_answers_id', sa.Integer(), nullable=False), + sa.Column('question_id', sa.Integer(), nullable=True), + sa.Column('answer_value', sa.Integer(), nullable=True), + sa.Column('answer_description', sa.String(), nullable=True), + sa.ForeignKeyConstraint(['question_id'], ['dialog_questions.question_id'], ), + sa.PrimaryKeyConstraint('closed_answers_id') + ) + op.create_table('dialog_open_answers', + sa.Column('dialog_open_answers_id', sa.Integer(), nullable=False), + sa.Column('users_nicedayuid', sa.Integer(), nullable=True), + sa.Column('question_id', sa.Integer(), nullable=True), + sa.Column('answer_value', sa.String(), nullable=True), + sa.Column('datetime', sa.TIMESTAMP(timezone=True), nullable=True), + sa.ForeignKeyConstraint(['question_id'], ['dialog_questions.question_id'], ), + sa.ForeignKeyConstraint(['users_nicedayuid'], ['users.nicedayuid'], ), + sa.PrimaryKeyConstraint('dialog_open_answers_id') + ) + op.create_table('dialog_closed_answers', + sa.Column('dialog_closed_answers_id', sa.Integer(), nullable=False), + sa.Column('users_nicedayuid', sa.Integer(), nullable=True), + sa.Column('closed_answers_id', sa.Integer(), nullable=True), + sa.Column('datetime', sa.TIMESTAMP(timezone=True), nullable=True), + sa.ForeignKeyConstraint(['closed_answers_id'], ['closed_answers.closed_answers_id'], ), + sa.ForeignKeyConstraint(['users_nicedayuid'], ['users.nicedayuid'], ), + sa.PrimaryKeyConstraint('dialog_closed_answers_id') + ) + op.drop_table('dialog_answers') + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('dialog_answers', + sa.Column('answer_id', sa.INTEGER(), autoincrement=True, nullable=False), + sa.Column('users_nicedayuid', sa.INTEGER(), autoincrement=False, nullable=True), + sa.Column('answer', sa.VARCHAR(), autoincrement=False, nullable=True), + sa.Column('question_id', sa.INTEGER(), autoincrement=False, nullable=True), + sa.Column('datetime', postgresql.TIMESTAMP(timezone=True), autoincrement=False, nullable=True), + sa.ForeignKeyConstraint(['question_id'], ['dialog_questions.question_id'], name='dialog_answers_question_id_fkey'), + sa.ForeignKeyConstraint(['users_nicedayuid'], ['users.nicedayuid'], name='dialog_answers_users_nicedayuid_fkey'), + sa.PrimaryKeyConstraint('answer_id', name='dialog_answers_pkey') + ) + op.drop_table('dialog_closed_answers') + op.drop_table('dialog_open_answers') + op.drop_table('closed_answers') + # ### end Alembic commands ### diff --git a/dbschema/models.py b/dbschema/models.py index ebebc5c..0d29ed4 100644 --- a/dbschema/models.py +++ b/dbschema/models.py @@ -17,23 +17,46 @@ class Users(Base): dob = Column(Date) # Refer to relationships - dialog_answers = relationship('DialogAnswers') + dialog_closed_answers = relationship('DialogClosedAnswers') + dialog_open_answers = relationship('DialogOpenAnswers') user_intervention_state = relationship("UserInterventionState", back_populates="user") user_preferences = relationship("UserPreferences", back_populates="user") first_aid_kit = relationship("FirstAidKit", back_populates="user") intervention_activities_performed = relationship("InterventionActivitiesPerformed", back_populates="user") -class DialogAnswers(Base): - __tablename__ = 'dialog_answers' - answer_id = Column(Integer, primary_key=True) +class DialogClosedAnswers(Base): + __tablename__ = 'dialog_closed_answers' + dialog_closed_answers_id = Column(Integer, primary_key=True) + users_nicedayuid = Column(Integer, ForeignKey('users.nicedayuid')) + closed_answers_id = Column(Integer, ForeignKey('closed_answers.closed_answers_id')) + datetime = Column(TIMESTAMP(timezone=True), default=func.now()) + + # Refer relationship + closed_answers = relationship('ClosedAnswers') + + +class DialogOpenAnswers(Base): + __tablename__ = 'dialog_open_answers' + dialog_open_answers_id = Column(Integer, primary_key=True) users_nicedayuid = Column(Integer, ForeignKey('users.nicedayuid')) - answer = Column(String) question_id = Column(Integer, ForeignKey('dialog_questions.question_id')) + answer_value = Column(String) datetime = Column(TIMESTAMP(timezone=True), default=func.now()) # Refer relationship - dialog_questions = relationship('DialogQuestions') + dialog_questions = relationship("DialogQuestions", back_populates="dialog_open_answers") + + +class ClosedAnswers(Base): + __tablename__ = 'closed_answers' + closed_answers_id = Column(Integer, primary_key=True) + question_id = Column(Integer, ForeignKey('dialog_questions.question_id')) + answer_value = Column(Integer) + answer_description = Column(String) + + # Refer relationship + dialog_questions = relationship("DialogQuestions", back_populates="closed_answers") class DialogQuestions(Base): @@ -41,6 +64,10 @@ class DialogQuestions(Base): question_id = Column(Integer, primary_key=True) question_description = Column(String) + # Refer relationships + dialog_open_answers = relationship("DialogOpenAnswers", back_populates="dialog_questions") + closed_answers = relationship("ClosedAnswers", back_populates="dialog_questions") + class FirstAidKit(Base): __tablename__ = "first_aid_kit" diff --git a/helper/definitions.py b/helper/definitions.py index 6ac9e8b..fbbdd56 100644 --- a/helper/definitions.py +++ b/helper/definitions.py @@ -30,6 +30,7 @@ class ExecutionInterventionComponents(str, Enum): GENERAL_ACTIVITY = 'general_activity' WEEKLY_REFLECTION = 'weekly_reflection' DAILY_REFLECTION = 'daily_reflection' + RELAPSE_DIALOG = 'relapse_dialog' class ExecutionInterventionComponentsTriggers(str, Enum): @@ -37,3 +38,33 @@ class ExecutionInterventionComponentsTriggers(str, Enum): GENERAL_ACTIVITY = 'EXTERNAL_trigger_general_activity' WEEKLY_REFLECTION = 'EXTERNAL_weekly_reflection' DAILY_REFLECTION = 'EXTERNAL_daily_reflection' + RELAPSE_DIALOG = 'EXTERNAL_relapse_dialog' + + +class DialogQuestionsEnum(Enum): + FUTURE_SELF_SMOKER_WORDS = 1 # Which three words suits you as smoker? + FUTURE_SELF_SMOKER_WHY = 2 # Why did you pick these words for smoking? + FUTURE_SELF_MOVER_WORDS = 3 # Which three words suits you as exerciser? + FUTURE_SELF_MOVER_WHY = 4 # Why did you pick these words for exercising? + FUTURE_SELF_I_SEE_MYSELF_AS_SMOKER = 5 # I see myself as smoker, non-smoker or quitter + FUTURE_SELF_I_SEE_MYSELF_AS_MOVER = 6 # I see myself as active, bit active or not active + RELAPSE_CRAVING_WHAT_DOING = 7 + RELAPSE_CRAVING_HOW_FEEL = 8 + RELAPSE_CRAVING_WITH_WHOM = 9 + RELAPSE_CRAVING_HAPPENED_SPECIAL = 10 + RELAPSE_LAPSE_TYPE_SMOKE = 11 + RELAPSE_LAPSE_NUMBER_CIGARETTES = 12 + RELAPSE_LAPSE_WHAT_DOING = 13 + RELAPSE_LAPSE_HOW_FEEL = 14 + RELAPSE_LAPSE_WITH_WHOM = 15 + RELAPSE_LAPSE_HAPPENED_SPECIAL = 16 + RELAPSE_RELAPSE_TYPE_SMOKE = 17 + RELAPSE_RELAPSE_NUMBER_CIGARETTES = 18 + RELAPSE_RELAPSE_WHAT_DOING = 19 + RELAPSE_RELAPSE_HOW_FEEL = 20 + RELAPSE_RELAPSE_WITH_WHOM = 21 + RELAPSE_RELAPSE_HAPPENED_SPECIAL = 22 + RELAPSE_PA_TOGETHER = 23 + RELAPSE_PA_WHY_FAIL = 24 + RELAPSE_PA_DOING_TODAY = 25 + RELAPSE_PA_HAPPENED_SPECIAL = 26 diff --git a/helper/populate_db.py b/helper/populate_db.py index 0f6636f..d5d5aaf 100644 --- a/helper/populate_db.py +++ b/helper/populate_db.py @@ -4,12 +4,13 @@ from datetime import datetime, date, time, timedelta from dateutil import tz -from dbschema.models import (Users, UserInterventionState, DialogQuestions, DialogAnswers, +from dbschema.models import (Users, UserInterventionState, DialogQuestions, DialogOpenAnswers, DialogClosedAnswers, FirstAidKit, InterventionActivity, InterventionComponents, InterventionPhases, - UserPreferences, InterventionActivitiesPerformed) + UserPreferences, InterventionActivitiesPerformed, ClosedAnswers) from helper.helper_functions import get_db_session from helper.definitions import (Phases, PreparationInterventionComponents, PreparationInterventionComponentsTriggers, - ExecutionInterventionComponents, ExecutionInterventionComponentsTriggers) + ExecutionInterventionComponents, ExecutionInterventionComponentsTriggers, + DialogQuestionsEnum) tz_nl = tz.gettz("Europe/Amsterdam") @@ -22,6 +23,10 @@ def populate_db_with_test_data(session, test_user_id, activities_file_path='../u # Fill question table objects_questions = initialize_questions() [session.merge(obj) for obj in objects_questions] + + # Fill closed answers table + objects_closed_answers = initialize_closed_anwers() + [session.merge(obj) for obj in objects_closed_answers] # Fill in intervention activities (placeholder activities for now) objects_intervention_activities = initialize_activities(activities_file_path) @@ -44,17 +49,139 @@ def populate_db_with_test_data(session, test_user_id, activities_file_path='../u def initialize_questions(): data = [ - DialogQuestions(question_id=1, question_description='future self dialog - smoker words'), - DialogQuestions(question_id=2, question_description='future self dialog - smoker why'), - DialogQuestions(question_id=3, question_description='future self dialog - mover words'), - DialogQuestions(question_id=4, question_description='future self dialog - mover why'), - DialogQuestions(question_id=5, question_description='future self dialog - smoker identity'), - DialogQuestions(question_id=6, question_description='future self dialog - mover identity') + DialogQuestions(question_id=DialogQuestionsEnum.FUTURE_SELF_SMOKER_WORDS.value, + question_description='future self dialog - smoker words'), + DialogQuestions(question_id=DialogQuestionsEnum.FUTURE_SELF_SMOKER_WHY.value, + question_description='future self dialog - smoker why'), + DialogQuestions(question_id=DialogQuestionsEnum.FUTURE_SELF_MOVER_WORDS.value, + question_description='future self dialog - mover words'), + DialogQuestions(question_id=DialogQuestionsEnum.FUTURE_SELF_MOVER_WHY.value, + question_description='future self dialog - mover why'), + DialogQuestions(question_id=DialogQuestionsEnum.FUTURE_SELF_I_SEE_MYSELF_AS_SMOKER.value, + question_description='future self dialog - smoker identity'), + DialogQuestions(question_id=DialogQuestionsEnum.FUTURE_SELF_I_SEE_MYSELF_AS_MOVER.value, + question_description='future self dialog - mover identity'), + DialogQuestions(question_id=DialogQuestionsEnum.RELAPSE_CRAVING_WHAT_DOING.value, + question_description='relapse dialog - craving - what doing'), + DialogQuestions(question_id=DialogQuestionsEnum.RELAPSE_CRAVING_HOW_FEEL.value, + question_description='relapse dialog - craving - how feel'), + DialogQuestions(question_id=DialogQuestionsEnum.RELAPSE_CRAVING_WITH_WHOM.value, + question_description='relapse dialog - craving - with whom'), + DialogQuestions(question_id=DialogQuestionsEnum.RELAPSE_CRAVING_HAPPENED_SPECIAL.value, + question_description='relapse dialog - craving - happened special'), + DialogQuestions(question_id=DialogQuestionsEnum.RELAPSE_LAPSE_TYPE_SMOKE.value, + question_description='relapse dialog - smoke lapse - type cigarettes'), + DialogQuestions(question_id=DialogQuestionsEnum.RELAPSE_LAPSE_NUMBER_CIGARETTES.value, + question_description='relapse dialog - smoke lapse - number cigarettes'), + DialogQuestions(question_id=DialogQuestionsEnum.RELAPSE_LAPSE_WHAT_DOING.value, + question_description='relapse dialog - smoke lapse - what doing'), + DialogQuestions(question_id=DialogQuestionsEnum.RELAPSE_LAPSE_HOW_FEEL.value, + question_description='relapse dialog - smoke lapse - how feel'), + DialogQuestions(question_id=DialogQuestionsEnum.RELAPSE_LAPSE_WITH_WHOM.value, + question_description='relapse dialog - smoke lapse - with whom'), + DialogQuestions(question_id=DialogQuestionsEnum.RELAPSE_LAPSE_HAPPENED_SPECIAL.value, + question_description='relapse dialog - smoke lapse - happened special'), + DialogQuestions(question_id=DialogQuestionsEnum.RELAPSE_RELAPSE_TYPE_SMOKE.value, + question_description='relapse dialog - smoke relapse - type cigarettes'), + DialogQuestions(question_id=DialogQuestionsEnum.RELAPSE_RELAPSE_NUMBER_CIGARETTES.value, + question_description='relapse dialog - smoke relapse - number cigarettes'), + DialogQuestions(question_id=DialogQuestionsEnum.RELAPSE_RELAPSE_WHAT_DOING.value, + question_description='relapse dialog - smoke relapse - what doing'), + DialogQuestions(question_id=DialogQuestionsEnum.RELAPSE_RELAPSE_HOW_FEEL.value, + question_description='relapse dialog - smoke relapse - how feel'), + DialogQuestions(question_id=DialogQuestionsEnum.RELAPSE_RELAPSE_WITH_WHOM.value, + question_description='relapse dialog - smoke relapse - with whom'), + DialogQuestions(question_id=DialogQuestionsEnum.RELAPSE_RELAPSE_HAPPENED_SPECIAL.value, + question_description='relapse dialog - smoke relapse - happened special'), + DialogQuestions(question_id=DialogQuestionsEnum.RELAPSE_PA_TOGETHER.value, + question_description='relapse dialog - pa - together'), + DialogQuestions(question_id=DialogQuestionsEnum.RELAPSE_PA_WHY_FAIL.value, + question_description='relapse dialog - pa - why fail'), + DialogQuestions(question_id=DialogQuestionsEnum.RELAPSE_PA_DOING_TODAY.value, + question_description='relapse dialog - pa - doing today'), + DialogQuestions(question_id=DialogQuestionsEnum.RELAPSE_PA_HAPPENED_SPECIAL.value, + question_description='relapse dialog - pa - happened special'), ] return data +def initialize_closed_anwers(): + answer_descriptions = {} + answer_descriptions[DialogQuestionsEnum.RELAPSE_CRAVING_WHAT_DOING.value] = ['Aan het werk', + 'Thuis bezig met klusjes of' + ' huishouden', + 'Iets voor jezelf (hobby, met iemand' + ' afspreken)', + 'Aan het eten of drinken', + 'Net alcohol of koffie gedronken', + 'Net wakker geworden', + 'Iets anders'] + answer_descriptions[DialogQuestionsEnum.RELAPSE_CRAVING_HOW_FEEL.value] = ['Stress', 'Verdrietig', 'Boos', + 'Verveeld', 'Honger', 'Bang of angstig', + 'Blij', 'Iets anders'] + answer_descriptions[DialogQuestionsEnum.RELAPSE_CRAVING_WITH_WHOM.value] = ['Met partner', 'Alleen', + 'Met vrienden of famillie', + 'Met kenissen', 'Met collega`s', + 'Met andere rokers'] + answer_descriptions[DialogQuestionsEnum.RELAPSE_LAPSE_TYPE_SMOKE.value] = ['Sigaretten', 'e-sigaretten', 'shags' + 'iets anders'] + answer_descriptions[DialogQuestionsEnum.RELAPSE_LAPSE_WHAT_DOING.value] = ['Aan het werk', + 'Thuis bezig met klusjes of huishouden', + 'Iets voor jezelf (hobby, met iemand' + ' afspreken)', + 'Aan het eten of drinken', + 'Net alcohol of koffie gedronken', + 'Net wakker geworden', + 'Iets anders'] + answer_descriptions[DialogQuestionsEnum.RELAPSE_LAPSE_HOW_FEEL.value] = ['Schuldig', 'Vervelend', 'Verdrietig', + 'Je had het gevoel dat het niet zou lukken' + ' om te stoppen met roken', + 'Opgelucht'] + answer_descriptions[DialogQuestionsEnum.RELAPSE_LAPSE_WITH_WHOM.value] = ['Met partner', 'Alleen', + 'Met vrienden of famillie', + 'Met kenissen', 'Met collega`s', + 'Met andere rokers'] + answer_descriptions[DialogQuestionsEnum.RELAPSE_RELAPSE_TYPE_SMOKE.value] = ['Sigaretten', 'e-sigaretten', 'shags', + 'iets anders'] + answer_descriptions[DialogQuestionsEnum.RELAPSE_RELAPSE_WHAT_DOING.value] = ['Aan het werk', + 'Thuis bezig met klusjes of' + ' huishouden', + 'Iets voor jezelf (hobby, met iemand' + ' afspreken)', + 'Aan het eten of drinken', + 'Net alcohol of koffie gedronken', + 'Net wakker geworden', + 'Iets anders'] + answer_descriptions[DialogQuestionsEnum.RELAPSE_RELAPSE_HOW_FEEL.value] = ['Schuldig', 'Vervelend', 'Verdrietig', + 'Je had het gevoel dat het niet zou' + ' lukken om te stoppen met roken', + 'Opgelucht'] + answer_descriptions[DialogQuestionsEnum.RELAPSE_RELAPSE_WITH_WHOM.value] = ['Met partner', 'Alleen', + 'Met vrienden of famillie', + 'Met kenissen', 'Met collega`s', + 'Met andere rokers'] + answer_descriptions[DialogQuestionsEnum.RELAPSE_PA_TOGETHER.value] = ['Ja', 'Nee'] + answer_descriptions[DialogQuestionsEnum.RELAPSE_PA_WHY_FAIL.value] = ['Geen zin', 'Moe en geen energie', + 'Geen tijd', 'Er is iets tussen gekomen', + 'Ligt aan het weer', 'Ziek of geblesseerd', + 'Iets anders'] + answer_descriptions[DialogQuestionsEnum.RELAPSE_PA_DOING_TODAY.value] = ['Aan het werk', + 'Thuis bezig met klusjes of huishouden', + 'Iets voor jezelf (hobby, met iemand' + ' afspreken)', + 'Al lichamelijk actief geweest', + 'Iets anders'] + + data = [ClosedAnswers(closed_answers_id=q*100+i, + question_id=q, + answer_value=i, + answer_description=a) + for q in answer_descriptions + for i, a in enumerate(answer_descriptions[q], start=1)] + return data + + def initialize_activities(activities_file_path): with open(activities_file_path, newline='') as csvfile: csv_reader = csv.DictReader(csvfile) @@ -132,25 +259,20 @@ def create_test_data(user_id: int): user_activity_description="Eat as many carrots as I can.", datetime=datetime.now().astimezone(tz_nl), activity_rating=3), - DialogAnswers(users_nicedayuid=user_id, answer='Fijn plezierig helpt mij ', question_id=1, #0.93 - datetime=datetime.now().astimezone(tz_nl)), - DialogAnswers(users_nicedayuid=user_id, answer='Fijn plezierig prettig', question_id=1, #0.898 - datetime=datetime.now().astimezone(tz_nl)), - DialogAnswers(users_nicedayuid=user_id, answer='gezellig sexy duur', question_id=1, #0.576 - datetime=datetime.now().astimezone(tz_nl)), - DialogAnswers(users_nicedayuid=user_id, answer='onderdeel van mijn leven verslavend niet zo heel erg', question_id=1,#-0.592 - datetime=datetime.now().astimezone(tz_nl)), - # ongezond duur gevaarlijk -0.92 - - DialogAnswers(users_nicedayuid=user_id, answer='stressvol straf lastig ', question_id=3, #-0.926 - datetime=datetime.now().astimezone(tz_nl)), - DialogAnswers(users_nicedayuid=user_id, answer='lastig', question_id=3, #-0.546 - datetime=datetime.now().astimezone(tz_nl)), - DialogAnswers(users_nicedayuid=user_id, answer='moet voor mijn gezondheid prettig', question_id=3, #0.446 - datetime=datetime.now().astimezone(tz_nl)), - DialogAnswers(users_nicedayuid=user_id, answer='moet voor mijn gezondheid goed', question_id=3, #0.562 - datetime=datetime.now().astimezone(tz_nl)), - #cool gezellig prettig 0.726 + DialogOpenAnswers(users_nicedayuid=user_id, answer_value='Fijn plezierig helpt mij ', question_id=1, + datetime=datetime.now().astimezone(tz_nl)), + DialogOpenAnswers(users_nicedayuid=user_id, answer_value='onderdeel van mijn leven verslavend niet zo heel erg', + question_id=1, datetime=datetime.now().astimezone(tz_nl)), + DialogOpenAnswers(users_nicedayuid=user_id, answer_value='stressvol straf lastig ', question_id=3, + datetime=datetime.now().astimezone(tz_nl)), + DialogOpenAnswers(users_nicedayuid=user_id, answer_value='lastig', question_id=3, + datetime=datetime.now().astimezone(tz_nl)), + DialogOpenAnswers(users_nicedayuid=user_id, answer_value='moet voor mijn gezondheid prettig', question_id=3, + datetime=datetime.now().astimezone(tz_nl)), + DialogOpenAnswers(users_nicedayuid=user_id, answer_value='moet voor mijn gezondheid goed', question_id=3, + datetime=datetime.now().astimezone(tz_nl)), + DialogClosedAnswers(users_nicedayuid=user_id, closed_answers_id=803, datetime=datetime.now().astimezone(tz_nl)), + DialogClosedAnswers(users_nicedayuid=user_id, closed_answers_id=1401, datetime=datetime.now().astimezone(tz_nl)), UserInterventionState(users_nicedayuid=user_id, intervention_phase_id=1, intervention_component_id=5, completed=False, last_time=datetime.now().astimezone(tz_nl), last_part=1),