mirror of
https://github.com/rzmk/learnhouse.git
synced 2025-12-19 04:19:25 +00:00
feat: various improvements
wip: frontend feat: enable cascade on foreign keys wip1 wip2 fix chapters issues wip4
This commit is contained in:
parent
2bf80030d7
commit
187f75e583
71 changed files with 879 additions and 568 deletions
|
|
@ -5,7 +5,7 @@ from sqlmodel import SQLModel, Session, create_engine
|
|||
|
||||
|
||||
engine = create_engine(
|
||||
"postgresql://learnhouse:learnhouse@db:5432/learnhouse", echo=True
|
||||
"postgresql://learnhouse:learnhouse@db:5432/learnhouse", echo=False
|
||||
)
|
||||
SQLModel.metadata.create_all(engine)
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
from typing import Optional
|
||||
from sqlalchemy import JSON, Column
|
||||
from sqlalchemy import JSON, BigInteger, Column, ForeignKey
|
||||
from sqlmodel import Field, SQLModel
|
||||
from enum import Enum
|
||||
|
||||
|
|
@ -34,20 +34,32 @@ class ActivityBase(SQLModel):
|
|||
content: dict = Field(default={}, sa_column=Column(JSON))
|
||||
published_version: int
|
||||
version: int
|
||||
course_id: int = Field(default=None, foreign_key="course.id")
|
||||
course_id: int = Field(
|
||||
default=None,
|
||||
sa_column=Column(
|
||||
BigInteger, ForeignKey("course.id", ondelete="CASCADE")
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
class Activity(ActivityBase, table=True):
|
||||
id: Optional[int] = Field(default=None, primary_key=True)
|
||||
org_id: int = Field(default=None, foreign_key="organization.id")
|
||||
course_id: int = Field(
|
||||
default=None,
|
||||
sa_column=Column(
|
||||
BigInteger, ForeignKey("course.id", ondelete="CASCADE")
|
||||
),
|
||||
)
|
||||
activity_uuid: str = ""
|
||||
creation_date: str = ""
|
||||
update_date: str = ""
|
||||
|
||||
|
||||
class ActivityCreate(ActivityBase):
|
||||
org_id: int = Field(default=None, foreign_key="organization.id")
|
||||
course_id: int = Field(default=None, foreign_key="course.id")
|
||||
course_id: int = Field(
|
||||
sa_column=Column("course_id", ForeignKey("course.id", ondelete="CASCADE"))
|
||||
)
|
||||
chapter_id: int
|
||||
pass
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
from typing import Optional
|
||||
from sqlalchemy import JSON, Column
|
||||
from sqlalchemy import JSON, Column, ForeignKey
|
||||
from sqlmodel import Field, SQLModel
|
||||
from enum import Enum
|
||||
|
||||
|
|
@ -22,9 +22,9 @@ class Block(BlockBase, table=True):
|
|||
id: Optional[int] = Field(default=None, primary_key=True)
|
||||
content: dict = Field(default={}, sa_column=Column(JSON))
|
||||
org_id: int = Field(default=None, foreign_key="organization.id")
|
||||
course_id: int = Field(default=None, foreign_key="course.id")
|
||||
chapter_id: int = Field(default=None, foreign_key="chapter.id")
|
||||
activity_id: int = Field(default=None, foreign_key="activity.id")
|
||||
course_id: int = Field(sa_column= Column("course_id", ForeignKey("course.id", ondelete="CASCADE")))
|
||||
chapter_id: int = Field(sa_column= Column("chapter_id", ForeignKey("chapter.id", ondelete="CASCADE")))
|
||||
activity_id: int = Field(sa_column= Column("activity_id", ForeignKey("activity.id", ondelete="CASCADE")))
|
||||
block_uuid: str
|
||||
creation_date: str
|
||||
update_date: str
|
||||
|
|
|
|||
|
|
@ -1,12 +1,13 @@
|
|||
from typing import Optional
|
||||
from sqlalchemy import BigInteger, Column, ForeignKey
|
||||
from sqlmodel import Field, SQLModel
|
||||
|
||||
class ChapterActivity(SQLModel, table=True):
|
||||
id: Optional[int] = Field(default=None, primary_key=True)
|
||||
order: int
|
||||
chapter_id: int = Field(default=None, foreign_key="chapter.id", )
|
||||
activity_id: int = Field(default=None, foreign_key="activity.id")
|
||||
course_id : int = Field(default=None, foreign_key="course.id")
|
||||
chapter_id: int = Field(sa_column=Column(BigInteger, ForeignKey("chapter.id", ondelete="CASCADE")))
|
||||
activity_id: int = Field(sa_column=Column(BigInteger, ForeignKey("activity.id", ondelete="CASCADE")))
|
||||
course_id : int = Field(sa_column=Column(BigInteger, ForeignKey("course.id", ondelete="CASCADE")))
|
||||
org_id : int = Field(default=None, foreign_key="organization.id")
|
||||
creation_date: str
|
||||
update_date: str
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
from typing import List, Optional
|
||||
from typing import Any, List, Optional
|
||||
from pydantic import BaseModel
|
||||
from sqlalchemy import Column, ForeignKey
|
||||
from sqlmodel import Field, SQLModel
|
||||
from src.db.activities import ActivityRead
|
||||
|
||||
|
|
@ -9,19 +10,21 @@ class ChapterBase(SQLModel):
|
|||
description: Optional[str] = ""
|
||||
thumbnail_image: Optional[str] = ""
|
||||
org_id: int = Field(default=None, foreign_key="organization.id")
|
||||
course_id: int = Field(default=None, foreign_key="course.id")
|
||||
creation_date: str
|
||||
update_date: str
|
||||
course_id: int = Field(
|
||||
sa_column=Column("course_id", ForeignKey("course.id", ondelete="CASCADE"))
|
||||
)
|
||||
|
||||
|
||||
class Chapter(ChapterBase, table=True):
|
||||
id: Optional[int] = Field(default=None, primary_key=True)
|
||||
course_id: int = Field(
|
||||
sa_column=Column("course_id", ForeignKey("course.id", ondelete="CASCADE"))
|
||||
)
|
||||
chapter_uuid: str = ""
|
||||
creation_date: str = ""
|
||||
update_date: str = ""
|
||||
|
||||
|
||||
|
||||
class ChapterCreate(ChapterBase):
|
||||
# referenced order here will be ignored and just used for validation
|
||||
# used order will be the next available.
|
||||
|
|
@ -32,6 +35,8 @@ class ChapterUpdate(ChapterBase):
|
|||
name: Optional[str]
|
||||
description: Optional[str]
|
||||
thumbnail_image: Optional[str]
|
||||
course_id: Optional[int]
|
||||
org_id: Optional[int]
|
||||
|
||||
|
||||
class ChapterRead(ChapterBase):
|
||||
|
|
@ -57,7 +62,7 @@ class ChapterUpdateOrder(BaseModel):
|
|||
|
||||
|
||||
class DepreceatedChaptersRead(BaseModel):
|
||||
chapter_order: list[str]
|
||||
chapters: List[ChapterRead]
|
||||
activities: List[ActivityRead]
|
||||
chapterOrder: Any
|
||||
chapters: Any
|
||||
activities: Any
|
||||
pass
|
||||
|
|
|
|||
|
|
@ -24,7 +24,6 @@ class CollectionCreate(CollectionBase):
|
|||
|
||||
|
||||
class CollectionUpdate(CollectionBase):
|
||||
collection_id: int
|
||||
courses: Optional[list]
|
||||
name: Optional[str]
|
||||
public: Optional[bool]
|
||||
|
|
|
|||
|
|
@ -1,11 +1,12 @@
|
|||
from typing import Optional
|
||||
from sqlalchemy import BigInteger, Column, ForeignKey
|
||||
from sqlmodel import Field, SQLModel
|
||||
|
||||
|
||||
class CollectionCourse(SQLModel, table=True):
|
||||
id: Optional[int] = Field(default=None, primary_key=True)
|
||||
collection_id: int = Field(default=None, foreign_key="collection.id")
|
||||
course_id: int = Field(default=None, foreign_key="course.id")
|
||||
collection_id: int = Field(sa_column=Column(BigInteger, ForeignKey("collection.id", ondelete="CASCADE")))
|
||||
course_id: int = Field(sa_column=Column(BigInteger, ForeignKey("course.id", ondelete="CASCADE")))
|
||||
org_id: int = Field(default=None, foreign_key="organization.id")
|
||||
creation_date: str
|
||||
update_date: str
|
||||
|
|
|
|||
|
|
@ -1,11 +1,17 @@
|
|||
from typing import Optional
|
||||
from sqlalchemy import BigInteger, Column, ForeignKey
|
||||
from sqlmodel import Field, SQLModel
|
||||
|
||||
|
||||
class CourseChapter(SQLModel, table=True):
|
||||
id: Optional[int] = Field(default=None, primary_key=True)
|
||||
order: int
|
||||
course_id: int = Field(default=None, foreign_key="course.id")
|
||||
chapter_id: int = Field(default=None, foreign_key="chapter.id")
|
||||
org_id : int = Field(default=None, foreign_key="organization.id")
|
||||
course_id: int = Field(
|
||||
sa_column=Column(BigInteger, ForeignKey("course.id", ondelete="CASCADE"))
|
||||
)
|
||||
chapter_id: int = Field(
|
||||
sa_column=Column(BigInteger, ForeignKey("chapter.id", ondelete="CASCADE"))
|
||||
)
|
||||
org_id: int = Field(default=None, foreign_key="organization.id")
|
||||
creation_date: str
|
||||
update_date: str
|
||||
update_date: str
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
from typing import List, Optional
|
||||
from sqlmodel import Field, SQLModel
|
||||
from src.db.users import User, UserRead
|
||||
from src.db.trails import TrailRead
|
||||
from src.db.chapters import ChapterRead
|
||||
|
||||
|
|
@ -22,7 +23,6 @@ class Course(CourseBase, table=True):
|
|||
update_date: str = ""
|
||||
|
||||
|
||||
|
||||
class CourseCreate(CourseBase):
|
||||
org_id: int = Field(default=None, foreign_key="organization.id")
|
||||
pass
|
||||
|
|
@ -40,6 +40,7 @@ class CourseUpdate(CourseBase):
|
|||
class CourseRead(CourseBase):
|
||||
id: int
|
||||
org_id: int = Field(default=None, foreign_key="organization.id")
|
||||
authors: List[UserRead]
|
||||
course_uuid: str
|
||||
creation_date: str
|
||||
update_date: str
|
||||
|
|
@ -53,6 +54,7 @@ class FullCourseRead(CourseBase):
|
|||
update_date: str
|
||||
# Chapters, Activities
|
||||
chapters: List[ChapterRead]
|
||||
authors: List[UserRead]
|
||||
pass
|
||||
|
||||
|
||||
|
|
@ -61,8 +63,9 @@ class FullCourseReadWithTrail(CourseBase):
|
|||
course_uuid: str
|
||||
creation_date: str
|
||||
update_date: str
|
||||
authors: List[UserRead]
|
||||
# Chapters, Activities
|
||||
chapters: List[ChapterRead]
|
||||
# Trail
|
||||
trail: TrailRead
|
||||
trail: TrailRead | None
|
||||
pass
|
||||
|
|
|
|||
|
|
@ -1,7 +1,9 @@
|
|||
from typing import Optional
|
||||
from sqlalchemy import BigInteger, Column, ForeignKey
|
||||
from sqlmodel import Field, SQLModel
|
||||
from enum import Enum
|
||||
|
||||
|
||||
class HeaderTypeEnum(str, Enum):
|
||||
LOGO_MENU_SETTINGS = "LOGO_MENU_SETTINGS"
|
||||
MENU_LOGO_SETTINGS = "MENU_LOGO_SETTINGS"
|
||||
|
|
@ -9,7 +11,9 @@ class HeaderTypeEnum(str, Enum):
|
|||
|
||||
class OrganizationSettings(SQLModel, table=True):
|
||||
id: Optional[int] = Field(default=None, primary_key=True)
|
||||
org_id: int = Field(default=None, foreign_key="organization.id")
|
||||
org_id: int = Field(
|
||||
sa_column=Column(BigInteger, ForeignKey("organization.id", ondelete="CASCADE"))
|
||||
)
|
||||
logo_image: Optional[str] = ""
|
||||
header_type: HeaderTypeEnum = HeaderTypeEnum.LOGO_MENU_SETTINGS
|
||||
color: str = ""
|
||||
|
|
|
|||
|
|
@ -1,11 +1,14 @@
|
|||
from typing import Optional
|
||||
from sqlalchemy import BigInteger, Column, ForeignKey
|
||||
from sqlmodel import Field, SQLModel
|
||||
|
||||
|
||||
class UserOrganization(SQLModel, table=True):
|
||||
id: Optional[int] = Field(default=None, primary_key=True)
|
||||
user_id: int = Field(default=None, foreign_key="user.id")
|
||||
org_id: int = Field(default=None, foreign_key="organization.id")
|
||||
org_id: int = Field(
|
||||
sa_column=Column(BigInteger, ForeignKey("organization.id", ondelete="CASCADE"))
|
||||
)
|
||||
role_id: int = Field(default=None, foreign_key="role.id")
|
||||
creation_date: str
|
||||
update_date: str
|
||||
|
|
|
|||
|
|
@ -9,11 +9,11 @@ from src.db.chapters import (
|
|||
DepreceatedChaptersRead,
|
||||
)
|
||||
from src.services.courses.chapters import (
|
||||
DEPRECEATED_get_course_chapters,
|
||||
create_chapter,
|
||||
delete_chapter,
|
||||
get_chapter,
|
||||
get_course_chapters,
|
||||
get_depreceated_course_chapters,
|
||||
reorder_chapters_and_activities,
|
||||
update_chapter,
|
||||
)
|
||||
|
|
@ -50,25 +50,25 @@ async def api_get_coursechapter(
|
|||
return await get_chapter(request, chapter_id, current_user, db_session)
|
||||
|
||||
|
||||
@router.get("/course/{course_id}/meta")
|
||||
@router.get("/course/{course_uuid}/meta", deprecated=True)
|
||||
async def api_get_chapter_meta(
|
||||
request: Request,
|
||||
course_id: int,
|
||||
course_uuid: str,
|
||||
current_user: PublicUser = Depends(get_current_user),
|
||||
db_session=Depends(get_db_session),
|
||||
) -> DepreceatedChaptersRead:
|
||||
):
|
||||
"""
|
||||
Get Chapters metadata
|
||||
"""
|
||||
return await get_depreceated_course_chapters(
|
||||
request, course_id, current_user, db_session
|
||||
return await DEPRECEATED_get_course_chapters(
|
||||
request, course_uuid, current_user, db_session
|
||||
)
|
||||
|
||||
|
||||
@router.put("/course/{course_id}/order")
|
||||
@router.put("/course/{course_uuid}/order")
|
||||
async def api_update_chapter_meta(
|
||||
request: Request,
|
||||
course_id: int,
|
||||
course_uuid: str,
|
||||
order: ChapterUpdateOrder,
|
||||
current_user: PublicUser = Depends(get_current_user),
|
||||
db_session=Depends(get_db_session),
|
||||
|
|
@ -77,7 +77,7 @@ async def api_update_chapter_meta(
|
|||
Update Chapter metadata
|
||||
"""
|
||||
return await reorder_chapters_and_activities(
|
||||
request, course_id, order, current_user, db_session
|
||||
request, course_uuid, order, current_user, db_session
|
||||
)
|
||||
|
||||
|
||||
|
|
@ -117,7 +117,7 @@ async def api_update_coursechapter(
|
|||
@router.delete("/{chapter_id}")
|
||||
async def api_delete_coursechapter(
|
||||
request: Request,
|
||||
chapter_id: int,
|
||||
chapter_id: str,
|
||||
current_user: PublicUser = Depends(get_current_user),
|
||||
db_session=Depends(get_db_session),
|
||||
):
|
||||
|
|
|
|||
|
|
@ -29,17 +29,17 @@ async def api_create_collection(
|
|||
return await create_collection(request, collection_object, current_user, db_session)
|
||||
|
||||
|
||||
@router.get("/{collection_id}")
|
||||
@router.get("/{collection_uuid}")
|
||||
async def api_get_collection(
|
||||
request: Request,
|
||||
collection_id: str,
|
||||
collection_uuid: str,
|
||||
current_user: PublicUser = Depends(get_current_user),
|
||||
db_session=Depends(get_db_session),
|
||||
) -> CollectionRead:
|
||||
"""
|
||||
Get single collection by ID
|
||||
"""
|
||||
return await get_collection(request, collection_id, current_user, db_session)
|
||||
return await get_collection(request, collection_uuid, current_user, db_session)
|
||||
|
||||
|
||||
@router.get("/org/{org_id}/page/{page}/limit/{limit}")
|
||||
|
|
@ -57,23 +57,26 @@ async def api_get_collections_by(
|
|||
return await get_collections(request, org_id, current_user, db_session, page, limit)
|
||||
|
||||
|
||||
@router.put("/{collection_id}")
|
||||
@router.put("/{collection_uuid}")
|
||||
async def api_update_collection(
|
||||
request: Request,
|
||||
collection_object: CollectionUpdate,
|
||||
collection_uuid: str,
|
||||
current_user: PublicUser = Depends(get_current_user),
|
||||
db_session=Depends(get_db_session),
|
||||
) -> CollectionRead:
|
||||
"""
|
||||
Update collection by ID
|
||||
"""
|
||||
return await update_collection(request, collection_object, current_user, db_session)
|
||||
return await update_collection(
|
||||
request, collection_object, collection_uuid, current_user, db_session
|
||||
)
|
||||
|
||||
|
||||
@router.delete("/{collection_id}")
|
||||
@router.delete("/{collection_uuid}")
|
||||
async def api_delete_collection(
|
||||
request: Request,
|
||||
collection_id: str,
|
||||
collection_uuid: str,
|
||||
current_user: PublicUser = Depends(get_current_user),
|
||||
db_session=Depends(get_db_session),
|
||||
):
|
||||
|
|
@ -81,4 +84,4 @@ async def api_delete_collection(
|
|||
Delete collection by ID
|
||||
"""
|
||||
|
||||
return await delete_collection(request, collection_id, current_user, db_session)
|
||||
return await delete_collection(request, collection_uuid, current_user, db_session)
|
||||
|
|
|
|||
|
|
@ -51,13 +51,13 @@ async def api_create_course(
|
|||
learnings=learnings,
|
||||
tags=tags,
|
||||
)
|
||||
return await create_course(request, course, current_user, db_session, thumbnail)
|
||||
return await create_course(request, org_id, course, current_user, db_session, thumbnail)
|
||||
|
||||
|
||||
@router.put("/{course_id}/thumbnail")
|
||||
@router.put("/{course_uuid}/thumbnail")
|
||||
async def api_create_course_thumbnail(
|
||||
request: Request,
|
||||
course_id: str,
|
||||
course_uuid: str,
|
||||
thumbnail: UploadFile | None = None,
|
||||
db_session: Session = Depends(get_db_session),
|
||||
current_user: PublicUser = Depends(get_current_user),
|
||||
|
|
@ -66,37 +66,37 @@ async def api_create_course_thumbnail(
|
|||
Update new Course Thumbnail
|
||||
"""
|
||||
return await update_course_thumbnail(
|
||||
request, course_id, current_user, db_session, thumbnail
|
||||
request, course_uuid, current_user, db_session, thumbnail
|
||||
)
|
||||
|
||||
|
||||
@router.get("/{course_id}")
|
||||
@router.get("/{course_uuid}")
|
||||
async def api_get_course(
|
||||
request: Request,
|
||||
course_id: str,
|
||||
course_uuid: str,
|
||||
db_session: Session = Depends(get_db_session),
|
||||
current_user: PublicUser = Depends(get_current_user),
|
||||
) -> CourseRead:
|
||||
"""
|
||||
Get single Course by course_id
|
||||
Get single Course by course_uuid
|
||||
"""
|
||||
return await get_course(
|
||||
request, course_id, current_user=current_user, db_session=db_session
|
||||
request, course_uuid, current_user=current_user, db_session=db_session
|
||||
)
|
||||
|
||||
|
||||
@router.get("/{course_id}/meta")
|
||||
@router.get("/{course_uuid}/meta")
|
||||
async def api_get_course_meta(
|
||||
request: Request,
|
||||
course_id: int,
|
||||
course_uuid: str,
|
||||
db_session: Session = Depends(get_db_session),
|
||||
current_user: PublicUser = Depends(get_current_user),
|
||||
) -> FullCourseReadWithTrail:
|
||||
"""
|
||||
Get single Course Metadata (chapters, activities) by course_id
|
||||
Get single Course Metadata (chapters, activities) by course_uuid
|
||||
"""
|
||||
return await get_course_meta(
|
||||
request, course_id, current_user=current_user, db_session=db_session
|
||||
request, course_uuid, current_user=current_user, db_session=db_session
|
||||
)
|
||||
|
||||
|
||||
|
|
@ -117,26 +117,26 @@ async def api_get_course_by_orgslug(
|
|||
)
|
||||
|
||||
|
||||
@router.put("/{course_id}")
|
||||
@router.put("/{course_uuid}")
|
||||
async def api_update_course(
|
||||
request: Request,
|
||||
course_object: CourseUpdate,
|
||||
course_id: int,
|
||||
course_uuid: str,
|
||||
db_session: Session = Depends(get_db_session),
|
||||
current_user: PublicUser = Depends(get_current_user),
|
||||
) -> CourseRead:
|
||||
"""
|
||||
Update Course by course_id
|
||||
Update Course by course_uuid
|
||||
"""
|
||||
return await update_course(
|
||||
request, course_object, course_id, current_user, db_session
|
||||
request, course_object, course_uuid, current_user, db_session
|
||||
)
|
||||
|
||||
|
||||
@router.delete("/{course_id}")
|
||||
@router.delete("/{course_uuid}")
|
||||
async def api_delete_course(
|
||||
request: Request,
|
||||
course_id: str,
|
||||
course_uuid: str,
|
||||
db_session: Session = Depends(get_db_session),
|
||||
current_user: PublicUser = Depends(get_current_user),
|
||||
):
|
||||
|
|
@ -144,4 +144,4 @@ async def api_delete_course(
|
|||
Delete Course by ID
|
||||
"""
|
||||
|
||||
return await delete_course(request, course_id, current_user, db_session)
|
||||
return await delete_course(request, course_uuid, current_user, db_session)
|
||||
|
|
|
|||
|
|
@ -56,33 +56,33 @@ async def api_get_trail_by_org_id(
|
|||
)
|
||||
|
||||
|
||||
@router.post("/add_course/{course_id}")
|
||||
@router.post("/add_course/{course_uuid}")
|
||||
async def api_add_course_to_trail(
|
||||
request: Request,
|
||||
course_id: str,
|
||||
course_uuid: str,
|
||||
user=Depends(get_current_user),
|
||||
db_session=Depends(get_db_session),
|
||||
) -> TrailRead:
|
||||
"""
|
||||
Add Course to trail
|
||||
"""
|
||||
return await add_course_to_trail(request, user, course_id, db_session)
|
||||
return await add_course_to_trail(request, user, course_uuid, db_session)
|
||||
|
||||
|
||||
@router.delete("/remove_course/{course_id}")
|
||||
@router.delete("/remove_course/{course_uuid}")
|
||||
async def api_remove_course_to_trail(
|
||||
request: Request,
|
||||
course_id: str,
|
||||
course_uuid: str,
|
||||
user=Depends(get_current_user),
|
||||
db_session=Depends(get_db_session),
|
||||
) -> TrailRead:
|
||||
"""
|
||||
Remove Course from trail
|
||||
"""
|
||||
return await remove_course_from_trail(request, user, course_id, db_session)
|
||||
return await remove_course_from_trail(request, user, course_uuid, db_session)
|
||||
|
||||
|
||||
@router.post("/add_activity/{activity_id}")
|
||||
@router.post("/add_activity/{activity_uuid}")
|
||||
async def api_add_activity_to_trail(
|
||||
request: Request,
|
||||
activity_id: int,
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
from typing import Literal
|
||||
from fastapi import APIRouter, Depends, Request
|
||||
from sqlmodel import Session
|
||||
from src.security.auth import get_current_user
|
||||
|
|
@ -12,6 +13,7 @@ from src.db.users import (
|
|||
UserUpdatePassword,
|
||||
)
|
||||
from src.services.users.users import (
|
||||
authorize_user_action,
|
||||
create_user,
|
||||
create_user_without_org,
|
||||
delete_user_by_id,
|
||||
|
|
@ -33,6 +35,22 @@ async def api_get_current_user(current_user: User = Depends(get_current_user)):
|
|||
return current_user.dict()
|
||||
|
||||
|
||||
@router.get("/authorize/ressource/{ressource_uuid}/action/{action}")
|
||||
async def api_get_authorization_status(
|
||||
request: Request,
|
||||
ressource_uuid: str,
|
||||
action: Literal["create", "read", "update", "delete"],
|
||||
db_session: Session = Depends(get_db_session),
|
||||
current_user: PublicUser = Depends(get_current_user),
|
||||
):
|
||||
"""
|
||||
Get current user authorization status
|
||||
"""
|
||||
return await authorize_user_action(
|
||||
request, db_session, current_user, ressource_uuid, action
|
||||
)
|
||||
|
||||
|
||||
@router.post("/{org_id}", response_model=UserRead, tags=["users"])
|
||||
async def api_create_user_with_orgid(
|
||||
*,
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
from typing import Literal
|
||||
from sqlmodel import Session, select
|
||||
from src.db.chapters import Chapter
|
||||
from src.security.rbac.rbac import (
|
||||
authorization_verify_based_on_roles_and_authorship,
|
||||
authorization_verify_if_user_is_anon,
|
||||
|
|
@ -27,21 +28,22 @@ async def create_activity(
|
|||
activity = Activity.from_orm(activity_object)
|
||||
|
||||
# CHeck if org exists
|
||||
statement = select(Organization).where(Organization.id == activity_object.org_id)
|
||||
org = db_session.exec(statement).first()
|
||||
statement = select(Chapter).where(Chapter.id == activity_object.chapter_id)
|
||||
chapter = db_session.exec(statement).first()
|
||||
|
||||
if not org:
|
||||
if not chapter:
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
detail="Organization not found",
|
||||
detail="Chapter not found",
|
||||
)
|
||||
|
||||
# RBAC check
|
||||
await rbac_check(request, org.org_uuid, current_user, "create", db_session)
|
||||
await rbac_check(request, chapter.chapter_uuid, current_user, "create", db_session)
|
||||
|
||||
activity.activity_uuid = str(f"activity_{uuid4()}")
|
||||
activity.creation_date = str(datetime.now())
|
||||
activity.update_date = str(datetime.now())
|
||||
activity.org_id = chapter.org_id
|
||||
|
||||
# Insert Activity in DB
|
||||
db_session.add(activity)
|
||||
|
|
@ -64,7 +66,7 @@ async def create_activity(
|
|||
chapter_id=activity_object.chapter_id,
|
||||
activity_id=activity.id if activity.id else 0,
|
||||
course_id=activity_object.course_id,
|
||||
org_id=activity_object.org_id,
|
||||
org_id=chapter.org_id,
|
||||
creation_date=str(datetime.now()),
|
||||
update_date=str(datetime.now()),
|
||||
order=to_be_used_order,
|
||||
|
|
|
|||
|
|
@ -51,7 +51,7 @@ async def create_documentpdf_activity(
|
|||
)
|
||||
|
||||
# get org_id
|
||||
org_id = coursechapter.id
|
||||
org_id = coursechapter.org_id
|
||||
|
||||
# create activity uuid
|
||||
activity_uuid = f"activity_{uuid4()}"
|
||||
|
|
|
|||
|
|
@ -82,6 +82,8 @@ async def create_video_activity(
|
|||
activity_type=ActivityTypeEnum.TYPE_VIDEO,
|
||||
activity_sub_type=ActivitySubTypeEnum.SUBTYPE_VIDEO_HOSTED,
|
||||
activity_uuid=activity_uuid,
|
||||
org_id=coursechapter.org_id,
|
||||
course_id=coursechapter.course_id,
|
||||
published_version=1,
|
||||
content={
|
||||
"filename": "video." + video_format,
|
||||
|
|
@ -171,6 +173,8 @@ async def create_external_video_activity(
|
|||
activity_type=ActivityTypeEnum.TYPE_VIDEO,
|
||||
activity_sub_type=ActivitySubTypeEnum.SUBTYPE_VIDEO_YOUTUBE,
|
||||
activity_uuid=activity_uuid,
|
||||
course_id=coursechapter.course_id,
|
||||
org_id=coursechapter.org_id,
|
||||
published_version=1,
|
||||
content={
|
||||
"uri": data.uri,
|
||||
|
|
@ -192,6 +196,8 @@ async def create_external_video_activity(
|
|||
chapter_activity_object = ChapterActivity(
|
||||
chapter_id=coursechapter.id, # type: ignore
|
||||
activity_id=activity.id, # type: ignore
|
||||
course_id=coursechapter.course_id,
|
||||
org_id=coursechapter.org_id,
|
||||
creation_date=str(datetime.now()),
|
||||
update_date=str(datetime.now()),
|
||||
order=1,
|
||||
|
|
|
|||
|
|
@ -36,6 +36,11 @@ async def create_chapter(
|
|||
) -> ChapterRead:
|
||||
chapter = Chapter.from_orm(chapter_object)
|
||||
|
||||
# Get COurse
|
||||
statement = select(Course).where(Course.id == chapter_object.course_id)
|
||||
|
||||
course = db_session.exec(statement).one()
|
||||
|
||||
# RBAC check
|
||||
await rbac_check(request, "chapter_x", current_user, "create", db_session)
|
||||
|
||||
|
|
@ -44,6 +49,7 @@ async def create_chapter(
|
|||
chapter.chapter_uuid = f"chapter_{uuid4()}"
|
||||
chapter.creation_date = str(datetime.now())
|
||||
chapter.update_date = str(datetime.now())
|
||||
chapter.org_id = course.org_id
|
||||
|
||||
# Find the last chapter in the course and add it to the list
|
||||
statement = (
|
||||
|
|
@ -155,14 +161,17 @@ async def update_chapter(
|
|||
db_session.commit()
|
||||
db_session.refresh(chapter)
|
||||
|
||||
chapter = ChapterRead(**chapter.dict())
|
||||
if chapter:
|
||||
chapter = await get_chapter(
|
||||
request, chapter.id, current_user, db_session # type: ignore
|
||||
)
|
||||
|
||||
return chapter
|
||||
|
||||
|
||||
async def delete_chapter(
|
||||
request: Request,
|
||||
chapter_id: int,
|
||||
chapter_id: str,
|
||||
current_user: PublicUser | AnonymousUser,
|
||||
db_session: Session,
|
||||
):
|
||||
|
|
@ -181,7 +190,7 @@ async def delete_chapter(
|
|||
db_session.commit()
|
||||
|
||||
# Remove all linked activities
|
||||
statement = select(ChapterActivity).where(ChapterActivity.chapter_id == chapter_id)
|
||||
statement = select(ChapterActivity).where(ChapterActivity.id == chapter.id)
|
||||
chapter_activities = db_session.exec(statement).all()
|
||||
|
||||
for chapter_activity in chapter_activities:
|
||||
|
|
@ -199,14 +208,16 @@ async def get_course_chapters(
|
|||
page: int = 1,
|
||||
limit: int = 10,
|
||||
) -> List[ChapterRead]:
|
||||
statement = select(Chapter).where(Chapter.course_id == course_id)
|
||||
statement = (
|
||||
select(Chapter)
|
||||
.join(CourseChapter, Chapter.id == CourseChapter.chapter_id)
|
||||
.where(CourseChapter.course_id == course_id)
|
||||
.where(Chapter.course_id == course_id)
|
||||
.order_by(CourseChapter.order)
|
||||
.group_by(Chapter.id, CourseChapter.order)
|
||||
)
|
||||
chapters = db_session.exec(statement).all()
|
||||
|
||||
if not chapters:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_409_CONFLICT, detail="Course do not have chapters"
|
||||
)
|
||||
|
||||
chapters = [ChapterRead(**chapter.dict(), activities=[]) for chapter in chapters]
|
||||
|
||||
# RBAC check
|
||||
|
|
@ -236,13 +247,16 @@ async def get_course_chapters(
|
|||
return chapters
|
||||
|
||||
|
||||
async def get_depreceated_course_chapters(
|
||||
# Important Note : this is legacy code that has been used because
|
||||
# the frontend is still not adapted for the new data structure, this implementation is absolutely not the best one
|
||||
# and should not be used for future features
|
||||
async def DEPRECEATED_get_course_chapters(
|
||||
request: Request,
|
||||
course_id: int,
|
||||
course_uuid: str,
|
||||
current_user: PublicUser,
|
||||
db_session: Session,
|
||||
) -> DepreceatedChaptersRead:
|
||||
statement = select(Course).where(Course.id == course_id)
|
||||
):
|
||||
statement = select(Course).where(Course.course_uuid == course_uuid)
|
||||
course = db_session.exec(statement).first()
|
||||
|
||||
if not course:
|
||||
|
|
@ -253,76 +267,79 @@ async def get_depreceated_course_chapters(
|
|||
# RBAC check
|
||||
await rbac_check(request, course.course_uuid, current_user, "read", db_session)
|
||||
|
||||
# Get chapters that are linked to his course and order them by order, using the order field in the CourseChapter table
|
||||
chapters_in_db = await get_course_chapters(request, course.id, db_session, current_user) # type: ignore
|
||||
|
||||
# activities
|
||||
chapter_activityIdsGlobal = []
|
||||
|
||||
# chapters
|
||||
chapters = {}
|
||||
|
||||
for chapter in chapters_in_db:
|
||||
chapter_activityIds = []
|
||||
|
||||
for activity in chapter.activities:
|
||||
print("test", activity)
|
||||
chapter_activityIds.append(activity.activity_uuid)
|
||||
|
||||
chapters[chapter.chapter_uuid] = {
|
||||
"uuid": chapter.chapter_uuid,
|
||||
"id": chapter.id,
|
||||
"name": chapter.name,
|
||||
"activityIds": chapter_activityIds,
|
||||
}
|
||||
|
||||
# activities
|
||||
activities_list = {}
|
||||
statement = (
|
||||
select(Activity)
|
||||
.join(ChapterActivity, ChapterActivity.activity_id == Activity.id)
|
||||
.where(ChapterActivity.activity_id == Activity.id)
|
||||
.group_by(Activity.id)
|
||||
)
|
||||
activities_in_db = db_session.exec(statement).all()
|
||||
|
||||
for activity in activities_in_db:
|
||||
activities_list[activity.activity_uuid] = {
|
||||
"uuid": activity.activity_uuid,
|
||||
"id": activity.id,
|
||||
"name": activity.name,
|
||||
"type": activity.activity_type,
|
||||
"content": activity.content,
|
||||
}
|
||||
|
||||
# get chapter order
|
||||
statement = (
|
||||
select(Chapter)
|
||||
.join(CourseChapter, Chapter.id == CourseChapter.chapter_id)
|
||||
.where(CourseChapter.course_id == course_id)
|
||||
.order_by(CourseChapter.order)
|
||||
.join(CourseChapter, CourseChapter.chapter_id == Chapter.id)
|
||||
.where(CourseChapter.chapter_id == Chapter.id)
|
||||
.group_by(Chapter.id, CourseChapter.order)
|
||||
.order_by(CourseChapter.order)
|
||||
)
|
||||
print("ded", statement)
|
||||
chapters = db_session.exec(statement).all()
|
||||
chapters_in_db = db_session.exec(statement).all()
|
||||
|
||||
chapters = [ChapterRead(**chapter.dict(), activities=[]) for chapter in chapters]
|
||||
chapterOrder = []
|
||||
|
||||
# Get activities for each chapter
|
||||
for chapter in chapters:
|
||||
statement = (
|
||||
select(Activity)
|
||||
.join(ChapterActivity, Activity.id == ChapterActivity.activity_id)
|
||||
.where(ChapterActivity.chapter_id == chapter.id)
|
||||
.order_by(ChapterActivity.order)
|
||||
.distinct(Activity.id, ChapterActivity.order)
|
||||
)
|
||||
chapter_activities = db_session.exec(statement).all()
|
||||
for chapter in chapters_in_db:
|
||||
chapterOrder.append(chapter.chapter_uuid)
|
||||
|
||||
for chapter_activity in chapter_activities:
|
||||
statement = (
|
||||
select(Activity)
|
||||
.join(ChapterActivity, Activity.id == ChapterActivity.activity_id)
|
||||
.where(Activity.id == chapter_activity.id)
|
||||
.distinct(Activity.id, ChapterActivity.order)
|
||||
.order_by(ChapterActivity.order)
|
||||
)
|
||||
activity = db_session.exec(statement).first()
|
||||
final = {
|
||||
"chapters": chapters,
|
||||
"chapterOrder": chapterOrder,
|
||||
"activities": activities_list,
|
||||
}
|
||||
|
||||
if activity:
|
||||
chapter.activities.append(ActivityRead(**activity.dict()))
|
||||
|
||||
# Get a list of chapter ids
|
||||
chapter_order: List[str] = [str(chapter.id) for chapter in chapters]
|
||||
|
||||
# Get activities for each chapter
|
||||
activities = []
|
||||
for chapter_id in chapter_order:
|
||||
# order by activity order
|
||||
statement = (
|
||||
select(Activity)
|
||||
.join(ChapterActivity, Activity.id == ChapterActivity.activity_id)
|
||||
.where(ChapterActivity.chapter_id == chapter_id)
|
||||
.order_by(ChapterActivity.order)
|
||||
.distinct(Activity.id, ChapterActivity.order)
|
||||
)
|
||||
chapter_activities = db_session.exec(statement).all()
|
||||
|
||||
activities.extend(chapter_activities)
|
||||
|
||||
result = DepreceatedChaptersRead(
|
||||
chapter_order=chapter_order, chapters=chapters, activities=activities
|
||||
)
|
||||
|
||||
return result
|
||||
return final
|
||||
|
||||
|
||||
async def reorder_chapters_and_activities(
|
||||
request: Request,
|
||||
course_id: int,
|
||||
course_uuid: str,
|
||||
chapters_order: ChapterUpdateOrder,
|
||||
current_user: PublicUser,
|
||||
db_session: Session,
|
||||
):
|
||||
statement = select(Course).where(Course.id == course_id)
|
||||
statement = select(Course).where(Course.course_uuid == course_uuid)
|
||||
course = db_session.exec(statement).first()
|
||||
|
||||
if not course:
|
||||
|
|
@ -341,7 +358,7 @@ async def reorder_chapters_and_activities(
|
|||
statement = (
|
||||
select(CourseChapter)
|
||||
.where(
|
||||
CourseChapter.course_id == course_id, CourseChapter.org_id == course.org_id
|
||||
CourseChapter.course_id == course.id, CourseChapter.org_id == course.org_id
|
||||
)
|
||||
.order_by(CourseChapter.order)
|
||||
)
|
||||
|
|
@ -357,7 +374,7 @@ async def reorder_chapters_and_activities(
|
|||
db_session.commit()
|
||||
|
||||
# Delete Chapters that are not in the list of chapters_order
|
||||
statement = select(Chapter).where(Chapter.course_id == course_id)
|
||||
statement = select(Chapter).where(Chapter.course_id == course.id)
|
||||
chapters = db_session.exec(statement).all()
|
||||
|
||||
chapter_ids_to_keep = [
|
||||
|
|
@ -376,7 +393,7 @@ async def reorder_chapters_and_activities(
|
|||
select(CourseChapter)
|
||||
.where(
|
||||
CourseChapter.chapter_id == chapter_order.chapter_id,
|
||||
CourseChapter.course_id == course_id,
|
||||
CourseChapter.course_id == course.id,
|
||||
)
|
||||
.order_by(CourseChapter.order)
|
||||
)
|
||||
|
|
@ -386,7 +403,7 @@ async def reorder_chapters_and_activities(
|
|||
# Add CourseChapter link
|
||||
course_chapter = CourseChapter(
|
||||
chapter_id=chapter_order.chapter_id,
|
||||
course_id=course_id,
|
||||
course_id=course.id, # type: ignore
|
||||
org_id=course.org_id,
|
||||
creation_date=str(datetime.now()),
|
||||
update_date=str(datetime.now()),
|
||||
|
|
@ -403,7 +420,7 @@ async def reorder_chapters_and_activities(
|
|||
select(CourseChapter)
|
||||
.where(
|
||||
CourseChapter.chapter_id == chapter_order.chapter_id,
|
||||
CourseChapter.course_id == course_id,
|
||||
CourseChapter.course_id == course.id,
|
||||
)
|
||||
.order_by(CourseChapter.order)
|
||||
)
|
||||
|
|
@ -424,7 +441,7 @@ async def reorder_chapters_and_activities(
|
|||
statement = (
|
||||
select(ChapterActivity)
|
||||
.where(
|
||||
ChapterActivity.course_id == course_id,
|
||||
ChapterActivity.course_id == course.id,
|
||||
ChapterActivity.org_id == course.org_id,
|
||||
)
|
||||
.order_by(ChapterActivity.order)
|
||||
|
|
@ -461,7 +478,7 @@ async def reorder_chapters_and_activities(
|
|||
chapter_id=chapter_order.chapter_id,
|
||||
activity_id=activity_order.activity_id,
|
||||
org_id=course.org_id,
|
||||
course_id=course_id,
|
||||
course_id=course.id, # type: ignore
|
||||
creation_date=str(datetime.now()),
|
||||
update_date=str(datetime.now()),
|
||||
order=activity_order.activity_id,
|
||||
|
|
|
|||
|
|
@ -25,9 +25,9 @@ from fastapi import HTTPException, status, Request
|
|||
|
||||
|
||||
async def get_collection(
|
||||
request: Request, collection_id: str, current_user: PublicUser, db_session: Session
|
||||
request: Request, collection_uuid: str, current_user: PublicUser, db_session: Session
|
||||
) -> CollectionRead:
|
||||
statement = select(Collection).where(Collection.id == collection_id)
|
||||
statement = select(Collection).where(Collection.collection_uuid == collection_uuid)
|
||||
collection = db_session.exec(statement).first()
|
||||
|
||||
if not collection:
|
||||
|
|
@ -107,12 +107,11 @@ async def create_collection(
|
|||
async def update_collection(
|
||||
request: Request,
|
||||
collection_object: CollectionUpdate,
|
||||
collection_uuid: str,
|
||||
current_user: PublicUser,
|
||||
db_session: Session,
|
||||
) -> CollectionRead:
|
||||
statement = select(Collection).where(
|
||||
Collection.id == collection_object.collection_id
|
||||
)
|
||||
statement = select(Collection).where(Collection.collection_uuid == collection_uuid)
|
||||
collection = db_session.exec(statement).first()
|
||||
|
||||
if not collection:
|
||||
|
|
@ -127,7 +126,6 @@ async def update_collection(
|
|||
|
||||
courses = collection_object.courses
|
||||
|
||||
del collection_object.collection_id
|
||||
del collection_object.courses
|
||||
|
||||
# Update only the fields that were passed in
|
||||
|
|
@ -181,9 +179,9 @@ async def update_collection(
|
|||
|
||||
|
||||
async def delete_collection(
|
||||
request: Request, collection_id: str, current_user: PublicUser, db_session: Session
|
||||
request: Request, collection_uuid: str, current_user: PublicUser, db_session: Session
|
||||
):
|
||||
statement = select(Collection).where(Collection.id == collection_id)
|
||||
statement = select(Collection).where(Collection.collection_uuid == collection_uuid)
|
||||
collection = db_session.exec(statement).first()
|
||||
|
||||
if not collection:
|
||||
|
|
@ -225,10 +223,7 @@ async def get_collections(
|
|||
)
|
||||
collections = db_session.exec(statement).all()
|
||||
|
||||
if not collections:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_409_CONFLICT, detail="No collections found"
|
||||
)
|
||||
|
||||
|
||||
collections_with_courses = []
|
||||
for collection in collections:
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ from src.db.trails import TrailRead
|
|||
|
||||
from src.services.trail.trail import get_user_trail_with_orgid
|
||||
from src.db.resource_authors import ResourceAuthor, ResourceAuthorshipEnum
|
||||
from src.db.users import PublicUser, AnonymousUser
|
||||
from src.db.users import PublicUser, AnonymousUser, User, UserRead
|
||||
from src.db.courses import (
|
||||
Course,
|
||||
CourseCreate,
|
||||
|
|
@ -26,11 +26,11 @@ from datetime import datetime
|
|||
|
||||
async def get_course(
|
||||
request: Request,
|
||||
course_id: str,
|
||||
course_uuid: str,
|
||||
current_user: PublicUser | AnonymousUser,
|
||||
db_session: Session,
|
||||
):
|
||||
statement = select(Course).where(Course.id == course_id)
|
||||
statement = select(Course).where(Course.course_uuid == course_uuid)
|
||||
course = db_session.exec(statement).first()
|
||||
|
||||
if not course:
|
||||
|
|
@ -42,21 +42,32 @@ async def get_course(
|
|||
# RBAC check
|
||||
await rbac_check(request, course.course_uuid, current_user, "read", db_session)
|
||||
|
||||
course = CourseRead.from_orm(course)
|
||||
# Get course authors
|
||||
authors_statement = (
|
||||
select(User)
|
||||
.join(ResourceAuthor)
|
||||
.where(ResourceAuthor.resource_uuid == course.course_uuid)
|
||||
)
|
||||
authors = db_session.exec(authors_statement).all()
|
||||
|
||||
# convert from User to UserRead
|
||||
authors = [UserRead.from_orm(author) for author in authors]
|
||||
|
||||
course = CourseRead(**course.dict(), authors=authors)
|
||||
|
||||
return course
|
||||
|
||||
|
||||
async def get_course_meta(
|
||||
request: Request,
|
||||
course_id: int,
|
||||
course_uuid: str,
|
||||
current_user: PublicUser | AnonymousUser,
|
||||
db_session: Session,
|
||||
) -> FullCourseReadWithTrail:
|
||||
# Avoid circular import
|
||||
from src.services.courses.chapters import get_course_chapters
|
||||
|
||||
course_statement = select(Course).where(Course.id == course_id)
|
||||
course_statement = select(Course).where(Course.course_uuid == course_uuid)
|
||||
course = db_session.exec(course_statement).first()
|
||||
|
||||
if not course:
|
||||
|
|
@ -65,12 +76,21 @@ 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)
|
||||
|
||||
course = CourseRead.from_orm(course)
|
||||
# Get course authors
|
||||
authors_statement = (
|
||||
select(User)
|
||||
.join(ResourceAuthor)
|
||||
.where(ResourceAuthor.resource_uuid == course.course_uuid)
|
||||
)
|
||||
authors = db_session.exec(authors_statement).all()
|
||||
|
||||
# convert from User to UserRead
|
||||
authors = [UserRead.from_orm(author) for author in authors]
|
||||
|
||||
course = CourseRead(**course.dict(), authors=authors)
|
||||
|
||||
# Get course chapters
|
||||
chapters = await get_course_chapters(request, course.id, db_session, current_user)
|
||||
|
|
@ -85,12 +105,13 @@ async def get_course_meta(
|
|||
return FullCourseReadWithTrail(
|
||||
**course.dict(),
|
||||
chapters=chapters,
|
||||
trail=trail,
|
||||
trail=trail if trail else None,
|
||||
)
|
||||
|
||||
|
||||
async def create_course(
|
||||
request: Request,
|
||||
org_id: int,
|
||||
course_object: CourseCreate,
|
||||
current_user: PublicUser | AnonymousUser,
|
||||
db_session: Session,
|
||||
|
|
@ -111,9 +132,9 @@ async def create_course(
|
|||
if thumbnail_file and thumbnail_file.filename:
|
||||
name_in_disk = f"{course.course_uuid}_thumbnail_{uuid4()}.{thumbnail_file.filename.split('.')[-1]}"
|
||||
await upload_thumbnail(
|
||||
thumbnail_file, name_in_disk, course_object.org_id, course.course_uuid
|
||||
thumbnail_file, name_in_disk, org_id, course.course_uuid
|
||||
)
|
||||
course_object.thumbnail = name_in_disk
|
||||
course_object.thumbnail_image = name_in_disk
|
||||
|
||||
# Insert course
|
||||
db_session.add(course)
|
||||
|
|
@ -134,17 +155,30 @@ async def create_course(
|
|||
db_session.commit()
|
||||
db_session.refresh(resource_author)
|
||||
|
||||
# Get course authors
|
||||
authors_statement = (
|
||||
select(User)
|
||||
.join(ResourceAuthor)
|
||||
.where(ResourceAuthor.resource_uuid == course.course_uuid)
|
||||
)
|
||||
authors = db_session.exec(authors_statement).all()
|
||||
|
||||
# convert from User to UserRead
|
||||
authors = [UserRead.from_orm(author) for author in authors]
|
||||
|
||||
course = CourseRead(**course.dict(), authors=authors)
|
||||
|
||||
return CourseRead.from_orm(course)
|
||||
|
||||
|
||||
async def update_course_thumbnail(
|
||||
request: Request,
|
||||
course_id: str,
|
||||
course_uuid: str,
|
||||
current_user: PublicUser | AnonymousUser,
|
||||
db_session: Session,
|
||||
thumbnail_file: UploadFile | None = None,
|
||||
):
|
||||
statement = select(Course).where(Course.id == course_id)
|
||||
statement = select(Course).where(Course.course_uuid == course_uuid)
|
||||
course = db_session.exec(statement).first()
|
||||
|
||||
name_in_disk = None
|
||||
|
|
@ -160,9 +194,7 @@ async def update_course_thumbnail(
|
|||
|
||||
# Upload thumbnail
|
||||
if thumbnail_file and thumbnail_file.filename:
|
||||
name_in_disk = (
|
||||
f"{course_id}_thumbnail_{uuid4()}.{thumbnail_file.filename.split('.')[-1]}"
|
||||
)
|
||||
name_in_disk = f"{course_uuid}_thumbnail_{uuid4()}.{thumbnail_file.filename.split('.')[-1]}"
|
||||
await upload_thumbnail(
|
||||
thumbnail_file, name_in_disk, course.org_id, course.course_uuid
|
||||
)
|
||||
|
|
@ -183,7 +215,20 @@ async def update_course_thumbnail(
|
|||
db_session.commit()
|
||||
db_session.refresh(course)
|
||||
|
||||
course = CourseRead.from_orm(course)
|
||||
# Get course authors
|
||||
authors_statement = (
|
||||
select(User)
|
||||
.join(ResourceAuthor)
|
||||
.where(ResourceAuthor.resource_uuid == course.course_uuid)
|
||||
)
|
||||
authors = db_session.exec(authors_statement).all()
|
||||
|
||||
|
||||
|
||||
# convert from User to UserRead
|
||||
authors = [UserRead.from_orm(author) for author in authors]
|
||||
|
||||
course = CourseRead(**course.dict(), authors=authors)
|
||||
|
||||
return course
|
||||
|
||||
|
|
@ -191,11 +236,11 @@ async def update_course_thumbnail(
|
|||
async def update_course(
|
||||
request: Request,
|
||||
course_object: CourseUpdate,
|
||||
course_id: int,
|
||||
course_uuid: str,
|
||||
current_user: PublicUser | AnonymousUser,
|
||||
db_session: Session,
|
||||
):
|
||||
statement = select(Course).where(Course.id == course_id)
|
||||
statement = select(Course).where(Course.course_uuid == course_uuid)
|
||||
course = db_session.exec(statement).first()
|
||||
|
||||
if not course:
|
||||
|
|
@ -219,18 +264,29 @@ async def update_course(
|
|||
db_session.commit()
|
||||
db_session.refresh(course)
|
||||
|
||||
course = CourseRead.from_orm(course)
|
||||
# Get course authors
|
||||
authors_statement = (
|
||||
select(User)
|
||||
.join(ResourceAuthor)
|
||||
.where(ResourceAuthor.resource_uuid == course.course_uuid)
|
||||
)
|
||||
authors = db_session.exec(authors_statement).all()
|
||||
|
||||
# convert from User to UserRead
|
||||
authors = [UserRead.from_orm(author) for author in authors]
|
||||
|
||||
course = CourseRead(**course.dict(), authors=authors)
|
||||
|
||||
return course
|
||||
|
||||
|
||||
async def delete_course(
|
||||
request: Request,
|
||||
course_id: str,
|
||||
course_uuid: str,
|
||||
current_user: PublicUser | AnonymousUser,
|
||||
db_session: Session,
|
||||
):
|
||||
statement = select(Course).where(Course.id == course_id)
|
||||
statement = select(Course).where(Course.course_uuid == course_uuid)
|
||||
course = db_session.exec(statement).first()
|
||||
|
||||
if not course:
|
||||
|
|
@ -275,7 +331,21 @@ async def get_courses_orgslug(
|
|||
|
||||
courses = db_session.exec(statement)
|
||||
|
||||
courses = [CourseRead.from_orm(course) for course in courses]
|
||||
courses = [CourseRead(**course.dict(),authors=[]) for course in courses]
|
||||
|
||||
# for every course, get the authors
|
||||
for course in courses:
|
||||
authors_statement = (
|
||||
select(User)
|
||||
.join(ResourceAuthor)
|
||||
.where(ResourceAuthor.resource_uuid == course.course_uuid)
|
||||
)
|
||||
authors = db_session.exec(authors_statement).all()
|
||||
|
||||
# convert from User to UserRead
|
||||
authors = [UserRead.from_orm(author) for author in authors]
|
||||
|
||||
course.authors = authors
|
||||
|
||||
return courses
|
||||
|
||||
|
|
|
|||
|
|
@ -136,8 +136,6 @@ async def update_org(
|
|||
# RBAC check
|
||||
await rbac_check(request, org.org_uuid, current_user, "update", db_session)
|
||||
|
||||
org = Organization.from_orm(org_object)
|
||||
|
||||
# Verify if the new slug is already in use
|
||||
statement = select(Organization).where(Organization.slug == org_object.slug)
|
||||
result = db_session.exec(statement)
|
||||
|
|
|
|||
|
|
@ -230,19 +230,10 @@ async def add_activity_to_trail(
|
|||
async def add_course_to_trail(
|
||||
request: Request,
|
||||
user: PublicUser,
|
||||
course_id: str,
|
||||
course_uuid: 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()
|
||||
|
||||
if trailrun:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST, detail="TrailRun already exists"
|
||||
)
|
||||
|
||||
statement = select(Course).where(Course.id == course_id)
|
||||
statement = select(Course).where(Course.course_uuid == course_uuid)
|
||||
course = db_session.exec(statement).first()
|
||||
|
||||
if not course:
|
||||
|
|
@ -250,6 +241,15 @@ async def add_course_to_trail(
|
|||
status_code=status.HTTP_404_NOT_FOUND, detail="Course not found"
|
||||
)
|
||||
|
||||
# check if run already exists
|
||||
statement = select(TrailRun).where(TrailRun.course_id == course.id)
|
||||
trailrun = db_session.exec(statement).first()
|
||||
|
||||
if trailrun:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST, detail="TrailRun already exists"
|
||||
)
|
||||
|
||||
statement = select(Trail).where(
|
||||
Trail.org_id == course.org_id, Trail.user_id == user.id
|
||||
)
|
||||
|
|
@ -308,10 +308,10 @@ async def add_course_to_trail(
|
|||
async def remove_course_from_trail(
|
||||
request: Request,
|
||||
user: PublicUser,
|
||||
course_id: str,
|
||||
course_uuid: str,
|
||||
db_session: Session,
|
||||
) -> TrailRead:
|
||||
statement = select(Course).where(Course.id == course_id)
|
||||
statement = select(Course).where(Course.course_uuid == course_uuid)
|
||||
course = db_session.exec(statement).first()
|
||||
|
||||
if not course:
|
||||
|
|
|
|||
|
|
@ -279,6 +279,37 @@ async def read_user_by_uuid(
|
|||
return user
|
||||
|
||||
|
||||
async def authorize_user_action(
|
||||
request: Request,
|
||||
db_session: Session,
|
||||
current_user: PublicUser | AnonymousUser,
|
||||
ressource_uuid: str,
|
||||
action: Literal["create", "read", "update", "delete"],
|
||||
):
|
||||
# Get user
|
||||
statement = select(User).where(User.user_uuid == current_user.user_uuid)
|
||||
user = db_session.exec(statement).first()
|
||||
|
||||
if not user:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="User does not exist",
|
||||
)
|
||||
|
||||
# RBAC check
|
||||
authorized = await authorization_verify_based_on_roles_and_authorship(
|
||||
request, current_user.id, action, ressource_uuid, db_session
|
||||
)
|
||||
|
||||
if authorized:
|
||||
return True
|
||||
else:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="You are not authorized to perform this action",
|
||||
)
|
||||
|
||||
|
||||
async def delete_user_by_id(
|
||||
request: Request,
|
||||
db_session: Session,
|
||||
|
|
@ -350,7 +381,7 @@ async def rbac_check(
|
|||
return True
|
||||
|
||||
await authorization_verify_based_on_roles_and_authorship(
|
||||
request, current_user.id, "read", action, db_session
|
||||
request, current_user.id, action, user_uuid, db_session
|
||||
)
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue