-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.py
294 lines (226 loc) · 9.24 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
289
290
291
292
293
294
import os
import sqlite3
from flask import Flask, flash, redirect, render_template, request, session
from flask_session import Session
from tempfile import mkdtemp
from datetime import datetime
import pandas as pd
from werkzeug.exceptions import default_exceptions, HTTPException, InternalServerError
from werkzeug.security import check_password_hash, generate_password_hash
from helpers import apology, login_required, password_check, user_location, lookdata
app = Flask(__name__)
app.config["TEMPLATES_AUTO_RELOAD"] = True
@app.after_request
def after_request(response):
response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
response.headers["Expires"] = 0
response.headers["Pragma"] = "no-cache"
return response
app.config["SESSION_FILE_DIR"] = mkdtemp()
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
Session(app)
# Connecting to database
conn = sqlite3.connect("database.db", timeout=50)
db = conn.cursor()
try:
sqliteConnection = sqlite3.connect('database.db', check_same_thread=False)
db = sqliteConnection.cursor()
print("Database created and Successfully Connected to SQLite")
sqlite_select_Query = "select sqlite_version();"
db.execute(sqlite_select_Query)
record = db.fetchall()
print("SQLite Database Version is: ", record)
except sqlite3.Error as error:
print("Error while connecting to sqlite", error)
# Created a table one time use
#db.execute("""CREATE TABLE 'info' ('id' integer PRIMARY KEY AUTOINCREMENT NOT NULL, 'rank' integer, 'institute' text, 'course' text, 'cutoff' real, 'exam' text)""")
#print("table successfull")
# Insert into database's info
rows = lookdata()
"""
print(rows)
print(len(rows))
for row in rows:
institute = row["intitute name"]
course = row["branch"]
rank = row["rank"]
cutoff = row["cutoff"]
exam = row["Exam"]
srno = row["sr no"]
"""
"""
for row in rows:
#print([institute, course, rank, cutoff, exam])
db.execute("UPDATE 'info' SET (institute) = ?", [institute])
db.execute("UPDATE 'info' SET (course) = ?", [course])
db.execute("UPDATE 'info' SET (rank) = (?)", [rank])
db.execute("UPDATE 'info' SET (cutoff) = (?)", [cutoff])
db.execute("UPDATE 'info' SET (exam) = (?)", [exam])
"""
"""
db.execute('DECLARE @i int = 0 WHILE @i < 200 BEGIN SET @i = @i + 1 institute = row["intitute name"] course = row["branch"] rank = row["rank"] cutoff = row["cutoff"] exam = row["Exam"] srno = row["sr no"] db.execute("INSERT INTO info (institute, course, rank, cutoff, exam) VALUES(?, ?, ?, ?, ?)", [institute, course, rank, cutoff, exam] END')
"""
#db.execute("SELECT * FROM info")
#r = db.fetchall()
#print(r)
#print("SUCCESS")
data = pd.read_csv("Database1.csv")
print(data)
@app.route("/index")
@login_required
def index():
# Homepage
rows = [{ "sr no": 1,
"rank": 56584,
"cutoff": 95.5178877,
"institute name": "5418 - Guru Gobind Singh College of Engineering & Research Centre, Nashik.",
"branch": "Mechanical Engineering",
"Exam": "MHT-CET"
},
{
"sr no": 2,
"rank": 57702,
"cutoff": 91.4432402,
"institute name": "3218 - Aldel Education Trust's St. John College of Engineering & Management, Vevoor,\nPalghar",
"branch": "Electronics and Telecommunication\nEngg",
"Exam": "MHT-CET"
}]
return render_template("index.html", rows=rows)
@app.route("/login", methods=["GET", "POST"])
def login():
# Logs user in
# Forget any user_id
session.clear()
# User reached route via POST (as by submitting a form via POST)
if request.method == "POST":
# Ensure username was submitted
if not request.form.get("username"):
return apology("must provide username", 400)
# Ensure password was submitted
elif not request.form.get("password"):
return apology("must provide password", 400)
# Query database for username
db.execute("SELECT * FROM users WHERE username = ?", [request.form.get("username")])
rows = db.fetchall()
print("BBBBBBBBB")
print(rows)
# Ensure username exists and password is correct
a = request.form.get("password")
print("AAAAAAAAAAAAAAAAAAAAAAA")
print(a)
# or not check_password_hash(rows[0][2]) == check_password_hash(a)
if (len(rows) != 1):
return apology("invalid username and/or password", 400)
# Remember which user has logged in
session["user_id"] = rows[0][0]
db.execute("SELECT * FROM users")
print(db.fetchall())
# Redirect user to home page
return redirect("/details")
# User reached route via GET (as by clicking a link or via redirect)
else:
return render_template("login.html")
@app.route("/register", methods=["GET", "POST"])
def register():
# Register user
# User reached route via POST (as by submitting a form via POST)
if request.method == "POST":
# Creat variables
username = request.form.get("username")
password = request.form.get("password")
confirmation = request.form.get("confirmation")
email = request.form.get("confirmation")
# Ensure user has written username
if not username:
return apology("Must provide Username", 400)
# Ensure no other user is in database with same username
db.execute("SELECT * FROM users WHERE username = ?", (username,))
rows = db.fetchall()
print(f"TYPE : ...............{type(rows)}")
if len(rows) != 0:
return apology("Username is already taken")
# Ensure user has written password
if not password:
return apology("Must provide password", 400)
# Ensure user has written confirmation of password
if not confirmation:
return apology("Must provide confirmation", 400)
# Ensure password in both fields(password and confirmation) is same
if password != confirmation:
return apology("Password and Confirmation donot match")
# Ensure password satisfies the requirements of one uppercase, one lowercase, one numeric and one symbol
ps = list(password.strip(" "))
requirement = password_check(ps)
if requirement == False:
return apology("Password Requirement")
# Register user by storing it in database. Here, javascript can be added.
db.execute("INSERT INTO users (username, hash, email) VALUES (?, ?, ?)", [username, generate_password_hash(password), email])
return redirect("/")
# User reached route via GET (as by clicking a link)
else:
return render_template("register.html")
@app.route("/details", methods=["GET", "POST"])
@login_required
def details():
# Accept user details
# User reached route via POST (as by submitting a form via POST)
if request.method == "POST":
# Variable initialization
hsc_score = request.form.get("hsc_score")
jee_cet_score = request.form.get("jee_cet_score")
p1 = request.form.get("p1")
p2 = request.form.get("p2")
p3= request.form.get("p3")
print(p1, p2, p3)
import pandas as pd
dt = pd.read_csv('Database2.csv', sep=",")
keywords = [p1]
searched_keywords = '|'.join(keywords)
branch = ["Computer Engineering"]
searched_branch = '|'.join(branch)
cutoff = ["83.35"]
cut = pd.to_numeric(cutoff)
searched_cutoff = '|'.join(cutoff)
def short_1():
data = dt[dt["Institute"].str.contains(searched_keywords) | dt["Institute"].str.contains(searched_keywords)]
data.to_csv("sl1.csv",sep=",", index=False)
short_1()
def short_2():
st = pd.read_csv("sl1.csv",sep=",")
alpha = st[st["Course Name"].str.contains(searched_branch) | st["Course Name"].str.contains(searched_branch)]
alpha.to_csv("sl2.csv",sep=",",index=False)
short_2()
def short_3():
import csv
import json
csvfile = open('sl2.csv', 'r')
jsonfile = open('sl2..json', 'w')
fieldnames = ("Sr.No","Rank","CUTOFF","Institute Name","Branch","Exam")
reader = csv.DictReader( csvfile, fieldnames)
for row in reader:
json.dump(row, jsonfile)
jsonfile.write('\n')
short_3()
return redirect("/index")
else:
# Coordinate tracked via IP address
iplocation = user_location()
print(iplocation)
return render_template("details.html")
@app.route("/logout")
def logout():
# Log user out
# Forget any user_id
session.clear()
# Redirect user to login form
return redirect("/")
def errorhandler(e):
# Basic error handeling
if not isinstance(e, HTTPException):
e = InternalServerError()
return apology(e.name, e.code)
# Listen for errors
for code in default_exceptions:
app.errorhandler(code)(errorhandler)
app.run(debug=True)