mirror of
https://github.com/rzmk/learnhouse.git
synced 2025-12-18 11:59:26 +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
|
||||
.vscode/
|
||||
|
||||
# Learnhouse
|
||||
content/*
|
||||
|
||||
# Distribution / packaging
|
||||
.Python
|
||||
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.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("/")
|
||||
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
|
||||
"""
|
||||
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}")
|
||||
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)
|
||||
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ import os
|
|||
from typing import List
|
||||
from uuid import uuid4
|
||||
from pydantic import BaseModel
|
||||
from src.services.uploads import upload_thumbnail
|
||||
from src.services.users import PublicUser, User
|
||||
from src.services.database import create_config_collection, check_database, create_database, learnhouseDB, learnhouseDB
|
||||
from src.services.security import *
|
||||
|
|
@ -16,7 +17,6 @@ class Course(BaseModel):
|
|||
name: str
|
||||
mini_description: str
|
||||
description: str
|
||||
photo: str
|
||||
learnings: List[str]
|
||||
thumbnail : str
|
||||
public: bool
|
||||
|
|
@ -79,21 +79,27 @@ async def get_course(course_id: str, org_id :str , current_user: PublicUser):
|
|||
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()
|
||||
courses = learnhouseDB["courses"]
|
||||
|
||||
# generate course_id with 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
|
||||
|
||||
hasRoleRights = await verify_user_rights_with_roles("create", current_user.user_id, course_id)
|
||||
|
||||
|
||||
|
||||
if not hasRoleRights:
|
||||
raise HTTPException(
|
||||
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=[
|
||||
current_user.user_id], creationDate=str(datetime.now()), updateDate=str(datetime.now()), **course_object.dict())
|
||||
|
||||
|
|
@ -119,20 +125,10 @@ async def update_course_thumbnail(course_id: str , current_user: PublicUser, thu
|
|||
creationDate = course["creationDate"]
|
||||
authors = course["authors"]
|
||||
if thumbnail_file:
|
||||
contents = thumbnail_file.file.read()
|
||||
name_in_disk = f"{course_id}_thumbnail_{uuid4()}.{thumbnail_file.filename.split('.')[-1]}"
|
||||
course = Course(**course).copy(update={"thumbnail": name_in_disk})
|
||||
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()
|
||||
|
||||
await upload_thumbnail( thumbnail_file, name_in_disk)
|
||||
|
||||
|
||||
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