Skip to content

Commit

Permalink
User profile Visibility (#504)
Browse files Browse the repository at this point in the history
* Adding user visibility

* Making the public field True by default

* Update app/controllers/users.py

Co-authored-by: Mubangizi Allan <mubangizia22@gmail.com>

* Update app/controllers/users.py

Co-authored-by: Mubangizi Allan <mubangizia22@gmail.com>

* Make field is_public

* refactor checks

---------

Co-authored-by: Mubangizi Allan <mubangizia22@gmail.com>
  • Loading branch information
LanternNassi and Mubangizi authored Jun 18, 2024
1 parent 701121a commit 5654d30
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 8 deletions.
6 changes: 6 additions & 0 deletions api_docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ paths:
produces:
- application/json
parameters:
- in: header
name: Authorization
required: true
description: "Bearer [token]"
- in: path
name: user_id
required: true
Expand All @@ -201,6 +205,8 @@ paths:
properties:
name:
type: string
is_public:
type: boolean

responses:
200:
Expand Down
24 changes: 16 additions & 8 deletions app/controllers/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from app.helpers.token import validate_token
from app.helpers.decorators import admin_required
from app.helpers.pagination import paginate
from app.helpers.admin import is_admin
import requests
import secrets
import string
Expand Down Expand Up @@ -411,26 +412,33 @@ def delete(self, user_id):
except Exception as e:
return dict(status='fail', message=str(e)), 500

@jwt_required
def patch(self, user_id):
"""
"""
try:
user_schema = UserSchema(only=("name",))
user_schema = UserSchema(only=("name", "is_public"))

user_data = request.get_json()

current_user_id = get_jwt_identity()
current_user_roles = get_jwt_claims()['roles']

user = User.get_by_id(user_id)

if (current_user_id != user_id):
if (not is_admin(current_user_roles)):
return dict(
status = 'UnAuthorised',
message = 'You are not authorized to edit this users information'
) , 401


validate_user_data, errors = user_schema.load(user_data)

if errors:
return dict(status='fail', message=errors), 400

user = User.get_by_id(user_id)

if not user:
return dict(
status='fail',
message=f'User {user_id} not found'
), 404

updated = User.update(user, **validate_user_data)

Expand Down
1 change: 1 addition & 0 deletions app/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class User(ModelMixin):
'CreditAssignment', backref='user', lazy=True)
disabled = db.Column(db.Boolean, default=False)
admin_disabled = db.Column(db.Boolean, default=False)
is_public = db.Column(db.Boolean, default=True)

def __init__(self, email, name, password, organisation=None):
""" initialize with email, username and password """
Expand Down
1 change: 1 addition & 0 deletions app/schemas/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class UserSchema(Schema):
])
disabled = fields.Boolean(dump_only=True)
admin_disabled = fields.Boolean(dump_only=True)
is_public = fields.Boolean()

def get_age(self, obj):
return get_item_age(obj.date_created)
Expand Down
28 changes: 28 additions & 0 deletions migrations/versions/5f6dfc8f1b35_.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""empty message
Revision ID: 5f6dfc8f1b35
Revises: bc50b6e9a79d
Create Date: 2024-06-11 18:48:32.755879
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '5f6dfc8f1b35'
down_revision = 'bc50b6e9a79d'
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('user', sa.Column('is_public', sa.Boolean(), nullable=True))
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('user', 'is_public')
# ### end Alembic commands ###

0 comments on commit 5654d30

Please sign in to comment.