-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.py
282 lines (219 loc) · 7.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import json
from flask import Flask, redirect, url_for, session, request, render_template
from authlib.integrations.flask_client import OAuth
import os
import keys
from datetime import timedelta
from auth_decorator import login_required
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_statistics import Statistics
from sqlalchemy import create_engine
from sqlalchemy import Table, Column, Integer, String, MetaData, ForeignKey
from sqlalchemy import inspect
from thefuzz import process
import time
import models
import json
# App config
app = Flask(__name__, static_folder='./client/build', static_url_path='/')
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///database.db"
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
MODE = "PROD" # Set to PROD or DEV
engine = create_engine('sqlite:///database.db')
with engine.connect() as con:
con.execute("""CREATE TABLE IF NOT EXISTS `request` (
`index` INTEGER PRIMARY KEY AUTOINCREMENT,
`response_time` FLOAT,
`date` DateTime,
`method` String,
`size` INTEGER,
`status_code` INTEGER,
`path` TEXT,
`user_agent` TEXT,
`remote_address` TEXT,
`exception` TEXT,
`referrer` TEXT,
`browser` TEXT,
`platform` TEXT,
`mimetype` TEXT
);""")
db = SQLAlchemy(app)
class Request(db.Model):
__tablename__ = "request"
index = db.Column(db.Integer, primary_key=True, autoincrement=True)
response_time = db.Column(db.Float)
date = db.Column(db.DateTime)
method = db.Column(db.String)
size = db.Column(db.Integer)
status_code = db.Column(db.Integer)
path = db.Column(db.String)
user_agent = db.Column(db.String)
remote_address = db.Column(db.String)
exception = db.Column(db.String)
referrer = db.Column(db.String)
browser = db.Column(db.String)
platform = db.Column(db.String)
mimetype = db.Column(db.String)
statistics = Statistics(app, db, Request)
# Session config
app.secret_key = "eeeeeeeeeeeeeeeeeeeeeeee"
app.config['SESSION_COOKIE_NAME'] = 'google-login-session'
# app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(minutes=5)
# oAuth Setup
oauth = OAuth(app)
google = oauth.register(
name='google',
client_id=keys.cid,
client_secret=keys.csecret,
access_token_url='https://accounts.google.com/o/oauth2/token',
access_token_params=None,
authorize_url='https://accounts.google.com/o/oauth2/auth',
authorize_params=None,
api_base_url='https://www.googleapis.com/oauth2/v1/',
userinfo_endpoint='https://openidconnect.googleapis.com/v1/userinfo',
# This is only needed if using openId to fetch user info
client_kwargs={'scope': 'openid email profile'},
)
@app.route('/api/isUser', methods=["GET"])
@login_required
def hello_world():
return dict(session)['profile']
@app.route('/api/login', methods=["GET"])
def login():
google = oauth.create_client('google') # create the google oauth client
redirect_uri = url_for('authorize', _external=True)
return google.authorize_redirect(redirect_uri)
@app.route('/api/authorize')
def authorize():
google = oauth.create_client('google') # create the google oauth client
token = google.authorize_access_token() # Access token from google (needed to get user info)
resp = google.get('userinfo') # userinfo contains stuff u specificed in the scrope
user_info = resp.json()
user = oauth.google.userinfo() # uses openid endpoint to fetch user info
# Here you use the profile/user data that you got and query your database find/register the user
# and set ur own data in the session not the profile from google
print(user_info)
session['profile'] = user_info
session.permanent = True # make the session permanant so it keeps existing after broweser gets closed
if MODE == "DEV":
return redirect('http://localhost:3000/')
models.insertIfDoesntExist(user_info)
return redirect('/')
@app.route('/api/logout')
def logout():
for key in list(session.keys()):
session.pop(key)
if MODE == "DEV":
return redirect('http://localhost:3000/')
return redirect('/')
@app.route('/api/chronicles', methods=["GET"])
@login_required
def send_chronicles():
return models.chronicles
@app.route('/api/chronicles2', methods=["GET"])
@login_required
def send_chronicles_sem2():
return models.chronicles_sem2
@app.route('/api/chroniclesps1', methods=["GET"])
@login_required
def send_chronicles_ps1():
return models.chronicles_ps1
@app.route('/api/stationDetailsSem1', methods=["GET"])
@login_required
def send_stationDetailsSem1():
return models.details_sem1
@app.route('/api/stationDetailsSem2', methods=["GET"])
@login_required
def send_stationDetailsSem2():
return models.details_sem2
@app.route('/api/stationDetailsPS1', methods=["GET"])
@login_required
def send_stationDetails_PS1():
return models.detailsps1
@app.route('/api/rasa/stationDetails', methods=["POST"])
def send_stationDetails():
ps2sem1 = json.loads(models.details_sem1)
ps2sem2 = json.loads(models.details_sem2)
ps1 = json.loads(models.detailsps1)
query = request.get_json()
if query["ps"] == "ps1":
choices = ps1.keys()
search_results = process.extract(query["name"], choices, limit=20)
results = {}
for i in search_results:
if i[1] > 80:
results[i[0]] = ps1[i[0]]
return results
elif query["ps"] == "ps2":
if query["sem"] == "sem1":
choices = ps2sem1.keys()
search_results = process.extract(query["name"], choices, limit=20)
results = {}
for i in search_results:
if i[1] > 80:
results[i[0]] = ps2sem1[i[0]]
return results
elif query["sem"] == "sem2":
choices = ps2sem2.keys()
search_results = process.extract(query["name"], choices, limit=20)
results = {}
for i in search_results:
if i[1] > 80:
results[i[0]] = ps2sem2[i[0]]
return results
return "bad request"
@app.route('/api/problembank', methods=["GET"])
@login_required
def send_bank():
return models.bank
@app.route('/api/sem1/formStationNames', methods=["GET"])
@login_required
def send_names_sem1():
return models.find_names_sem1()
@app.route('/api/sem2/formStationNames', methods=["GET"])
@login_required
def send_names_sem2():
return models.find_names_sem2()
@app.route('/api/sem1/formResponses', methods=["GET"])
@login_required
def send_form_responses():
return models.form_responses_sem1()
@app.route('/api/sem2/formResponses', methods=["GET"])
@login_required
def send_form_responses_sem2():
return models.form_responses_sem2()
@app.route('/api/sem1/formResponsesDetailed', methods=["GET"])
@login_required
def send_form_detailed_responses():
return models.form_detailed_responses_sem1()
@app.route('/api/sem2/formResponsesDetailed', methods=["GET"])
@login_required
def send_form_detailed_responses_sem2():
return models.form_detailed_responses_sem2()
@app.route('/api/sem1/formSubmit', methods=["POST"])
@login_required
def form_submit():
data = request.get_json()
models.form_submit_sem1(data)
return "Cool"
@app.route('/api/sem2/formSubmit', methods=["POST"])
@login_required
def form_submit_sem2():
data = request.get_json()
models.form_submit_sem2(data)
return "Cool"
@app.route('/api/noOfUsers', methods=["GET"])
# @login_required
def noOfUsers():
return json.dumps(models.users.count_documents({}))
@app.route('/')
def index():
return app.send_static_file('index.html')
@app.errorhandler(404)
def not_found(e):
return app.send_static_file('index.html')
if __name__ == "__main__":
s = os.popen("python3 models.py")
app.debug = True
app.run(host="0.0.0.0")