-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathform_orm.py
70 lines (51 loc) · 2.18 KB
/
form_orm.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
from custom_exception import FormNotFoundError, QuestionNotFoundError
from flask import jsonify
from sqlalchemy.orm import Session
from sqlalchemy import create_engine
from database import Form, Question, Response, Answer, question_form_association
from utils import DB_URL
engine = create_engine(DB_URL, echo=True)
def create_response(form_id, response_id, questions_data):
try:
# Create a session to interact with the database
session = Session(engine)
# Check if the response already exists
response = session.query(Response).get(response_id)
if response:
return jsonify({"error": "Response already exists"}), 400
# Check if the form exists
form = session.query(Form).get(form_id)
if not form:
raise FormNotFoundError("Invalid form_id")
for question_data in questions_data:
question_id = question_data.get("question_id")
# Query the database to get the question
question = session.query(Question).get(question_id)
# Check if the question do not exists
if not question:
raise QuestionNotFoundError("Invalid question_id")
# Create a new response for the form
response = Response(form=form)
session.add(response)
session.commit()
for question_data in questions_data:
question_id = question_data.get("question_id")
answer_text = question_data.get("answer")
question = (
session.query(Question)
.join(question_form_association)
.join(Form)
.filter(Question.id == question_id, Form.id == form_id)
.first()
)
answer = Answer(text=answer_text, question=question, response=response)
session.add(answer)
session.commit()
session.close()
return jsonify({"message": "Responses submitted successfully"}), 200
except FormNotFoundError as e:
raise e
except QuestionNotFoundError as e:
raise e
except Exception as e:
return jsonify({"error": str(e)}), 500