44 lines
1.5 KiB
Python
44 lines
1.5 KiB
Python
import random
|
|
import logging
|
|
from typing import List
|
|
|
|
from app.controllers.abc import IReadingController
|
|
from app.services.abc import IReadingService
|
|
from app.configs.constants import FieldsAndExercises
|
|
from app.helpers import ExercisesHelper
|
|
|
|
|
|
class ReadingController(IReadingController):
|
|
|
|
def __init__(self, reading_service: IReadingService):
|
|
self._service = reading_service
|
|
self._logger = logging.getLogger(__name__)
|
|
self._passages = {
|
|
"passage_1": {
|
|
"total_exercises": FieldsAndExercises.TOTAL_READING_PASSAGE_1_EXERCISES
|
|
},
|
|
"passage_2": {
|
|
"total_exercises": FieldsAndExercises.TOTAL_READING_PASSAGE_2_EXERCISES
|
|
},
|
|
"passage_3": {
|
|
"total_exercises": FieldsAndExercises.TOTAL_READING_PASSAGE_3_EXERCISES
|
|
}
|
|
}
|
|
|
|
async def get_reading_passage(self, passage_id: int, topic: str, req_exercises: List[str], difficulty: str):
|
|
try:
|
|
passage = self._passages[f'passage_{str(passage_id)}']
|
|
|
|
if len(req_exercises) == 0:
|
|
req_exercises = random.sample(FieldsAndExercises.READING_EXERCISE_TYPES, 2)
|
|
|
|
number_of_exercises_q = ExercisesHelper.divide_number_into_parts(
|
|
passage["total_exercises"], len(req_exercises)
|
|
)
|
|
|
|
return await self._service.gen_reading_passage(
|
|
passage_id, topic, req_exercises, number_of_exercises_q, difficulty
|
|
)
|
|
except Exception as e:
|
|
return str(e)
|