-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
265 lines (222 loc) · 7 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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import ast
import os
import queries
import flask
from flask import Flask, jsonify, request,abort
app=Flask(__name__)
import configparser
my_config_parser = configparser.ConfigParser()
my_config_parser.read('config.ini')
port = my_config_parser.get('DEFAULT','port')
host = my_config_parser.get('DEFAULT','host')
# convert dic expression to dic
def convExprToDict(expression):
dict=[]
for x in expression:
dict.append(ast.literal_eval(x[0]))
return dict
@app.route('/')
def index():
return 'hello!'
# get info about worldwide
@app.route('/covid19/all')
def get_forAll():
exp_all=queries.worldWideData()
if(exp_all is None):
abort(404)
else:
all=convExprToDict(exp_all)
dict1=all[0]
# the position
dict1["lat"]=34.80746
dict1["long"]=-40.4796
return jsonify(all)
# get info about one country
@app.route('/covid19/<countryId>')
def get_country(countryId):
# get info from the db
id=int(countryId)
exp_country=queries.countryData(id)
if(exp_country is None):
abort(404)
else:
country=convExprToDict(exp_country)
return jsonify(country)
# get info about each country,include lands
@app.route('/covid19/countries')
def get_countries():
# get info from the db, value -can be the id of the country
exp_countries=queries.countriesData()
if(exp_countries is None):
abort(404)
else:
countries=convExprToDict(exp_countries)
return jsonify(countries)
# get info about each country sorted
@app.route('/covid19/countries/sorted')
def get_countriesSorted():
# get info from the db,all the cases till now of each country
exp_countries=queries.countriesSorted()
if(exp_countries is None):
abort(404)
else:
countries=convExprToDict(exp_countries)
return jsonify(countries)
# get historical info in days
@app.route('/covid19/historical/all/<numDays>,<case>')
def get_historicalAll(numDays,case):
# if(case!='cases' and case!='deaths'):
# return jsonify("worng input!no case like this!")
# get info from the db,all the cases till now of each country
exp_history=queries.historyInfo(numDays,case)
if(exp_history is None):
abort(404)
else:
history=convExprToDict(exp_history)
# print(exp_history)
return jsonify(history)
# return the active polls only
@app.route('/ActivePolls')
def get_active_polls():
# need to return only the active
exp_active_polls=queries.activePolls()
if(exp_active_polls is None):
abort(404)
else:
active_polls=convExprToDict(exp_active_polls)
num = len(active_polls)
result = {"polls": active_polls, "num": num}
return result
# update poll, add 1 to counter of yes or no
@app.route('/polls/counter/<id>,<answer>',methods=['PUT'])
def update_poll(id,answer):
# send the db request to update the id
index=int(id)
check=queries.updatePollCount(index,answer)
if(check is None):
abort(404)
else:
if check==1:
return jsonify("succed update")
else:
return jsonify("failed updating")
# return mainlands sorted in desc order by cases
@app.route('/covid19/mainlandsSorted')
def get_mainlandSorted():
exp_mainlands=queries.mainlandSorted()
if(exp_mainlands is None):
abort(404)
else:
mainlands=convExprToDict(exp_mainlands)
return jsonify(mainlands)
# get true of false for password and user
@app.route('/Mangaer',methods=['PUT'])
def check_user():
user=request.json['user']
password=request.json['password']
# check if the user exsits
bool=queries.checkLogin(user,password)
if(bool is None):
abort(404)
else:
if bool:
check=True
else:
check=False
return jsonify(check)
# get all the polls from the db
@app.route('/polls')
def get_polls():
exp_polls=queries.allPolls()
if(exp_polls is None):
abort(404)
else:
polls=convExprToDict(exp_polls)
return jsonify(polls)
# add new poll-func 10
@app.route('/polls/<qes>',methods=['POST'])
def add_poll(qes):
q=request.json['qes']
# add new poll to db and get his id
new_id=queries.addPoll(q)
if(new_id is None):
abort(404)
else:
# return the new item
item = {"id": new_id, "title": q, "mode": 0, "yes": 0, "no": 0}
return jsonify(item)
# update poll, make him hide or show ,true is show
@app.route('/polls/mode/<id>,<mode>',methods=['PUT'])
def update_mode_poll(id,mode):
if(mode=='true'or mode=='True'):
mode=True
else:
mode=False
index=int(id)
# update the poll in db ,1-means show,0-means hide
check=queries.updatePollMode(mode,index)
if(check is None):
abort(404)
else:
if check==1:
return jsonify("succed update")
else:
return jsonify("the same value,nothing change!")
# delete poll
@app.route('/polls/<id>',methods=['DELETE'])
def delete_poll(id):
index=int(id)
check=queries.deletePoll(index)
if(check is None):
abort(404)
else:
if check == 1:
return jsonify("succed deleting")
return jsonify("failed deleting")
# return the data for graph -given days back and the contury
@app.route('/covid19/Graphs/finance/<num_back>,<id_country>')
def get_financeGraphData(num_back,id_country):
exp_graphData=queries.financeGraph(num_back,id_country)
if(exp_graphData is None):
abort(404)
else:
graphData=convExprToDict(exp_graphData)
return jsonify(graphData)
# return the data for graph -given days back and the contury in proportion
@app.route('/covid19/Graphs/finance/prop/<num_back>,<id_country>')
def get_financeGraphDataProp(num_back, id_country):
exp_graphData = queries.financeGraphProp(num_back, id_country)
if (exp_graphData is None):
abort(404)
else:
graphData = convExprToDict(exp_graphData)
return jsonify(graphData)
# return the data for graph -given days back
@app.route('/covid19/Graphs/material/<num_back>')
def get_materialGraphData(num_back):
exp_graphData = queries.materialGraph(num_back)
if(exp_graphData is None):
abort(404)
else:
graphData = convExprToDict(exp_graphData)
return jsonify(graphData)
# return the data for graph -given days back in proporstion
@app.route('/covid19/Graphs/material/prop/<num_back>')
def get_materialGraphDataProp(num_back):
exp_graphData=queries.materialGraphProp(num_back)
if(exp_graphData is None):
abort(404)
else:
graphData = convExprToDict(exp_graphData)
return jsonify(graphData)
# return the countries of finance graph
@app.route('/covid19/Graphs/finance/countries')
def get_financeCountries():
exp_countries =queries.countriesFinance()
if(exp_countries is None):
abort(404)
else:
countries = convExprToDict(exp_countries)
return jsonify(countries)
if __name__ == "__main__":
app.run(host=host, port=port)