-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
288 lines (249 loc) · 9.72 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
283
284
285
286
287
288
# import google
# Python standard libraries
import json
import os, datetime
import sqlite3
import requests
# Third party libraries
from flask import Flask, redirect, request, url_for, render_template, g
from flask_login import (
LoginManager,
current_user,
login_required,
login_user,
logout_user,
)
from oauthlib.oauth2 import WebApplicationClient
import requests
import psycopg2
def get_db():
if "db" not in g:
DATABASE_URL = os.environ['DATABASE_URL']
g.db = psycopg2.connect(DATABASE_URL,sslmode='prefer')
return g.db
# Internal imports
# from db import init_db_command
from user import User
from backend import backend, get_preferences, get_cgpi
# Configuration
GOOGLE_CLIENT_ID = os.getenv('GOOGLE_CLIENT_ID',None)
GOOGLE_CLIENT_SECRET = os.getenv('GOOGLE_CLIENT_SECRET',None)
# from google import GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET
GOOGLE_DISCOVERY_URL = (
"https://accounts.google.com/.well-known/openid-configuration"
)
# Flask app setup
app = Flask(__name__)
app.secret_key = os.environ.get("SECRET_KEY") or "local-secret"
# or os.urandom(24)
#registering blueprints
app.register_blueprint(backend)
# User session management setup
# https://flask-login.readthedocs.io/en/latest
login_manager = LoginManager()
login_manager.init_app(app)
@login_manager.unauthorized_handler
def unauthorized():
print("Unauthorized access, Redirecting to index")
# return "You must be logged in to access this content.", 403
# return render_template('login.html')
return redirect(url_for('index'))
# Naive database setup
# try:
# init_db_command()
# except Exception as e:
# print(e)
# # Assume it's already been created
# pass
# OAuth2 client setup
client = WebApplicationClient(GOOGLE_CLIENT_ID)
# Flask-Login helper to retrieve a user from our db
@login_manager.user_loader
def load_user(user_id):
return User.get(user_id)
@app.before_request
def enforceHttpsInHeroku():
if request.headers.get('X-Forwarded-Proto') == 'http':
url = request.url.replace('http://', 'https://', 1)
code = 301
return redirect(url, code=code)
@app.route("/")
def index():
if current_user.is_authenticated:
logout_user()
return render_template('login.html')
# return redirect(url_for('logout'))
else:
return render_template('login.html')
# return "<a href='/login'>login</a>"
@app.route("/login") #(also checking unicode support)
def login():
# Find out what URL to hit for Google login
try:
google_provider_cfg = get_google_provider_cfg()
authorization_endpoint = google_provider_cfg["authorization_endpoint"]
# Use library to construct the request for login and provide
# scopes that let you retrieve user's profile from Google
request_uri = client.prepare_request_uri(
authorization_endpoint,
redirect_uri=request.base_url + "/callback",
# redirect_uri="/callback",
scope=["openid", "email", "profile"],
)
return redirect(request_uri)
except (requests.exceptions.ConnectionError):
return '<html><body><center><h1 style="margin-top:10%">Check Your Network Connection</h1></center</body></html>'
@app.route("/login/callback")
def callback():
print("Reached at login/callback")
# Get authorization code Google sent back to you
code = request.args.get("code")
# Find out what URL to hit to get tokens that allow you to ask for
# things on behalf of a user
google_provider_cfg = get_google_provider_cfg()
token_endpoint = google_provider_cfg["token_endpoint"]
# Prepare and send request to get tokens! Yay tokens!
token_url, headers, body = client.prepare_token_request(
token_endpoint,
authorization_response=request.url,
redirect_url=request.base_url,
code=code,
)
token_response = requests.post(
token_url,
headers=headers,
data=body,
auth=(GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET),
)
# Parse the tokens!
client.parse_request_body_response(json.dumps(token_response.json()))
# Now that we have tokens (yay) let's find and hit URL
# from Google that gives user's profile information,
# including their Google Profile Image and Email
userinfo_endpoint = google_provider_cfg["userinfo_endpoint"]
uri, headers, body = client.add_token(userinfo_endpoint)
userinfo_response = requests.get(uri, headers=headers, data=body)
# We want to make sure their email is verified.
# The user authenticated with Google, authorized our
# app, and now we've verified their email through Google!
if userinfo_response.json().get("email_verified"):
unique_id = userinfo_response.json()["sub"]
users_email = userinfo_response.json()["email"]
# picture = userinfo_response.json()["picture"]
users_name = userinfo_response.json()["given_name"]
else:
return "User email not available or not verified by Google.", 400
# Create a user in our db with the information provided
# by Google
try:
# Verifying user is from nith or not
if not (users_email.endswith('@nith.ac.in')):
return redirect(url_for('invalid_email'))
elif (users_email.startswith('iiitu')):
return "Sorry, It's only for NITians"
elif not (users_email[:2].isnumeric()):
return "Don't Try! it's only for Students"
else:
print(f"{users_email} is valid.")
# Finding students Rollno. and branch
current_year = str(datetime.datetime.now().year)[2:]
current_month = datetime.datetime.now().month
student_sem = None
student_year = int(current_year) - int(users_email[:2])
if(current_month > 5):
student_year += 1
student_sem = str(2 * student_year - 1)
else:
student_sem = str(2 * student_year)
roll_number = users_email[:users_email.index('@')]
branch_name = ''
branch_code = ''
users_email = str(users_email)
if('mi5' in users_email):
branch_name = 'CSE-Dual'
branch_code = 'CS'
elif('mi4' in users_email):
branch_name = 'ECE-Dual'
branch_code = 'EC'
elif(users_email[2] == '3'):
branch_name = 'Mechanical'
branch_code = 'MC'
elif(users_email[2] == '5'):
branch_name = 'CSE'
branch_code = 'CS'
elif(users_email[2] == '6'):
branch_name = 'Architecture'
branch_code = 'AR'
elif(users_email[2] == '7'):
branch_name = 'Chemical'
branch_code = 'CH'
elif(users_email[2] == '1'):
branch_name = 'Civil'
branch_code = 'CE'
elif(users_email[2] == '4'):
branch_name = 'ECE'
branch_code = 'EC'
elif(users_email[2] == '2'):
branch_name = 'Electrical'
branch_code = 'EE'
elif(users_email[2] == '8'):
branch_name = 'Material'
branch_code = 'MS'
student_cgpi = get_cgpi(roll_number)
if student_cgpi is None:
return "CGPI not found"
user = User(
id_=unique_id, name=users_name, email=users_email, roll_number=roll_number, branch=branch_name, branch_code=branch_code, semester=student_sem, cgpi=student_cgpi
)
login_user(user)
# Doesn't exist? Add to database
try:
if not User.get(unique_id):
User.create(unique_id, users_name, users_email, roll_number, branch_name, branch_code, student_sem, student_cgpi)
except Exception as e:
print(e)
return "FFFF"
print("User login success")
# Begin user session by logging the user in
if student_year == '1':
return "You have enough time for open elective choice! Now just focus on your study only :)"
elif (student_sem == '7' or student_sem == '6'):
return redirect(url_for('home'))
# elif(str(roll_number).startswith("17")):
# return redirect(url_for('home'))
else:
return "Open elective is not for you"
except:
redirect(url_for('invalid_email'))
@app.route("/invalid_email")
def invalid_email():
return render_template('invalid_email.html')
@app.route("/home")
@login_required
def home():
subjects = get_preferences(current_user.roll_number)
return render_template('student_details.html', subjects = subjects)
@app.route("/about")
def about():
return render_template('about.html')
@app.route("/syllabus")
def syllabus():
return render_template('syllabus.html')
@app.route("/logout")
@login_required
def logout():
logout_user()
return redirect(url_for("index"))
def get_google_provider_cfg():
return requests.get(GOOGLE_DISCOVERY_URL).json()
@app.errorhandler(500)
def internal_error(error):
return "500 error"
if __name__ == "__main__":
# from db import get_db
# from backend import do_allotment
# with app.app_context():
# get_db()
# do_allotment()
# app.run(ssl_context=('cert.pem','key.pem'))
app.run(ssl_context='adhoc', debug=0)