mirror of
https://github.com/rzmk/learnhouse.git
synced 2025-12-19 04:19:25 +00:00
feat: use forms for course creation
This commit is contained in:
parent
3581b4409f
commit
9a8e4e4492
4 changed files with 33 additions and 21 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
|
@ -9,6 +9,9 @@ __pycache__/
|
||||||
# Visual Studio Code
|
# Visual Studio Code
|
||||||
.vscode/
|
.vscode/
|
||||||
|
|
||||||
|
# Learnhouse
|
||||||
|
content/*
|
||||||
|
|
||||||
# Distribution / packaging
|
# Distribution / packaging
|
||||||
.Python
|
.Python
|
||||||
build/
|
build/
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
from fastapi import APIRouter, Depends, File, UploadFile
|
from fastapi import APIRouter, Depends, File, UploadFile, Form
|
||||||
from src.services.auth import get_current_user
|
from src.services.auth import get_current_user
|
||||||
|
|
||||||
from src.services.courses import Course, CourseChapter, create_course, create_coursechapter, delete_coursechapter, get_course, get_coursechapter, get_coursechapters, get_courses, update_course, delete_course, update_course_thumbnail, update_coursechapter
|
from src.services.courses import Course, CourseChapter, create_course, create_coursechapter, delete_coursechapter, get_course, get_coursechapter, get_coursechapters, get_courses, update_course, delete_course, update_course_thumbnail, update_coursechapter
|
||||||
|
|
@ -9,16 +9,17 @@ router = APIRouter()
|
||||||
|
|
||||||
|
|
||||||
@router.post("/")
|
@router.post("/")
|
||||||
async def api_create_course(course_object: Course, org_id :str , current_user: PublicUser = Depends(get_current_user)):
|
async def api_create_course(org_id :str , name : str = Form(), mini_description : str = Form() , description :str = Form(), public : bool = Form(), current_user: PublicUser = Depends(get_current_user) , thumbnail: UploadFile | None = None):
|
||||||
"""
|
"""
|
||||||
Create new Course
|
Create new Course
|
||||||
"""
|
"""
|
||||||
return await create_course(course_object, org_id , current_user)
|
course = Course(name=name, mini_description=mini_description, description=description, org_id=org_id, public=public , thumbnail="" , chapters=[], learnings=[])
|
||||||
|
return await create_course(course, org_id , current_user, thumbnail)
|
||||||
|
|
||||||
@router.put("/thumbnail/{course_id}")
|
@router.put("/thumbnail/{course_id}")
|
||||||
async def api_create_course_thumbnail(course_id : str, thumbnail: UploadFile | None = None, current_user: PublicUser = Depends(get_current_user)):
|
async def api_create_course_thumbnail(course_id : str, thumbnail: UploadFile | None = None, current_user: PublicUser = Depends(get_current_user)):
|
||||||
"""
|
"""
|
||||||
Create new Course Thumbnail
|
Update new Course Thumbnail
|
||||||
"""
|
"""
|
||||||
return await update_course_thumbnail(course_id, current_user, thumbnail)
|
return await update_course_thumbnail(course_id, current_user, thumbnail)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ import os
|
||||||
from typing import List
|
from typing import List
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
from src.services.uploads import upload_thumbnail
|
||||||
from src.services.users import PublicUser, User
|
from src.services.users import PublicUser, User
|
||||||
from src.services.database import create_config_collection, check_database, create_database, learnhouseDB, learnhouseDB
|
from src.services.database import create_config_collection, check_database, create_database, learnhouseDB, learnhouseDB
|
||||||
from src.services.security import *
|
from src.services.security import *
|
||||||
|
|
@ -16,7 +17,6 @@ class Course(BaseModel):
|
||||||
name: str
|
name: str
|
||||||
mini_description: str
|
mini_description: str
|
||||||
description: str
|
description: str
|
||||||
photo: str
|
|
||||||
learnings: List[str]
|
learnings: List[str]
|
||||||
thumbnail : str
|
thumbnail : str
|
||||||
public: bool
|
public: bool
|
||||||
|
|
@ -79,21 +79,27 @@ async def get_course(course_id: str, org_id :str , current_user: PublicUser):
|
||||||
return course
|
return course
|
||||||
|
|
||||||
|
|
||||||
async def create_course(course_object: Course, org_id : str , current_user: PublicUser):
|
async def create_course(course_object: Course, org_id : str , current_user: PublicUser, thumbnail_file: UploadFile | None = None):
|
||||||
await check_database()
|
await check_database()
|
||||||
courses = learnhouseDB["courses"]
|
courses = learnhouseDB["courses"]
|
||||||
|
|
||||||
# generate course_id with uuid4
|
# generate course_id with uuid4
|
||||||
course_id = str(f"course_{uuid4()}")
|
course_id = str(f"course_{uuid4()}")
|
||||||
|
|
||||||
|
# TODO(fix) : the implementation here is clearly not the best one (this entire function)
|
||||||
course_object.org_id = org_id
|
course_object.org_id = org_id
|
||||||
|
|
||||||
hasRoleRights = await verify_user_rights_with_roles("create", current_user.user_id, course_id)
|
hasRoleRights = await verify_user_rights_with_roles("create", current_user.user_id, course_id)
|
||||||
|
|
||||||
|
|
||||||
if not hasRoleRights:
|
if not hasRoleRights:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=status.HTTP_409_CONFLICT, detail="Roles : Insufficient rights to perform this action")
|
status_code=status.HTTP_409_CONFLICT, detail="Roles : Insufficient rights to perform this action")
|
||||||
|
|
||||||
|
if thumbnail_file:
|
||||||
|
name_in_disk = f"{course_id}_thumbnail_{uuid4()}.{thumbnail_file.filename.split('.')[-1]}"
|
||||||
|
await upload_thumbnail(thumbnail_file, name_in_disk)
|
||||||
|
course_object.thumbnail = name_in_disk
|
||||||
|
|
||||||
course = CourseInDB(course_id=course_id, authors=[
|
course = CourseInDB(course_id=course_id, authors=[
|
||||||
current_user.user_id], creationDate=str(datetime.now()), updateDate=str(datetime.now()), **course_object.dict())
|
current_user.user_id], creationDate=str(datetime.now()), updateDate=str(datetime.now()), **course_object.dict())
|
||||||
|
|
||||||
|
|
@ -119,19 +125,9 @@ async def update_course_thumbnail(course_id: str , current_user: PublicUser, thu
|
||||||
creationDate = course["creationDate"]
|
creationDate = course["creationDate"]
|
||||||
authors = course["authors"]
|
authors = course["authors"]
|
||||||
if thumbnail_file:
|
if thumbnail_file:
|
||||||
contents = thumbnail_file.file.read()
|
|
||||||
name_in_disk = f"{course_id}_thumbnail_{uuid4()}.{thumbnail_file.filename.split('.')[-1]}"
|
name_in_disk = f"{course_id}_thumbnail_{uuid4()}.{thumbnail_file.filename.split('.')[-1]}"
|
||||||
course = Course(**course).copy(update={"thumbnail": name_in_disk})
|
course = Course(**course).copy(update={"thumbnail": name_in_disk})
|
||||||
try:
|
await upload_thumbnail( thumbnail_file, name_in_disk)
|
||||||
with open(f"content/uploads/img/{name_in_disk}", 'wb') as f:
|
|
||||||
f.write(contents)
|
|
||||||
f.close()
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
print(e)
|
|
||||||
return {"message": "There was an error uploading the file"}
|
|
||||||
finally:
|
|
||||||
thumbnail_file.file.close()
|
|
||||||
|
|
||||||
|
|
||||||
updated_course = CourseInDB(course_id=course_id, creationDate=creationDate, authors=authors, updateDate=str(datetime.now()) , **course.dict())
|
updated_course = CourseInDB(course_id=course_id, creationDate=creationDate, authors=authors, updateDate=str(datetime.now()) , **course.dict())
|
||||||
|
|
|
||||||
12
src/services/uploads.py
Normal file
12
src/services/uploads.py
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
async def upload_thumbnail(thumbnail_file, name_in_disk):
|
||||||
|
contents = thumbnail_file.file.read()
|
||||||
|
try:
|
||||||
|
with open(f"content/uploads/img/{name_in_disk}", 'wb') as f:
|
||||||
|
f.write(contents)
|
||||||
|
f.close()
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
|
return {"message": "There was an error uploading the file"}
|
||||||
|
finally:
|
||||||
|
thumbnail_file.file.close()
|
||||||
Loading…
Add table
Add a link
Reference in a new issue