fix: video activites issue

This commit is contained in:
swve 2023-04-13 21:33:03 +02:00
parent 137a909347
commit 86645fa81a
5 changed files with 38 additions and 7 deletions

View file

@ -17,10 +17,12 @@ function DynamicCanvaModal({ submitActivity, chapterId }: any) {
const handleSubmit = async (e: any) => { const handleSubmit = async (e: any) => {
e.preventDefault(); e.preventDefault();
console.log({ activityName, activityDescription, chapterId }); console.log({ activityName, activityDescription, chapterId });
submitActivity({ submitActivity({
name: activityName, name: activityName,
chapterId: chapterId, chapterId: chapterId,
type: "dynamic", type: "dynamic",
org_id : "test",
}); });
}; };
return ( return (

View file

@ -16,12 +16,14 @@ export async function createFileActivity(file: File, type: string, data: any, ch
const formData = new FormData(); const formData = new FormData();
formData.append("coursechapter_id", chapter_id); formData.append("coursechapter_id", chapter_id);
let endpoint = `${getAPIUrl()}activities/video`; let org_id = "test";
let endpoint = `${getAPIUrl()}activities/video?org_id=${org_id}`;
if (type === "video") { if (type === "video") {
formData.append("name", data.name); formData.append("name", data.name);
formData.append("video_file", file); formData.append("video_file", file);
endpoint = `${getAPIUrl()}activities/video`; endpoint = endpoint;
} }
const result: any = await fetch(endpoint, RequestBodyForm("POST", formData)); const result: any = await fetch(endpoint, RequestBodyForm("POST", formData));

View file

@ -4,6 +4,11 @@ import os
async def upload_video(video_file, activity_id): async def upload_video(video_file, activity_id):
contents = video_file.file.read() contents = video_file.file.read()
video_format = video_file.filename.split(".")[-1] video_format = video_file.filename.split(".")[-1]
if not os.path.exists("content/uploads/video"):
# create folder
os.mkdir("content/uploads/video")
# create folder # create folder
os.mkdir(f"content/uploads/video/{activity_id}") os.mkdir(f"content/uploads/video/{activity_id}")

View file

@ -10,18 +10,37 @@ from datetime import datetime
async def create_video_activity(request: Request,name: str, coursechapter_id: str, current_user: PublicUser, video_file: UploadFile | None = None): async def create_video_activity(request: Request,name: str, coursechapter_id: str, current_user: PublicUser, video_file: UploadFile | None = None):
activities = request.app.db["activities"] activities = request.app.db["activities"]
coursechapters = request.app.db["coursechapters"] courses = request.app.db["courses"]
# generate activity_id # generate activity_id
activity_id = str(f"activity_{uuid4()}") activity_id = str(f"activity_{uuid4()}")
# get org_id from course
coursechapter = await courses.find_one(
{"chapters_content.coursechapter_id": coursechapter_id})
org_id = coursechapter["org_id"]
# check if video_file is not None # check if video_file is not None
if not video_file: if not video_file:
raise HTTPException( raise HTTPException(
status_code=status.HTTP_409_CONFLICT, detail="Video : No video file provided") status_code=status.HTTP_409_CONFLICT, detail="Video : No video file provided")
video_format = video_file.filename.split(".")[-1] if video_file.content_type not in ["video/mp4", "video/webm"]:
raise HTTPException(
status_code=status.HTTP_409_CONFLICT, detail="Video : Wrong video format")
# get video format
if video_file.filename:
video_format = video_file.filename.split(".")[-1]
else:
raise HTTPException(
status_code=status.HTTP_409_CONFLICT, detail="Video : No video file provided")
activity_object = ActivityInDB( activity_object = ActivityInDB(
org_id=org_id,
activity_id=activity_id, activity_id=activity_id,
coursechapter_id=coursechapter_id, coursechapter_id=coursechapter_id,
name=name, name=name,
@ -36,7 +55,7 @@ async def create_video_activity(request: Request,name: str, coursechapter_id: s
updateDate=str(datetime.now()), updateDate=str(datetime.now()),
) )
hasRoleRights = await verify_user_rights_with_roles(request,"create", current_user.user_id, activity_id) hasRoleRights = await verify_user_rights_with_roles(request,"create", current_user.user_id, activity_id, element_org_id=org_id)
if not hasRoleRights: if not hasRoleRights:
raise HTTPException( raise HTTPException(
@ -53,7 +72,7 @@ async def create_video_activity(request: Request,name: str, coursechapter_id: s
# todo : choose whether to update the chapter or not # todo : choose whether to update the chapter or not
# update chapter # update chapter
await coursechapters.update_one({"coursechapter_id": coursechapter_id}, { await courses.update_one({"chapters_content.coursechapter_id": coursechapter_id}, {
"$addToSet": {"activities": activity_id}}) "$addToSet": {"chapters_content.$.activities": activity_id}})
return activity return activity

View file

@ -4,6 +4,9 @@ import os
async def upload_thumbnail(thumbnail_file, name_in_disk): async def upload_thumbnail(thumbnail_file, name_in_disk):
contents = thumbnail_file.file.read() contents = thumbnail_file.file.read()
try: try:
if not os.path.exists("content/uploads/img"):
os.makedirs("content/uploads/img")
with open(f"content/uploads/img/{name_in_disk}", 'wb') as f: with open(f"content/uploads/img/{name_in_disk}", 'wb') as f:
f.write(contents) f.write(contents)
f.close() f.close()