forked from dblunk88/pyth_api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmethods.py
195 lines (143 loc) · 6.57 KB
/
methods.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!flask/bin/python
import json
from flask import Response
from sqlalchemy import text
from Database import Answer_Type,Assessment,Questions,Question_Answer,User_Type,Users,Companies,Forms,Results
from settings import session
# TODO: Implement sendAssessments (by userid), get_pendingAssessments (by userid)
def send_form(user_id,form_id):
current_pending = session.query(Users.pending_forms).filter(Users.uid == user_id).one()
new_pending = [int(form_id)]
for form in current_pending[0]:
if int(form) not in new_pending:
new_pending.append(int(form))
session.query(Users) \
.filter(Users.uid == user_id) \
.update(dict(pending_forms=new_pending))
session.commit()
return get_user(user_id)
def submit_form(user_id,form_id,data):
current_pending = session.query(Users.pending_forms).filter(Users.uid == user_id).one()
current_completed = session.query(Users.completed_forms).filter(Users.uid == user_id).one()
new_completed = [int(form_id)]
for form in current_completed[0]:
if int(form) not in new_completed and int(form_id) != int(form):
new_completed.append(int(form))
new_pending = []
for form in current_pending[0]:
if int(form) not in new_pending and int(form_id) != int(form):
new_pending.append(int(form))
session.query(Users) \
.filter(Users.uid == user_id) \
.update(dict(completed_forms=new_completed,pending_forms=new_pending))
session.commit()
# insert a result for each assessment question
for question in data["results"]:
value = (10 / question["answer"])
answer_type = 3
if question["answer"] > 7:
answer_type = 4
elif question["answer"] < 4:
answer_type = 2
extra_data = session \
.query(Companies.company_name,
Assessment.title,
Users.uid,
Assessment.assessment_id,
Companies.company_id) \
.filter(Users.uid == user_id,Users.company_id == Companies.company_id,
Assessment.assessment_id == question["questionId"]) \
.one()
results = Results()
results.user_uid = extra_data[2]
results.category = extra_data[1]
results.parent_company = extra_data[0]
results.value = int(round(value))
results.assessment_id = int(question["questionId"])
results.answer_type_id = answer_type
session.add(results)
session.commit()
return get_user(user_id)
def get_questions():
query = session.query(Questions.questions_id,Questions.question).all()
return formatResponse(query)
def get_forms():
query = session.query(Forms.form_id,Forms.form_title).all()
results = []
for item in query:
results.append({"id": item[0],"title": item[1]})
return formatResponse(results)
def get_policy_holders_by_company(company):
query = session.query(Users.username,Users.uid,Users.email,Companies.company_name,Users.img_url) \
.join(Companies,Companies.company_id == Users.company_id) \
.filter(Users.user_type_id == 2,Companies.company_name.like("{}".format(company))).all()
results = []
for item in query:
results.append({"username": item[0],"uid": item[1],"email": item[2],"company": item[3],"img_url": item[4]})
return formatResponse(results)
def search_policy_holders(search):
query = session.query(Users.username,Users.uid,Users.email,Companies.company_name,Users.img_url) \
.join(Companies,Companies.company_id == Users.company_id) \
.filter(Users.email.like("%{}%".format(search))).all()
results = []
for item in query:
results.append({"username": item[0],"uid": item[1],"email": item[2],"company": item[3],"img_url": item[4]})
return formatResponse(results)
def get_institutions():
query = session.query(Companies.company_name,Companies.img_url).all()
results = []
for item in query:
results.append({"name": item[0],"img_url": item[1]})
return formatResponse(results)
def get_users():
query = session.query(Users.username,Users.email,User_Type.user_type_id) \
.filter(Users.user_type_id == User_Type.user_type_id).all()
return formatResponse(query)
def get_user(user_id):
query = session \
.query(Users.username,Users.email,User_Type.type,
User_Type.user_type_id,Users.pending_forms,Users.completed_forms,Users.img_url) \
.filter(Users.uid == user_id,Users.user_type_id == User_Type.user_type_id).one()
results = {
"userType": {
"id": query[3],
"type": query[2]
},
"pending_forms": query[4],
"completed_forms": query[5],
"img_url": query[6]
}
return formatResponse(results)
def get_assessments():
query = session.query(Assessment.description,Assessment.title,Assessment.category).all()
return formatResponse(query)
# ignore
def get_question_answers():
query = session.query(Questions.questions_id,Questions.question,Answer_Type.answer_type).filter(
Questions.questions_id == Question_Answer.question_id,
Answer_Type.answer_type_id == Question_Answer.answer_type_id).all()
response = []
for item in query:
response.append({"questionId": item[0],"question": item[1],"answerType": item[2]})
return formatResponse(response)
def get_form_questions(form):
query = session.query(Assessment.assessment_id,Assessment.description,Forms.form_title). \
from_statement(text("SELECT assessment_id, description, forms.form_title FROM assessment, forms "
"WHERE assessment.category = forms.form_title "
"AND forms.form_id = {};".format(form))).all()
titles = session.query(Forms.form_title)
questions = []
for item in query:
questions.append({"questionId": item[0],"question": item[1]})
res = {"formId": form,"title": query[-1][-1],"questions": questions}
return formatResponse(res)
def formatResponse(query):
resp = Response(
response=json.dumps(query),
status=200,
mimetype='application/json')
resp.headers["Access-Control-Allow-Origin"] = "*"
resp.headers["Access-Control-Allow-Credentials"] = True
resp.headers["Access-Control-Allow-Headers"] = "*"
resp.headers["Access-Control-Allow-Methods"] = ["GET", "POST"]
return resp