mirror of
https://github.com/rzmk/learnhouse.git
synced 2025-12-19 04:19:25 +00:00
fix: various bugs + improve api readability
This commit is contained in:
parent
7daf6df5a0
commit
2bf80030d7
21 changed files with 98 additions and 75 deletions
|
|
@ -104,10 +104,11 @@ async def get_activity(
|
|||
async def update_activity(
|
||||
request: Request,
|
||||
activity_object: ActivityUpdate,
|
||||
activity_id: int,
|
||||
current_user: PublicUser | AnonymousUser,
|
||||
db_session: Session,
|
||||
):
|
||||
statement = select(Activity).where(Activity.id == activity_object.activity_id)
|
||||
statement = select(Activity).where(Activity.id == activity_id)
|
||||
activity = db_session.exec(statement).first()
|
||||
|
||||
if not activity:
|
||||
|
|
@ -121,8 +122,6 @@ async def update_activity(
|
|||
request, activity.activity_uuid, current_user, "update", db_session
|
||||
)
|
||||
|
||||
del activity_object.activity_id
|
||||
|
||||
# Update only the fields that were passed in
|
||||
for var, value in vars(activity_object).items():
|
||||
if value is not None:
|
||||
|
|
@ -183,7 +182,7 @@ async def delete_activity(
|
|||
|
||||
async def get_activities(
|
||||
request: Request,
|
||||
coursechapter_id: str,
|
||||
coursechapter_id: int,
|
||||
current_user: PublicUser | AnonymousUser,
|
||||
db_session: Session,
|
||||
) -> list[ActivityRead]:
|
||||
|
|
|
|||
|
|
@ -130,10 +130,11 @@ async def get_chapter(
|
|||
async def update_chapter(
|
||||
request: Request,
|
||||
chapter_object: ChapterUpdate,
|
||||
chapter_id: int,
|
||||
current_user: PublicUser | AnonymousUser,
|
||||
db_session: Session,
|
||||
) -> ChapterRead:
|
||||
statement = select(Chapter).where(Chapter.id == chapter_object.chapter_id)
|
||||
statement = select(Chapter).where(Chapter.id == chapter_id)
|
||||
chapter = db_session.exec(statement).first()
|
||||
|
||||
if not chapter:
|
||||
|
|
@ -161,7 +162,7 @@ async def update_chapter(
|
|||
|
||||
async def delete_chapter(
|
||||
request: Request,
|
||||
chapter_id: str,
|
||||
chapter_id: int,
|
||||
current_user: PublicUser | AnonymousUser,
|
||||
db_session: Session,
|
||||
):
|
||||
|
|
@ -190,7 +191,6 @@ async def delete_chapter(
|
|||
return {"detail": "chapter deleted"}
|
||||
|
||||
|
||||
|
||||
async def get_course_chapters(
|
||||
request: Request,
|
||||
course_id: int,
|
||||
|
|
|
|||
|
|
@ -65,6 +65,8 @@ async def get_course_meta(
|
|||
detail="Course not found",
|
||||
)
|
||||
|
||||
print('cd',course.course_uuid)
|
||||
|
||||
# RBAC check
|
||||
await rbac_check(request, course.course_uuid, current_user, "read", db_session)
|
||||
|
||||
|
|
@ -189,10 +191,11 @@ async def update_course_thumbnail(
|
|||
async def update_course(
|
||||
request: Request,
|
||||
course_object: CourseUpdate,
|
||||
course_id: int,
|
||||
current_user: PublicUser | AnonymousUser,
|
||||
db_session: Session,
|
||||
):
|
||||
statement = select(Course).where(Course.id == course_object.course_id)
|
||||
statement = select(Course).where(Course.id == course_id)
|
||||
course = db_session.exec(statement).first()
|
||||
|
||||
if not course:
|
||||
|
|
@ -204,8 +207,6 @@ async def update_course(
|
|||
# RBAC check
|
||||
await rbac_check(request, course.course_uuid, current_user, "update", db_session)
|
||||
|
||||
del course_object.course_id
|
||||
|
||||
# Update only the fields that were passed in
|
||||
for var, value in vars(course_object).items():
|
||||
if value is not None:
|
||||
|
|
|
|||
|
|
@ -118,10 +118,11 @@ async def create_org(
|
|||
async def update_org(
|
||||
request: Request,
|
||||
org_object: OrganizationUpdate,
|
||||
org_id: int,
|
||||
current_user: PublicUser | AnonymousUser,
|
||||
db_session: Session,
|
||||
):
|
||||
statement = select(Organization).where(Organization.id == org_object.org_id)
|
||||
statement = select(Organization).where(Organization.id == org_id)
|
||||
result = db_session.exec(statement)
|
||||
|
||||
org = result.first()
|
||||
|
|
@ -149,9 +150,6 @@ async def update_org(
|
|||
detail="Organization slug already exists",
|
||||
)
|
||||
|
||||
# Remove the org_id from the org_object
|
||||
del org_object.org_id
|
||||
|
||||
# Update only the fields that were passed in
|
||||
for var, value in vars(org_object).items():
|
||||
if value is not None:
|
||||
|
|
@ -203,7 +201,6 @@ async def update_org_logo(
|
|||
db_session.commit()
|
||||
db_session.refresh(org)
|
||||
|
||||
|
||||
return {"detail": "Logo updated"}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ from datetime import datetime
|
|||
from uuid import uuid4
|
||||
from fastapi import HTTPException, Request, status
|
||||
from sqlmodel import Session, select
|
||||
from src.db.activities import Activity
|
||||
from src.db.courses import Course
|
||||
from src.db.trail_runs import TrailRun, TrailRunRead
|
||||
from src.db.trail_steps import TrailStep
|
||||
|
|
@ -120,13 +121,20 @@ async def get_user_trail_with_orgid(
|
|||
async def add_activity_to_trail(
|
||||
request: Request,
|
||||
user: PublicUser,
|
||||
course_id: int,
|
||||
activity_id: int,
|
||||
db_session: Session,
|
||||
) -> TrailRead:
|
||||
|
||||
# Look for the activity
|
||||
statement = select(Activity).where(Activity.id == activity_id)
|
||||
activity = db_session.exec(statement).first()
|
||||
|
||||
if not activity:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND, detail="Activity not found"
|
||||
)
|
||||
|
||||
# check if run already exists
|
||||
statement = select(TrailRun).where(TrailRun.course_id == course_id)
|
||||
statement = select(TrailRun).where(TrailRun.course_id == activity.course_id)
|
||||
trailrun = db_session.exec(statement).first()
|
||||
|
||||
if trailrun:
|
||||
|
|
@ -134,7 +142,7 @@ async def add_activity_to_trail(
|
|||
status_code=status.HTTP_400_BAD_REQUEST, detail="TrailRun already exists"
|
||||
)
|
||||
|
||||
statement = select(Course).where(Course.id == course_id)
|
||||
statement = select(Course).where(Course.id == activity.course_id)
|
||||
course = db_session.exec(statement).first()
|
||||
|
||||
if not course:
|
||||
|
|
@ -160,7 +168,7 @@ async def add_activity_to_trail(
|
|||
if not trailrun:
|
||||
trailrun = TrailRun(
|
||||
trail_id=trail.id if trail.id is not None else 0,
|
||||
course_id=course.id if course.id is not None else 0 ,
|
||||
course_id=course.id if course.id is not None else 0,
|
||||
org_id=course.org_id,
|
||||
user_id=user.id,
|
||||
creation_date=str(datetime.now()),
|
||||
|
|
@ -177,7 +185,7 @@ async def add_activity_to_trail(
|
|||
|
||||
if not trailstep:
|
||||
trailstep = TrailStep(
|
||||
trailrun_id=trailrun.id if trailrun.id is not None else 0 ,
|
||||
trailrun_id=trailrun.id if trailrun.id is not None else 0,
|
||||
activity_id=activity_id,
|
||||
course_id=course.id if course.id is not None else 0,
|
||||
org_id=course.org_id,
|
||||
|
|
@ -225,7 +233,6 @@ async def add_course_to_trail(
|
|||
course_id: str,
|
||||
db_session: Session,
|
||||
) -> TrailRead:
|
||||
|
||||
# check if run already exists
|
||||
statement = select(TrailRun).where(TrailRun.course_id == course_id)
|
||||
trailrun = db_session.exec(statement).first()
|
||||
|
|
@ -234,7 +241,7 @@ async def add_course_to_trail(
|
|||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST, detail="TrailRun already exists"
|
||||
)
|
||||
|
||||
|
||||
statement = select(Course).where(Course.id == course_id)
|
||||
course = db_session.exec(statement).first()
|
||||
|
||||
|
|
|
|||
|
|
@ -158,11 +158,12 @@ async def create_user_without_org(
|
|||
async def update_user(
|
||||
request: Request,
|
||||
db_session: Session,
|
||||
user_id: int,
|
||||
current_user: PublicUser | AnonymousUser,
|
||||
user_object: UserUpdate,
|
||||
):
|
||||
# Get user
|
||||
statement = select(User).where(User.username == user_object.username)
|
||||
statement = select(User).where(User.id == user_id)
|
||||
user = db_session.exec(statement).first()
|
||||
|
||||
if not user:
|
||||
|
|
@ -170,7 +171,7 @@ async def update_user(
|
|||
status_code=400,
|
||||
detail="User does not exist",
|
||||
)
|
||||
|
||||
|
||||
# RBAC check
|
||||
await rbac_check(request, current_user, "update", user.user_uuid, db_session)
|
||||
|
||||
|
|
@ -195,10 +196,11 @@ async def update_user_password(
|
|||
request: Request,
|
||||
db_session: Session,
|
||||
current_user: PublicUser | AnonymousUser,
|
||||
user_id: int,
|
||||
form: UserUpdatePassword,
|
||||
):
|
||||
# Get user
|
||||
statement = select(User).where(User.username == form.user_id)
|
||||
statement = select(User).where(User.id == user_id)
|
||||
user = db_session.exec(statement).first()
|
||||
|
||||
if not user:
|
||||
|
|
@ -206,7 +208,7 @@ async def update_user_password(
|
|||
status_code=400,
|
||||
detail="User does not exist",
|
||||
)
|
||||
|
||||
|
||||
# RBAC check
|
||||
await rbac_check(request, current_user, "update", user.user_uuid, db_session)
|
||||
|
||||
|
|
@ -339,7 +341,6 @@ async def rbac_check(
|
|||
await authorization_verify_based_on_roles_and_authorship(
|
||||
request, current_user.id, "create", "user_x", db_session
|
||||
)
|
||||
|
||||
|
||||
else:
|
||||
await authorization_verify_if_user_is_anon(current_user.id)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue