-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
156 lines (120 loc) · 4.16 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
from flask import Flask,render_template,request,redirect,session,flash,url_for
from functools import wraps
from flask_mysqldb import MySQL
import mysql.connector as db
from flask_caching import Cache
app=Flask(__name__)
app.config['MYSQL_HOST']='localhost'
app.config['MYSQL_USER']='root'
app.config['MYSQL_PASSWORD']='Sravani@364'
app.config['MYSQL_DB']='RED_BUS'
app.config['MYSQL_CURSORCLASS']='DictCursor'
mysql=MySQL(app)
mydb = db.connect(
host="localhost",
user="root",
db = "RED_BUS",
password = "Sravani@364"
)
cur = mydb.cursor()
#Login
@app.route('/')
@app.route('/login',methods=['POST','GET'])
def login():
status=True
if request.method=='POST':
mobile_number=request.form["mobile_no"]
pwd=request.form["password"]
cur=mysql.connection.cursor()
cur.execute("select * from user_info where mobile_no = %s and password=%s",(mobile_number,pwd))
data=cur.fetchone()
if data:
session['logged_in']=True
session['mobile_number']=data["mobile_no"]
session['username'] = data["name"]
flash('Login Successfully','sucess')
return redirect(url_for('home'))
else:
flash('Invalid Login. Try Again','danger')
return render_template("login.html")
#check if user logged in
def is_logged_in(f):
@wraps(f)
def wrap(*args,**kwargs):
if 'logged_in' in session:
return f(*args,**kwargs)
else:
flash('Unauthorized, Please Login','danger')
return redirect(url_for('login'))
return wrap
#Registration
@app.route('/reg',methods=['POST','GET'])
def reg():
status=False
if request.method=='POST':
name=request.form["username"]
mobile_number=request.form["mobile_no"]
pwd=request.form["password"]
cur=mysql.connection.cursor()
cur.execute("insert into user_info(name,password,mobile_no) values(%s,%s,%s)",(name,pwd,mobile_number))
mysql.connection.commit()
cur.close()
flash('Registration Successfully. Login Here...','success')
return redirect('login')
return render_template("reg.html",status=status)
#Home page
@app.route("/home")
@is_logged_in
def home():
cur.execute('SELECT * FROM bus_available')
data = cur.fetchall()
return render_template('home.html', result = data)
@app.route('/bus_available',methods=['GET','POST'])
@is_logged_in
def bus_available():
cur.execute('SELECT * FROM bus_deatails')
data = cur.fetchall()
return render_template('bus_available.html', result = data)
@app.route('/reg_details',methods=['GET','POST'])
@is_logged_in
def reg_details():
status=False
if request.method=='POST':
user_id=request.form["user_id"]
bus_id=request.form["bus_id"]
source = request.form['source']
destination = request.form['destination']
cur=mysql.connection.cursor()
cur.execute("insert into user_reg_details(user_id,bus_id,source,destination) values(%s,%s,%s,%s)",(user_id,bus_id,source,destination))
mysql.connection.commit()
cur.close()
flash('Bus Booked Successfully','success')
return redirect(url_for('book'))
return render_template("reg_details.html",status=status)
# @app.route('/reg',methods=['POST','GET'])
# def bus_available():
# source_=request.form["source"]
# destination=request.form["destination"]
# date = request.form['date']
# cur=mysql.connection.cursor()
# cur.execute("select * from bus_available where source = %s and destination=%s and date = %s",(source_,destination,date))
# data=cur.fetchall()
# return render_template('bus_avaiable.html',result = data)
@app.route("/book")
def book():
return redirect(url_for('home'))
#logout
@app.route("/logout")
def logout():
session.clear()
flash('You are now logged out','success')
return redirect(url_for('login'))
#caching-time for the application
@app.after_request
def add_header(response):
response.headers['X-UA-Compatible'] = 'IE=Edge,chrome=1'
response.headers['Cache-Control'] = 'public,max-age = 30'
return response
if __name__=='__main__':
app.secret_key='secret123'
app.run(debug=True)