Skip to content

Commit

Permalink
users update added
Browse files Browse the repository at this point in the history
  • Loading branch information
warreprovoost committed Feb 25, 2024
1 parent 774601b commit de11124
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 13 deletions.
5 changes: 3 additions & 2 deletions backend/project/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from .endpoints.index.index import index_bp
from .endpoints.users import users_bp


db = SQLAlchemy()

Expand All @@ -15,6 +14,8 @@ def create_app():
Returns:
Flask -- A Flask application instance
"""
from .endpoints.index.index import index_bp
from .endpoints.users import users_bp

app = Flask(__name__)
app.register_blueprint(index_bp)
Expand Down
14 changes: 12 additions & 2 deletions backend/project/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,21 @@
from sys import path
from os import getenv
from dotenv import load_dotenv
from sqlalchemy import URL
from project import create_app_with_db

path.append(".")

if __name__ == "__main__":
load_dotenv()
app = create_app_with_db(getenv("DB_HOST"))
app.run(debug=True)

url = URL.create(
drivername="postgresql",
username=getenv("POSTGRES_USER"),
password=getenv("POSTGRES_PASSWORD"),
host=getenv("POSTGRES_HOST"),
database=getenv("POSTGRES_DB")
)

app = create_app_with_db(url)
app.run(debug=True)
38 changes: 32 additions & 6 deletions backend/project/endpoints/users.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
# users.py
"""Users api endpoint"""
from flask import Blueprint, request
from flask_restful import Resource, Api
from project import db
from project.models.users import Users as UserModel

users_bp = Blueprint("users", __name__)
users_api = Api(users_bp)


class Users(Resource):
"""Api endpoint for the /users route"""


def post(self):
"""
This function will respond to post requests made to /users.
Expand All @@ -25,11 +27,35 @@ def post(self):
"uid": "User ID (string)",
"is_teacher": "Teacher status (boolean)",
"is_admin": "Admin status (boolean)"
}
}, 400
}
}, 400

# Code to create a new user in the database using the uid, is_teacher, and is_admin values

new_user = UserModel(uid=uid, is_teacher=is_teacher, is_admin=is_admin)
db.session.add(new_user)
db.session.commit()
return {"Message": "User created successfully!"}

users_api.add_resource(Users, "/users")
def update(self):
uid = request.json.get('uid')
is_teacher = request.json.get('is_teacher')
is_admin = request.json.get('is_admin')
if uid is None:
return {"Message": "User ID is required!"}, 400

user = UserModel.query.get(uid)
if user is None:
return {"Message": "User not found!"}, 404

if is_teacher is not None:
user.is_teacher = is_teacher
if is_admin is not None:
user.is_admin = is_admin

# Save the changes to the database
db.session.commit()
return {"Message": "User updated successfully!"}


users_api.add_resource(Users, "/users")
8 changes: 5 additions & 3 deletions backend/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
flask
flask~=3.0.2
flask-restful
flask-sqlalchemy
python-dotenv
psycopg2-binary
python-dotenv~=1.0.1
psycopg2-binary
pytest~=8.0.1
SQLAlchemy~=2.0.27

0 comments on commit de11124

Please sign in to comment.