mirror of
https://github.com/rzmk/learnhouse.git
synced 2025-12-19 04:19:25 +00:00
feat: refactor the entire learnhouse project
This commit is contained in:
parent
f556e41dda
commit
4c215e91d5
247 changed files with 7716 additions and 1013 deletions
0
apps/api/src/services/blocks/__init__.py
Normal file
0
apps/api/src/services/blocks/__init__.py
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
from uuid import uuid4
|
||||
from fastapi import HTTPException, status, UploadFile, Request
|
||||
from src.services.blocks.schemas.blocks import Block
|
||||
from src.services.blocks.utils.upload_files import upload_file_and_return_file_object
|
||||
|
||||
from src.services.users.users import PublicUser
|
||||
|
||||
|
||||
async def create_image_block(
|
||||
request: Request, image_file: UploadFile, activity_id: str
|
||||
):
|
||||
blocks = request.app.db["blocks"]
|
||||
activity = request.app.db["activities"]
|
||||
courses = request.app.db["courses"]
|
||||
|
||||
block_type = "imageBlock"
|
||||
|
||||
# get org_id from activity
|
||||
activity = await activity.find_one({"activity_id": activity_id}, {"_id": 0})
|
||||
org_id = activity["org_id"]
|
||||
|
||||
coursechapter_id = activity["coursechapter_id"]
|
||||
|
||||
# get course_id from coursechapter
|
||||
course = await courses.find_one(
|
||||
{"chapters": coursechapter_id},
|
||||
{"_id": 0},
|
||||
)
|
||||
|
||||
|
||||
# get block id
|
||||
block_id = str(f"block_{uuid4()}")
|
||||
|
||||
block_data = await upload_file_and_return_file_object(
|
||||
request,
|
||||
image_file,
|
||||
activity_id,
|
||||
block_id,
|
||||
["jpg", "jpeg", "png", "gif"],
|
||||
block_type,
|
||||
org_id,
|
||||
course["course_id"],
|
||||
)
|
||||
|
||||
# create block
|
||||
block = Block(
|
||||
block_id=block_id,
|
||||
activity_id=activity_id,
|
||||
block_type=block_type,
|
||||
block_data=block_data,
|
||||
org_id=org_id,
|
||||
course_id=course["course_id"],
|
||||
)
|
||||
|
||||
# insert block
|
||||
await blocks.insert_one(block.dict())
|
||||
|
||||
return block
|
||||
|
||||
|
||||
async def get_image_block(request: Request, file_id: str, current_user: PublicUser):
|
||||
blocks = request.app.db["blocks"]
|
||||
|
||||
video_block = await blocks.find_one({"block_id": file_id})
|
||||
|
||||
if video_block:
|
||||
return Block(**video_block)
|
||||
|
||||
else:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_409_CONFLICT, detail="Image block does not exist"
|
||||
)
|
||||
|
|
@ -0,0 +1,69 @@
|
|||
from uuid import uuid4
|
||||
from fastapi import HTTPException, status, UploadFile, Request
|
||||
from src.services.blocks.schemas.blocks import Block
|
||||
from src.services.blocks.utils.upload_files import upload_file_and_return_file_object
|
||||
|
||||
from src.services.users.users import PublicUser
|
||||
|
||||
|
||||
async def create_pdf_block(request: Request, pdf_file: UploadFile, activity_id: str):
|
||||
blocks = request.app.db["blocks"]
|
||||
activity = request.app.db["activities"]
|
||||
courses = request.app.db["courses"]
|
||||
|
||||
block_type = "pdfBlock"
|
||||
|
||||
# get org_id from activity
|
||||
activity = await activity.find_one({"activity_id": activity_id}, {"_id": 0})
|
||||
org_id = activity["org_id"]
|
||||
|
||||
# get block id
|
||||
block_id = str(f"block_{uuid4()}")
|
||||
|
||||
coursechapter_id = activity["coursechapter_id"]
|
||||
|
||||
# get course_id from coursechapter
|
||||
course = await courses.find_one(
|
||||
{"chapters": coursechapter_id},
|
||||
{"_id": 0},
|
||||
)
|
||||
|
||||
block_data = await upload_file_and_return_file_object(
|
||||
request,
|
||||
pdf_file,
|
||||
activity_id,
|
||||
block_id,
|
||||
["pdf"],
|
||||
block_type,
|
||||
org_id,
|
||||
course["course_id"],
|
||||
)
|
||||
|
||||
# create block
|
||||
block = Block(
|
||||
block_id=block_id,
|
||||
activity_id=activity_id,
|
||||
block_type=block_type,
|
||||
block_data=block_data,
|
||||
org_id=org_id,
|
||||
course_id=course["course_id"],
|
||||
)
|
||||
|
||||
# insert block
|
||||
await blocks.insert_one(block.dict())
|
||||
|
||||
return block
|
||||
|
||||
|
||||
async def get_pdf_block(request: Request, file_id: str, current_user: PublicUser):
|
||||
blocks = request.app.db["blocks"]
|
||||
|
||||
pdf_block = await blocks.find_one({"block_id": file_id})
|
||||
|
||||
if pdf_block:
|
||||
return Block(**pdf_block)
|
||||
|
||||
else:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_409_CONFLICT, detail="Video file does not exist"
|
||||
)
|
||||
|
|
@ -0,0 +1,70 @@
|
|||
from typing import List, Literal
|
||||
from uuid import uuid4
|
||||
from fastapi import Request
|
||||
from pydantic import BaseModel
|
||||
from src.services.blocks.schemas.blocks import Block
|
||||
from src.services.users.users import PublicUser
|
||||
|
||||
|
||||
class option(BaseModel):
|
||||
option_id: str
|
||||
option_type: Literal["text", "image"]
|
||||
option_data: str
|
||||
|
||||
|
||||
class answer(BaseModel):
|
||||
question_id: str
|
||||
option_id: str
|
||||
|
||||
|
||||
class question(BaseModel):
|
||||
question_id: str
|
||||
question_value:str
|
||||
options: List[option]
|
||||
|
||||
|
||||
class quizBlock(BaseModel):
|
||||
questions: List[question]
|
||||
answers: List[answer]
|
||||
|
||||
|
||||
async def create_quiz_block(request: Request, quizBlock: quizBlock, activity_id: str, user: PublicUser):
|
||||
blocks = request.app.db["blocks"]
|
||||
activities = request.app.db["activities"]
|
||||
request.app.db["courses"]
|
||||
|
||||
# Get org_id from activity
|
||||
activity = await activities.find_one({"activity_id": activity_id}, {"_id": 0, "org_id": 1})
|
||||
org_id = activity["org_id"]
|
||||
|
||||
# Get course_id from activity
|
||||
course = await activities.find_one({"activity_id": activity_id}, {"_id": 0, "course_id": 1})
|
||||
|
||||
block_id = str(f"block_{uuid4()}")
|
||||
|
||||
# create block
|
||||
block = Block(block_id=block_id, activity_id=activity_id,
|
||||
block_type="quizBlock", block_data=quizBlock, org_id=org_id, course_id=course["course_id"])
|
||||
|
||||
# insert block
|
||||
await blocks.insert_one(block.dict())
|
||||
|
||||
return block
|
||||
|
||||
|
||||
async def get_quiz_block_options(request: Request, block_id: str, user: PublicUser):
|
||||
blocks = request.app.db["blocks"]
|
||||
# find block but get only the options
|
||||
block = await blocks.find_one({"block_id": block_id, }, {
|
||||
"_id": 0, "block_data.answers": 0})
|
||||
|
||||
return block
|
||||
|
||||
async def get_quiz_block_answers(request: Request, block_id: str, user: PublicUser):
|
||||
blocks = request.app.db["blocks"]
|
||||
|
||||
# find block but get only the answers
|
||||
block = await blocks.find_one({"block_id": block_id, }, {
|
||||
"_id": 0, "block_data.questions": 0})
|
||||
|
||||
return block
|
||||
|
|
@ -0,0 +1,73 @@
|
|||
from uuid import uuid4
|
||||
from fastapi import HTTPException, status, UploadFile, Request
|
||||
from src.services.blocks.schemas.blocks import Block
|
||||
from src.services.blocks.utils.upload_files import upload_file_and_return_file_object
|
||||
|
||||
from src.services.users.users import PublicUser
|
||||
|
||||
|
||||
async def create_video_block(
|
||||
request: Request, video_file: UploadFile, activity_id: str
|
||||
):
|
||||
blocks = request.app.db["blocks"]
|
||||
activity = request.app.db["activities"]
|
||||
courses = request.app.db["courses"]
|
||||
|
||||
block_type = "videoBlock"
|
||||
|
||||
# get org_id from activity
|
||||
activity = await activity.find_one(
|
||||
{"activity_id": activity_id}, {"_id": 0}
|
||||
)
|
||||
org_id = activity["org_id"]
|
||||
|
||||
# get block id
|
||||
block_id = str(f"block_{uuid4()}")
|
||||
|
||||
coursechapter_id = activity["coursechapter_id"]
|
||||
|
||||
# get course_id from coursechapter
|
||||
course = await courses.find_one(
|
||||
{"chapters": coursechapter_id},
|
||||
{"_id": 0},
|
||||
)
|
||||
|
||||
block_data = await upload_file_and_return_file_object(
|
||||
request,
|
||||
video_file,
|
||||
activity_id,
|
||||
block_id,
|
||||
["mp4", "webm", "ogg"],
|
||||
block_type,
|
||||
org_id,
|
||||
course["course_id"],
|
||||
)
|
||||
|
||||
# create block
|
||||
block = Block(
|
||||
block_id=block_id,
|
||||
activity_id=activity_id,
|
||||
block_type=block_type,
|
||||
block_data=block_data,
|
||||
org_id=org_id,
|
||||
course_id=course["course_id"],
|
||||
)
|
||||
|
||||
# insert block
|
||||
await blocks.insert_one(block.dict())
|
||||
|
||||
return block
|
||||
|
||||
|
||||
async def get_video_block(request: Request, file_id: str, current_user: PublicUser):
|
||||
blocks = request.app.db["blocks"]
|
||||
|
||||
video_block = await blocks.find_one({"block_id": file_id})
|
||||
|
||||
if video_block:
|
||||
return Block(**video_block)
|
||||
|
||||
else:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_409_CONFLICT, detail="Video file does not exist"
|
||||
)
|
||||
12
apps/api/src/services/blocks/schemas/blocks.py
Normal file
12
apps/api/src/services/blocks/schemas/blocks.py
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
|
||||
from typing import Any, Literal
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class Block(BaseModel):
|
||||
block_id: str
|
||||
activity_id: str
|
||||
course_id: str
|
||||
org_id: str
|
||||
block_type: Literal["quizBlock", "videoBlock", "pdfBlock", "imageBlock"]
|
||||
block_data: Any
|
||||
10
apps/api/src/services/blocks/schemas/files.py
Normal file
10
apps/api/src/services/blocks/schemas/files.py
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class BlockFile(BaseModel):
|
||||
file_id: str
|
||||
file_format: str
|
||||
file_name: str
|
||||
file_size: int
|
||||
file_type: str
|
||||
activity_id: str
|
||||
58
apps/api/src/services/blocks/utils/upload_files.py
Normal file
58
apps/api/src/services/blocks/utils/upload_files.py
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
import uuid
|
||||
from fastapi import HTTPException, Request, UploadFile, status
|
||||
from src.services.blocks.schemas.files import BlockFile
|
||||
from src.services.utils.upload_content import upload_content
|
||||
|
||||
|
||||
async def upload_file_and_return_file_object(
|
||||
request: Request,
|
||||
file: UploadFile,
|
||||
activity_id: str,
|
||||
block_id: str,
|
||||
list_of_allowed_file_formats: list,
|
||||
type_of_block: str,
|
||||
org_id: str,
|
||||
course_id: str,
|
||||
):
|
||||
# get file id
|
||||
file_id = str(uuid.uuid4())
|
||||
|
||||
# get file format
|
||||
file_format = file.filename.split(".")[-1]
|
||||
|
||||
# validate file format
|
||||
if file_format not in list_of_allowed_file_formats:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_409_CONFLICT, detail="File format not supported"
|
||||
)
|
||||
|
||||
# create file
|
||||
file_binary = await file.read()
|
||||
|
||||
# get file size
|
||||
file_size = len(await file.read())
|
||||
|
||||
# get file type
|
||||
file_type = file.content_type
|
||||
|
||||
# get file name
|
||||
file_name = file.filename
|
||||
|
||||
# create file object
|
||||
uploadable_file = BlockFile(
|
||||
file_id=file_id,
|
||||
file_format=file_format,
|
||||
file_name=file_name,
|
||||
file_size=file_size,
|
||||
file_type=file_type,
|
||||
activity_id=activity_id,
|
||||
)
|
||||
|
||||
await upload_content(
|
||||
f"courses/{course_id}/activities/{activity_id}/dynamic/blocks/{type_of_block}/{block_id}",
|
||||
org_id=org_id,
|
||||
file_binary=file_binary,
|
||||
file_and_format=f"{file_id}.{file_format}",
|
||||
)
|
||||
|
||||
return uploadable_file
|
||||
Loading…
Add table
Add a link
Reference in a new issue