-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
137 lines (109 loc) · 4.28 KB
/
main.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
import pymysql
from conda.gateways import connection
from flask import Flask, render_template, request, redirect, session, url_for, flash
from flask_login import login_required, logout_user
from sqlalchemy.sql.functions import *
connection = pymysql.connect(host='localhost', user='root', password='', database='Travel')
print("Database connection successful")
# def check_user():
# if 'email' in session:
# return True
# else:
# return False
# start
app = Flask(__name__)
app.secret_key = "IloveProgramming"
@app.route('/')
def index():
# login = False
# if 'username' in session:
# login = True
if 'key' in session:
return render_template('index.html', login=login)
else:
return redirect('/login')
@app.route('/signup', methods=['POST', 'GET'])
def sign_up():
if request.method == 'POST':
user_name = request.form['username']
user_email = request.form['email']
user_password = request.form['password']
connection = pymysql.connect(host='localhost', user='root', password='', database='Travel')
print("Database connection successful")
cursor = connection.cursor()
sql = 'INSERT INTO users (username, email, password) VALUES (%s,%s,%s)'
cursor.execute(sql, (user_name, user_email, user_password))
connection.commit()
return render_template('login.html', message="Registered successfully")
else:
return render_template('signup.html')
@app.route('/login', methods=['POST', 'GET'])
def login():
if request.method == 'POST':
user_email = request.form['email']
user_password = request.form['password']
connection = pymysql.connect(host='localhost', user='root', password='', database='Travel')
print("Database connection successful")
cursor = connection.cursor()
sql = 'SELECT * FROM users WHERE email =%s AND password =%s'
cursor.execute(sql, (user_email, user_password))
if cursor.rowcount == 0:
return render_template('login.html', error="Invalid Credentials, Try Again")
elif cursor.rowcount == 1:
row = cursor.fetchone()
session['logged_in'] = True
session['username'] = row[1]
session['email'] = row[2] # user_email
session['user_id'] = row[0]
session['key'] = row[1]
return redirect('/')
else:
return render_template('login.html', error="Something wrong with credentials")
else:
return render_template('login.html')
@app.route('/booking', methods=['POST', 'GET'])
def booking():
if 'key' in session:
if request.method == 'POST':
email = request.form['email']
phone = request.form['phone']
destination_to = request.form['destTo']
destination_from = request.form['destFrom']
booking_time = request.form['time']
booking_date = request.form['date']
connection = pymysql.connect(host='localhost', user='root', password='', database='Travel')
print("Database connection successful")
cursor = connection.cursor()
sql = 'INSERT INTO book (email, phone, destination_to, destination_from, booking_time, booking_date) ' \
'VALUES (%s,%s,' \
'%s,%s,%s,%s) '
cursor.execute(sql, (email, phone, destination_to, destination_from, booking_time, booking_date))
connection.commit()
return render_template('index.html', message="Trip booked successfully")
else:
return render_template('booking.html')
else:
return redirect('/login')
@app.route('/bookings')
def check_booking():
cursor = connection.cursor()
sql = 'SELECT * FROM book'
cursor.execute(sql)
book = cursor.fetchall()
return render_template('bookings.html', book=book)
@app.route('/users')
def check_users():
cursor = connection.cursor()
sql = 'SELECT * FROM users'
cursor.execute(sql)
users = cursor.fetchall()
return render_template('users.html', users=users)
# Admin Login
@app.route('/admin')
def admin():
return render_template('admin.html')
@app.route('/logout')
def logout():
session.pop('username', None)
return redirect('/login')
app.run(debug=True)