Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 03-Access Report #4

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"<User {self.username}>"
Expand Down
43 changes: 41 additions & 2 deletions app/routes/user_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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 ###
Loading