Added question generating API

This commit is contained in:
2021-11-28 18:17:50 +00:00
parent e6b37ce453
commit ac1ad771f0
3 changed files with 73 additions and 6 deletions

View File

@ -2,7 +2,10 @@ import os
import pathlib
from json import dump, loads
from datetime import datetime
from flask.json import jsonify
from main import app
from random import shuffle
from werkzeug.utils import secure_filename
@ -58,4 +61,36 @@ def store_data_file(file, default:bool=None):
if default:
with open(os.path.join(app.config['DATA_FILE_DIRECTORY'], '.default.txt'), 'w') as _file:
_file.write(filename)
return filename
return filename
def randomise_list(list:list):
_list = list.copy()
shuffle(_list)
return(_list)
def generate_questions(dataset:dict):
questions_list = dataset['questions']
output = []
for block in randomise_list(questions_list):
if block['type'] == 'question':
question = {
'q_type': 'question',
'q_no': block['q_no'],
'question_header': '',
'text': block['text'],
'options': randomise_list(block['options'])
}
output.append(question)
if block['type'] == 'block':
for key, _question in enumerate(randomise_list(block['questions'])):
question = {
'q_type': 'block',
'q_no': _question['q_no'],
'question_header': block['question_header'] if 'question_header' in block else '',
'block_length': len(block['questions']),
'block_q_no': key,
'text': _question['text'],
'options': randomise_list(_question['options'])
}
output.append(question)
return output