diff --git a/.gitignore b/.gitignore index 8a145bd..15efdab 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ __pycache__ .venv -scheme \ No newline at end of file +scheme +answers +results \ No newline at end of file diff --git a/main.py b/main.py index 705bc69..72981c2 100644 --- a/main.py +++ b/main.py @@ -1,21 +1,74 @@ from fastapi import FastAPI import json import random +from pathlib import Path app = FastAPI() +# student_answers_example = { +# "version": 1, +# "student": "asd", +# "attempt": 0, +# "questions": [ +# { +# "id": 4, +# "question": "Сколько винтов у теодолита?", +# "student_answer": 100 +# } +# ] +# } + +# open files once and use variables after with open("questions.json", "r", encoding="UTF-8") as f: content = json.load(f) - questions_with_answers = content["questions"] + all_questions = content["questions"] + topics = ["теодолит"] + topics_questions = [q for q in all_questions if q.get("topic") in topics] remove_keys = ["author", "answer", "topic"] - questions_without_answers = [{key: value for key, value in q.items() if key not in remove_keys} for q in questions_with_answers] + quiz_questions = [{key: value for key, value in q.items() if key not in remove_keys} for q in topics_questions] + +with open("students.json", "r", encoding="UTF-8") as f: + content = json.load(f) + students = content["students"] + +Path("answers").mkdir(parents=True, exist_ok=True) +Path("results").mkdir(parents=True, exist_ok=True) + +def check_answers(student_answers: dict): + checked_answers = student_answers + for a in checked_answers["questions"]: + question_id = a["id"] + a["correct_answer"] = next(question["answer"] for question in topics_questions if question["id"] == question_id) + a["is_correct"] = a["student_answer"] == a["correct_answer"] -@app.get("/") -def give_quiz(student: str): - quiz_questions = random.sample(questions_without_answers, len(questions_without_answers)) # questions selection would be more complicated + checked_answers["correct"] = sum([a["is_correct"] for a in checked_answers["questions"]]) + checked_answers["correct_percent"] = round(checked_answers["correct"] * 100 / len(checked_answers["questions"])) + return checked_answers + +@app.get("/students") +def show_students(): + return students + +@app.get("/give_quiz") +def give_quiz(student_id, student: str): + questions_for_student = random.sample(quiz_questions, len(quiz_questions))[:1] # random order and only first n questions return { "version": 1, + "student_id": student_id, "student": student, "attempt": 0, # TODO how to count attempts? - "questions": quiz_questions - } \ No newline at end of file + "questions": questions_for_student + } + +@app.get("/get_student_answers") +def get_answers(student_answers: str): + json_answers = json.loads(student_answers) + print(json_answers) + path_to_answers = f'answers/{json_answers["student"]}_{json_answers["attempt"]}.json' + with open(path_to_answers, 'w', encoding='utf-8') as f: + json.dump(json_answers, f, ensure_ascii=False) + + path_to_results = f'results/{json_answers["student"]}_{json_answers["attempt"]}.json' + with open(path_to_results, 'w', encoding='utf-8') as f: + json.dump(check_answers(json_answers), f, ensure_ascii=False) # TODO move checking to background? + return json_answers["student"] \ No newline at end of file diff --git a/questions.json b/questions.json index 4aa957d..c1b99a1 100644 --- a/questions.json +++ b/questions.json @@ -28,6 +28,13 @@ "author": "GT", "question": "Как называется прибор для измерения превышений?", "answer": "нивелир" + }, + { + "id": 4, + "topic": "теодолит", + "author": "GT", + "question": "Сколько винтов у теодолита?", + "answer": 520 } ] } \ No newline at end of file diff --git a/students.json b/students.json index 6371f09..27d98f4 100644 --- a/students.json +++ b/students.json @@ -2,6 +2,7 @@ "version": 1, "students": [ { + "id": 1, "name": "Екатерина Александровна Щербацкая", "professor": "Михаил Иванович Кознышев" }