-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.py
180 lines (167 loc) · 6.35 KB
/
routes.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
from models import db, User, Form, Field, Response, ResponseData
from webargs import fields
from flask_apispec import use_kwargs, marshal_with
from auth.utils import token_required
from app import app
from .utils import get_form_response, get_all_response
from plugins.googlesheet import GS
@app.route("/form/get_all_forms", methods=["GET"])
@token_required
def get_all_forms(**kwargs):
try:
forms = db.session.query(Form).filter_by(user_id=kwargs["user"].id).all()
if forms:
return {
"message": "Forms found",
"data": [form.serialize() for form in forms],
}, 200
else:
return {"message": "Forms not found"}, 404
except Exception as e:
print(e)
return {"message": "Something Went Wrong!"}, 500
@app.route("/form/get_complete_form/<int:form_id>", methods=["GET"])
@token_required
def get_complete_form(form_id, **kwargs):
try:
form = (
db.session.query(Form)
.filter_by(id=form_id, user_id=kwargs["user"].id)
.first()
)
if form:
fields = db.session.query(Field).filter_by(form_id=form_id).all()
if fields:
return {
"message": "Form found",
"data": {
"form": form.serialize(),
"fields": [field.serialize() for field in fields],
},
}, 200
else:
return {"message": "Form not found"}, 404
else:
return {"message": "Form not found"}, 404
except Exception as e:
print(e)
return {"message": "Something Went Wrong!"}, 500
@app.route("/form/new_response/<int:form_id>", methods=["POST"])
@use_kwargs({"data": fields.Dict(required=True)})
@token_required
def create_new_response(form_id, **kwargs):
try:
form = db.session.query(Form).filter_by(id=form_id).first()
if form:
if (
db.session.query(Response)
.filter_by(form_id=form_id, user_id=kwargs["user"].id)
.first()
):
return {"message": "Response already exists"}, 409
fields = db.session.query(Field).filter_by(form_id=form_id).all()
new_response = Response(form_id=form_id, user_id=kwargs["user"].id)
db.session.add(new_response)
db.session.flush()
new_response
data = kwargs["data"]
print
for field in fields:
if (not data.get(str(field.id))) and field["required"]:
db.session.rollback()
return {"message": "Missing required field"}, 400
new_response_data = ResponseData(
response_id=new_response.id,
field_id=field.id,
value=data.get(str(field.id)),
)
db.session.add(new_response_data)
db.session.commit()
return {
"message": "Response created successfully",
"data": get_form_response(form_id, kwargs["user"].id),
}, 201
return {"message": "Form not found"}, 404
except Exception as e:
db.session.rollback()
print(e)
return {"message": "Something Went Wrong!"}, 500
@app.route("/form/get_user_response/<int:form_id>", methods=["GET"])
@token_required
def get_user_response(form_id, **kwargs):
try:
response = (
db.session.query(Response)
.filter_by(form_id=form_id, user_id=kwargs["user"].id)
.first()
)
if response:
return {
"message": "Response found",
"data": get_form_response(form_id, kwargs["user"].id),
}, 200
else:
return {"message": "Response not found"}, 404
except Exception as e:
print(e)
return {"message": "Something Went Wrong!"}, 500
@app.route("/form/get_all_responses/<int:form_id>", methods=["GET"])
@token_required
def get_all_responses(form_id, **kwargs):
try:
form = (
db.session.query(Form)
.filter_by(id=form_id, user_id=kwargs["user"].id)
.first()
)
if form:
resp = get_all_response(form_id)
if resp:
return {"message": "Responses found", "data": resp}, 200
return {"message": "No Response Found"}, 200
return {"message": "Form not found"}, 404
except Exception as e:
print(e)
return {"message": "Something Went Wrong!"}, 500
@app.route("/form/get_response_in_google_sheet/<int:form_id>", methods=["GET"])
@token_required
def get_response_in_google_sheet(form_id, **kwargs):
try:
form = (
db.session.query(Form)
.filter_by(id=form_id, user_id=kwargs["user"].id)
.first()
)
if form:
gs = GS()
if gs.service:
if form.gsheet_id:
gsheet = gs.get_sheet(form.gsheet_id)
else:
gsheet = gs.create_sheet(form.name)
form.gsheet_id = gsheet["spreadsheetId"]
db.session.commit()
fields = db.session.query(Field).filter_by(form_id=form_id).all()
field_names = [field.question for field in fields]
values = []
values.append(field_names)
for resp in db.session.query(Response).filter_by(form_id=form_id).all():
response = []
for field in fields:
response.append(
db.session.query(ResponseData)
.filter_by(response_id=resp.id, field_id=field.id)
.first()
.value
)
values.append(response)
gs.update_spreadsheet(gsheet["spreadsheetId"], values, "A1")
return {
"message": "GoogleSheet Created",
"data": gsheet["spreadsheetUrl"],
}, 200
return {"message": "Error occured while creating sheet "}, 500
return {"message": "Form not found"}, 404
except Exception as e:
print(e)
return {"message": "Something Went Wrong!"}, 500