From 699c3eb619e05d1bfb85db9eba3b01757a2a8489 Mon Sep 17 00:00:00 2001 From: jasquat Date: Thu, 4 May 2023 12:51:15 -0400 Subject: [PATCH] updated active user db model w/ burnettk --- .../versions/{a41f08815751_.py => 6aa02463da9c_.py} | 10 ++++++---- .../src/spiffworkflow_backend/models/active_user.py | 2 +- .../routes/active_users_controller.py | 4 +++- 3 files changed, 10 insertions(+), 6 deletions(-) rename spiffworkflow-backend/migrations/versions/{a41f08815751_.py => 6aa02463da9c_.py} (79%) diff --git a/spiffworkflow-backend/migrations/versions/a41f08815751_.py b/spiffworkflow-backend/migrations/versions/6aa02463da9c_.py similarity index 79% rename from spiffworkflow-backend/migrations/versions/a41f08815751_.py rename to spiffworkflow-backend/migrations/versions/6aa02463da9c_.py index 6ebbf66a7..ed75245d7 100644 --- a/spiffworkflow-backend/migrations/versions/a41f08815751_.py +++ b/spiffworkflow-backend/migrations/versions/6aa02463da9c_.py @@ -1,8 +1,8 @@ """empty message -Revision ID: a41f08815751 +Revision ID: 6aa02463da9c Revises: 68adb1d504e1 -Create Date: 2023-05-03 16:51:35.252279 +Create Date: 2023-05-04 12:50:07.979692 """ from alembic import op @@ -10,7 +10,7 @@ import sqlalchemy as sa # revision identifiers, used by Alembic. -revision = 'a41f08815751' +revision = '6aa02463da9c' down_revision = '68adb1d504e1' branch_labels = None depends_on = None @@ -22,12 +22,13 @@ def upgrade(): sa.Column('id', sa.Integer(), nullable=False), sa.Column('user_id', sa.Integer(), nullable=False), sa.Column('last_visited_identifier', sa.String(length=255), nullable=False), - sa.Column('last_seen_in_seconds', sa.Integer(), nullable=True), + sa.Column('last_seen_in_seconds', sa.Integer(), nullable=False), sa.ForeignKeyConstraint(['user_id'], ['user.id'], ), sa.PrimaryKeyConstraint('id'), sa.UniqueConstraint('user_id', 'last_visited_identifier', name='user_last_visited_unique') ) with op.batch_alter_table('active_user', schema=None) as batch_op: + batch_op.create_index(batch_op.f('ix_active_user_last_seen_in_seconds'), ['last_seen_in_seconds'], unique=False) batch_op.create_index(batch_op.f('ix_active_user_last_visited_identifier'), ['last_visited_identifier'], unique=False) batch_op.create_index(batch_op.f('ix_active_user_user_id'), ['user_id'], unique=False) @@ -39,6 +40,7 @@ def downgrade(): with op.batch_alter_table('active_user', schema=None) as batch_op: batch_op.drop_index(batch_op.f('ix_active_user_user_id')) batch_op.drop_index(batch_op.f('ix_active_user_last_visited_identifier')) + batch_op.drop_index(batch_op.f('ix_active_user_last_seen_in_seconds')) op.drop_table('active_user') # ### end Alembic commands ### diff --git a/spiffworkflow-backend/src/spiffworkflow_backend/models/active_user.py b/spiffworkflow-backend/src/spiffworkflow_backend/models/active_user.py index 943edc6fc..400e51da9 100644 --- a/spiffworkflow-backend/src/spiffworkflow_backend/models/active_user.py +++ b/spiffworkflow-backend/src/spiffworkflow_backend/models/active_user.py @@ -14,4 +14,4 @@ class ActiveUserModel(SpiffworkflowBaseDBModel): id: int = db.Column(db.Integer, primary_key=True) user_id: int = db.Column(ForeignKey(UserModel.id), nullable=False, index=True) # type: ignore last_visited_identifier: str = db.Column(db.String(255), nullable=False, index=True) - last_seen_in_seconds: int = db.Column(db.Integer) + last_seen_in_seconds: int = db.Column(db.Integer, nullable=False, index=True) diff --git a/spiffworkflow-backend/src/spiffworkflow_backend/routes/active_users_controller.py b/spiffworkflow-backend/src/spiffworkflow_backend/routes/active_users_controller.py index 496d9b064..73f881026 100644 --- a/spiffworkflow-backend/src/spiffworkflow_backend/routes/active_users_controller.py +++ b/spiffworkflow-backend/src/spiffworkflow_backend/routes/active_users_controller.py @@ -18,7 +18,9 @@ def active_user_updates(last_visited_identifier: str) -> Response: user_id=g.user.id, last_visited_identifier=last_visited_identifier ).first() if active_user is None: - active_user = ActiveUserModel(user_id=g.user.id, last_visited_identifier=last_visited_identifier) + active_user = ActiveUserModel( + user_id=g.user.id, last_visited_identifier=last_visited_identifier, last_seen_in_seconds=round(time.time()) + ) db.session.add(active_user) db.session.commit()