From 53c7d5efbcde7bc13acb52dad20325a5ca7642d0 Mon Sep 17 00:00:00 2001 From: ScotterMonk Date: Tue, 15 Oct 2024 14:26:22 -0500 Subject: [PATCH] Implement access report feature and update user model --- app/models.py | 3 ++ app/routes/user_routes.py | 43 ++++++++++++++++++- ...d0ebe_added_access_level_to_users_table.py | 40 +++++++++++++++++ 3 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 migrations/versions/8e0626ed0ebe_added_access_level_to_users_table.py diff --git a/app/models.py b/app/models.py index 7164045..3f7bc82 100644 --- a/app/models.py +++ b/app/models.py @@ -24,6 +24,9 @@ class User(db.Model): email = db.Column(db.String(120), unique=True, nullable=False) password = db.Column(db.String(128), nullable=False) active = db.Column(db.Boolean, default=False) # Issue 02-Active users + access_level = db.Column( + db.String(16), default="basic", nullable=False + ) # "Role" - Issue 03-Access levels def __repr__(self): return f"" diff --git a/app/routes/user_routes.py b/app/routes/user_routes.py index 6e86bc7..094e2b3 100644 --- a/app/routes/user_routes.py +++ b/app/routes/user_routes.py @@ -68,15 +68,54 @@ def toggle_active(): # Route to show all users. +# Deprecated in favor of access-report route. @user_bp.route("/users", methods=["GET"]) def users(): users = User.query.all() user_list = [] for user in users: user_list.append( - {"username": user.username, "email": user.email, "active": user.active} + { + "user": user.username, + "email": user.email, + "role": user.access_level, + "active": user.active, + } + ) + logger.debug( + f"{user.username} | {user.email} | Active: {user.active} | Role: {user.access_level}" + ) + response = json.dumps(user_list) + return Response(response, mimetype="application/json"), 200 + + +# Route to show all users. +@user_bp.route("/access-report", methods=["POST"]) +def access_report(): + data = request.get_json() + limit_to = data.get("limit_to") + # limit_to may be "all_users", "active_users", or "inactive_users" + if limit_to == "all_users": + users = User.query.all() + elif limit_to == "active_users": + users = User.query.filter_by(active=True).all() + elif limit_to == "inactive_users": + users = User.query.filter_by(active=False).all() + else: + users = User.query.all() + user_list = [] + for user in users: + user_list.append( + { + "user": user.username, + "email": user.email, + "role": user.access_level, + "active": user.active, + } + ) + logger.debug( + f"{user.username} | {user.email} | {user.access_level} | {user.active}" ) - logger.debug(f"{user.username} | {user.email} | {user.active}") response = json.dumps(user_list) return Response(response, mimetype="application/json"), 200 diff --git a/migrations/versions/8e0626ed0ebe_added_access_level_to_users_table.py b/migrations/versions/8e0626ed0ebe_added_access_level_to_users_table.py new file mode 100644 index 0000000..75370ca --- /dev/null +++ b/migrations/versions/8e0626ed0ebe_added_access_level_to_users_table.py @@ -0,0 +1,40 @@ +"""Added access_level to users table + +Revision ID: 8e0626ed0ebe +Revises: dcfa167aa33c +Create Date: 2024-10-15 13:46:06.617848 + +""" + +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = "8e0626ed0ebe" +down_revision = "dcfa167aa33c" +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table("users", schema=None) as batch_op: + batch_op.add_column( + sa.Column( + "access_level", + sa.String(length=16), + nullable=False, + server_default="basic", + ) + ) + + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table("users", schema=None) as batch_op: + batch_op.drop_column("access_level") + + # ### end Alembic commands ###