mirror of
https://github.com/rzmk/learnhouse.git
synced 2025-12-19 04:19:25 +00:00
feat: init activities
This commit is contained in:
parent
ee07f49139
commit
727f17ba7c
9 changed files with 358 additions and 370 deletions
|
|
@ -1,7 +1,16 @@
|
|||
from sqlmodel import Session, select
|
||||
from src.db.chapters import Chapter
|
||||
from src.db.activities import (
|
||||
Activity,
|
||||
ActivityRead,
|
||||
ActivitySubTypeEnum,
|
||||
ActivityTypeEnum,
|
||||
)
|
||||
from src.db.chapter_activities import ChapterActivity
|
||||
from src.db.course_chapters import CourseChapter
|
||||
from src.db.users import PublicUser
|
||||
from src.security.rbac.rbac import authorization_verify_based_on_roles
|
||||
from src.services.courses.activities.uploads.pdfs import upload_pdf
|
||||
from src.services.users.users import PublicUser
|
||||
from src.services.courses.activities.activities import ActivityInDB
|
||||
from fastapi import HTTPException, status, UploadFile, Request
|
||||
from uuid import uuid4
|
||||
from datetime import datetime
|
||||
|
|
@ -10,26 +19,35 @@ from datetime import datetime
|
|||
async def create_documentpdf_activity(
|
||||
request: Request,
|
||||
name: str,
|
||||
coursechapter_id: str,
|
||||
chapter_id: str,
|
||||
current_user: PublicUser,
|
||||
db_session: Session,
|
||||
pdf_file: UploadFile | None = None,
|
||||
):
|
||||
activities = request.app.db["activities"]
|
||||
courses = request.app.db["courses"]
|
||||
users = request.app.db["users"]
|
||||
# get chapter_id
|
||||
statement = select(Chapter).where(Chapter.id == chapter_id)
|
||||
chapter = db_session.exec(statement).first()
|
||||
|
||||
# get user
|
||||
user = await users.find_one({"user_id": current_user.user_id})
|
||||
if not chapter:
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
detail="Chapter not found",
|
||||
)
|
||||
|
||||
# generate activity_id
|
||||
activity_id = str(f"activity_{uuid4()}")
|
||||
statement = select(CourseChapter).where(CourseChapter.chapter_id == chapter_id)
|
||||
coursechapter = db_session.exec(statement).first()
|
||||
|
||||
# get org_id from course
|
||||
coursechapter = await courses.find_one(
|
||||
{"chapters_content.coursechapter_id": coursechapter_id}
|
||||
)
|
||||
if not coursechapter:
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
detail="CourseChapter not found",
|
||||
)
|
||||
|
||||
org_id = coursechapter["org_id"]
|
||||
# get org_id
|
||||
org_id = coursechapter.id
|
||||
|
||||
# create activity uuid
|
||||
activity_uuid = f"activity_{uuid4()}"
|
||||
|
||||
# check if pdf_file is not None
|
||||
if not pdf_file:
|
||||
|
|
@ -51,45 +69,48 @@ async def create_documentpdf_activity(
|
|||
status_code=status.HTTP_409_CONFLICT, detail="Pdf : No pdf file provided"
|
||||
)
|
||||
|
||||
activity_object = ActivityInDB(
|
||||
org_id=org_id,
|
||||
activity_id=activity_id,
|
||||
coursechapter_id=coursechapter_id,
|
||||
# Create activity
|
||||
activity = Activity(
|
||||
name=name,
|
||||
type="documentpdf",
|
||||
course_id=coursechapter["course_id"],
|
||||
activity_type=ActivityTypeEnum.TYPE_DOCUMENT,
|
||||
activity_sub_type=ActivitySubTypeEnum.SUBTYPE_DOCUMENT_PDF,
|
||||
content={
|
||||
"documentpdf": {
|
||||
"filename": "documentpdf." + pdf_format,
|
||||
"activity_id": activity_id,
|
||||
}
|
||||
"filename": "documentpdf." + pdf_format,
|
||||
"activity_uuid": activity_uuid,
|
||||
},
|
||||
creationDate=str(datetime.now()),
|
||||
updateDate=str(datetime.now()),
|
||||
published_version=1,
|
||||
version=1,
|
||||
org_id=org_id is not None,
|
||||
course_id=coursechapter.course_id,
|
||||
activity_uuid=activity_uuid,
|
||||
creation_date=str(datetime.now()),
|
||||
update_date=str(datetime.now()),
|
||||
)
|
||||
|
||||
await authorization_verify_based_on_roles(
|
||||
request,
|
||||
current_user.user_id,
|
||||
"create",
|
||||
user["roles"],
|
||||
activity_id,
|
||||
)
|
||||
# Insert Activity in DB
|
||||
db_session.add(activity)
|
||||
db_session.commit()
|
||||
db_session.refresh(activity)
|
||||
|
||||
# create activity
|
||||
activity = ActivityInDB(**activity_object.dict())
|
||||
await activities.insert_one(activity.dict())
|
||||
# Add activity to chapter
|
||||
activity_chapter = ChapterActivity(
|
||||
chapter_id=(int(chapter_id)),
|
||||
activity_id=activity.id is not None,
|
||||
course_id=coursechapter.course_id,
|
||||
org_id=coursechapter.org_id,
|
||||
creation_date=str(datetime.now()),
|
||||
update_date=str(datetime.now()),
|
||||
order=1,
|
||||
)
|
||||
|
||||
# upload pdf
|
||||
if pdf_file:
|
||||
# get pdffile format
|
||||
await upload_pdf(pdf_file, activity_id, org_id, coursechapter["course_id"])
|
||||
await upload_pdf(pdf_file, activity.id, org_id, coursechapter.course_id)
|
||||
|
||||
# todo : choose whether to update the chapter or not
|
||||
# update chapter
|
||||
await courses.update_one(
|
||||
{"chapters_content.coursechapter_id": coursechapter_id},
|
||||
{"$addToSet": {"chapters_content.$.activities": activity_id}},
|
||||
)
|
||||
# Insert ChapterActivity link in DB
|
||||
db_session.add(activity_chapter)
|
||||
db_session.commit()
|
||||
db_session.refresh(activity_chapter)
|
||||
|
||||
return activity
|
||||
return ActivityRead.from_orm(activity)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue