-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
executable file
·81 lines (64 loc) · 2.73 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
from flask import Flask, render_template, redirect, session, url_for, request, jsonify
from flask_pymongo import PyMongo
app = Flask(__name__)
app.config['SECRET_KEY'] = 'SECRET'
app.config['MONGO_DBNAME'] = 'dbms'
app.config['MONGO_URI'] = 'URI'
mongo = PyMongo(app)
@app.route('/')
def index():
if 'username' in session:
return render_template('search.html')
return render_template('index.html')
@app.route('/login', methods=['POST'])
def login():
users = mongo.db.users
login_user = users.find_one({'name' : request.form['username']})
if login_user:
if request.form['pass'] == login_user['password']:
session['username'] = request.form['username']
return redirect(url_for('index'))
return 'Invalid username/password combination'
@app.route('/register', methods=['POST', 'GET'])
def register():
if request.method == 'POST':
users = mongo.db.users
existing_user = users.find_one({'name' : request.form['username']})
if existing_user is None:
users.insert({'name' : request.form['username'], 'password' : request.form['pass']})
session['username'] = request.form['username']
return redirect(url_for('index'))
return 'That username already exists!'
return render_template('register.html')
@app.route('/search', methods = ['GET', 'POST'])
def search():
data = mongo.db.records
output = []
if len(request.form['student_name']) > 0:
if request.form['student_name'] == "all":
for x in range(1,data.count()+1):
student = data.find_one({'sno' :x})
temp = {'sno' : student['sno'], 'name' : student['name'], 'age' : student['age'], 'CGPA' : student['CGPA']}
output.append(temp)
else:
student = data.find_one({'name' : request.form['student_name']})
if student:
temp = {'sno' : student['sno'], 'name' : student['name'], 'age' : student['age'], 'CGPA' : student['CGPA']}
output.append(temp)
elif request.form['lower_bound'] is not None and request.form['upper_bound'] is not None:
for x in range(1,data.count()+1):
student = data.find_one({'sno':x})
if student['CGPA']>=int(request.form['lower_bound']) and student['CGPA']<=int(request.form['upper_bound']):
temp = {'sno' : student['sno'], 'name' : student['name'], 'age' : student['age'], 'CGPA' : student['CGPA']}
output.append(temp)
return render_template('search.html', output = output)
@app.route('/about')
def about():
return render_template('about.html')
@app.route('/logout')
def logout():
session.pop('username', None)
return redirect(url_for('index'))
if __name__ == '__main__':
app.secret_key = 'mysecret'
app.run(debug=True)