-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
101 lines (76 loc) · 3.01 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import json
import os
from typing import List
from uuid import UUID, uuid4
import flask
from flask import Flask
from flask_cors import CORS
from flask_restful import Api
from quiz.models import Quiz, Question, AnswerOption
from quiz.QuizController import QuizController
from quiz.CacheStorageRepository import CacheStorageRepository
from quiz.StorageRepository import StorageRepository
from sql.SQLStorageRepository import SQLStorageRepository
from sql.database import db_session
app = Flask(__name__)
api = Api(app)
CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'
config_filename = f"config.{os.environ['MODE']}.json"
app.config.from_file(config_filename, json.load)
repo: StorageRepository = CacheStorageRepository()
if app.config['STORAGE'] == "SQL":
repo = SQLStorageRepository()
quiz_controller: QuizController = QuizController(repo)
class Encoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, UUID):
return obj.hex
if isinstance(obj, Quiz):
return obj.__dict__
if isinstance(obj, Question):
return obj.__dict__
if isinstance(obj, AnswerOption):
return obj.__dict__
return json.JSONEncoder.default(self, obj)
@app.route('/')
def hello_world():
return 'Hello World!!!!'
@app.route("/api/quiz", methods=['POST'])
def quiz_create_route():
quiz_name: str = flask.request.get_json()['name']
quiz: Quiz = quiz_controller.create_quiz(quiz_name)
app.logger.info(f'"{quiz.name}" successfully created')
return {"status": "success", "quiz": quiz.__dict__}, 200
@app.route("/api/quiz/all", methods=['GET'])
def quiz_get_route():
amount: int = int(flask.request.args.get("amount"))
offset: int = int(flask.request.args.get("offset"))
app.logger.info(f'Numbers of quizzes requested - Amount: {amount} & Offset: {offset}')
quizzes: List[Quiz] = quiz_controller.get_quizzes(offset, amount)
response = app.response_class(
response=json.dumps(quizzes, cls=Encoder),
status=200,
mimetype='application/json'
)
return response
@app.route("/api/question", methods=['POST'])
def create_question_route():
data = flask.request.get_json()
app.logger.info(data)
quiz_id: UUID = data['quiz']['id']
questions: List[Question] = []
for question_dict in data['questions']:
answer_options: List[AnswerOption] = []
for answer_option_dict in question_dict['answer_options']:
answer_option: AnswerOption = AnswerOption(answer_option_dict['text'], answer_option_dict['is_correct'], uuid4())
answer_options.append(answer_option)
question: Question = Question(question_dict['question'], answer_options, uuid4())
questions.append(question)
quiz_controller.add_questions_to_quiz(quiz_id, questions)
return {"status": "success"}, 200
@app.teardown_appcontext
def shutdown_session(exception=None):
db_session.remove()
if __name__ == '__main__':
app.run(host=app.config["LISTEN_TO"], debug=app.config["DEBUG"])