From 3d2d415c5ee37fc8d31d42ba9d742c11723ad840 Mon Sep 17 00:00:00 2001 From: gtitov Date: Mon, 24 Oct 2022 23:41:17 +0300 Subject: [PATCH] add and edit stuff --- main.py | 53 ++++++++++++++++++++++++++++++++++++++++++----------- settings.py | 3 +++ 2 files changed, 45 insertions(+), 11 deletions(-) create mode 100644 settings.py diff --git a/main.py b/main.py index 643a12a..beb05e0 100644 --- a/main.py +++ b/main.py @@ -3,6 +3,9 @@ from fastapi.middleware.cors import CORSMiddleware # CORS import json import random from pathlib import Path +from datetime import datetime + +from settings import WAVE, END_TEST_PASSWORD, QUIZ_LENGTH app = FastAPI() @@ -18,10 +21,11 @@ app.add_middleware( # CORS allow_headers=["*"], ) + # student_answers_example = { # "version": 1, # "student": "asd", -# "attempt": 0, +# "wave": 0, # "questions": [ # { # "id": 4, @@ -35,8 +39,9 @@ app.add_middleware( # CORS with open("questions.json", "r", encoding="UTF-8") as f: content = json.load(f) all_questions = content["questions"] - topics = ["теодолит"] - topics_questions = [q for q in all_questions if q.get("topic") in topics] + topics_questions = all_questions # don't use topics for now. see next to lines to use topics + # topics = ["теодолит"] # move topics to VARIABLES + # topics_questions = [q for q in all_questions if q.get("topic") in topics] remove_keys = ["author", "answer", "topic"] quiz_questions = [{key: value for key, value in q.items() if key not in remove_keys} for q in topics_questions] @@ -74,19 +79,20 @@ def get_quiz(student_id, student: str): Returns: obj: json-like quiz """ - quiz_length = 1 + quiz_length = QUIZ_LENGTH questions_for_student = random.sample(quiz_questions, len(quiz_questions))[:quiz_length] # random order and only first n questions return { "version": 1, "student_id": student_id, "student": student, - "attempt": 0, # TODO how to count attempts? + "wave": WAVE, # волна сдачи теста + "start_time": datetime.now().strftime("%Y-%m-%dT%H-%M-%S"), "questions": questions_for_student } -@app.get("/send_student_answers") +@app.get("/save_student_answers") def send_student_answers(student_answers: str): - """Save answers as-is and checked answers + """Save answers as-is and check answers Args: student_answers (str): json-like string with student's answers @@ -95,12 +101,37 @@ def send_student_answers(student_answers: str): str: student name """ json_answers = json.loads(student_answers) - print(json_answers) - path_to_answers = f'answers/{json_answers["student"]}_{json_answers["attempt"]}.json' + timestamp_str = datetime.now().strftime("%Y-%m-%dT%H-%M-%S") + json_answers["end_time"] = timestamp_str + path_to_answers = f'answers/{json_answers["student"]}_{json_answers["wave"]}_{timestamp_str}.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' + path_to_results = f'results/{json_answers["student"]}_{json_answers["wave"]}_{timestamp_str}.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 + return json_answers["student"] + +@app.get("/end_quiz") +def end_test(password: str): + """End test and save results to csv + + Args: + password (str): password to end test + """ + if password == END_TEST_PASSWORD: + csv_string = "student,percent,start,end\n" + for file_in_results in Path("results").iterdir(): + if file_in_results.is_file() and file_in_results.suffix == ".json": + print(file_in_results) + with open(file_in_results, "r", encoding="UTF-8") as f: + content = json.load(f) + csv_string += f"{content['student']},{content['correct_percent']},{content['start_time']},{content['end_time']}\n" + timestamp = datetime.now().strftime("%Y-%m-%dT%H-%M-%S") + results_path = Path(f"results/results_{timestamp}.csv") + with open(results_path, "w", encoding='utf-8') as f: + f.write(csv_string) + return(f"Тестирование завершено. Сводные результаты сохранены в {results_path.resolve()}") + else: + return("Неверный пароль") + \ No newline at end of file diff --git a/settings.py b/settings.py new file mode 100644 index 0000000..70b9807 --- /dev/null +++ b/settings.py @@ -0,0 +1,3 @@ +WAVE = 1 +END_TEST_PASSWORD = "qwe" +QUIZ_LENGTH = 3 \ No newline at end of file