48 lines
1.8 KiB
Python
48 lines
1.8 KiB
Python
import logging
|
|
import uuid
|
|
from typing import Optional
|
|
|
|
from fastapi import BackgroundTasks
|
|
|
|
from app.controllers.abc import ISpeakingController
|
|
from app.dtos.speaking import SaveSpeakingDTO
|
|
|
|
from app.services.abc import ISpeakingService
|
|
from app.configs.constants import ExamVariant, MinTimers
|
|
from app.configs.question_templates import getSpeakingTemplate
|
|
|
|
|
|
class SpeakingController(ISpeakingController):
|
|
|
|
def __init__(self, speaking_service: ISpeakingService):
|
|
self._service = speaking_service
|
|
self._logger = logging.getLogger(__name__)
|
|
|
|
async def get_speaking_part(self, task: int, topic: str, difficulty: str, second_topic: Optional[str] = None):
|
|
return await self._service.get_speaking_part(task, topic, difficulty, second_topic)
|
|
|
|
async def save_speaking(self, data: SaveSpeakingDTO, background_tasks: BackgroundTasks):
|
|
exercises = data.exercises
|
|
min_timer = data.minTimer
|
|
|
|
template = getSpeakingTemplate()
|
|
template["minTimer"] = min_timer
|
|
|
|
if min_timer < MinTimers.SPEAKING_MIN_TIMER_DEFAULT:
|
|
template["variant"] = ExamVariant.PARTIAL.value
|
|
else:
|
|
template["variant"] = ExamVariant.FULL.value
|
|
|
|
req_id = str(uuid.uuid4())
|
|
self._logger.info(f'Received request to save speaking with id: {req_id}')
|
|
|
|
background_tasks.add_task(self._service.create_videos_and_save_to_db, exercises, template, req_id)
|
|
|
|
self._logger.info('Started background task to save speaking.')
|
|
|
|
# Return response without waiting for create_videos_and_save_to_db to finish
|
|
return {**template, "id": req_id}
|
|
|
|
async def generate_video(self, *args, **kwargs):
|
|
return await self._service.generate_video(*args, **kwargs)
|