-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.py
More file actions
188 lines (153 loc) · 6.42 KB
/
app.py
File metadata and controls
188 lines (153 loc) · 6.42 KB
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
from flask import Flask, render_template, request, redirect, url_for, flash, session
import mysql.connector
import heapq # For Dijkstra's Algorithm
app = Flask(__name__)
app.secret_key = 'your_secret_key_here' # Required for session management
# Function to get a new database connection
def get_db_connection():
return mysql.connector.connect(
host="localhost",
user="root",
password="", # Add your MySQL password here
database="returnloop" # Updated database name
)
# Graph Representation of Kochi (Distances in KM)
graph = {
"Aluva": {"Edappally": 10, "Kakkanad": 14},
"Edappally": {"Aluva": 10, "Kaloor": 6, "Kakkanad": 8},
"Kaloor": {"Edappally": 6, "MG Road": 4},
"MG Road": {"Kaloor": 4, "Fort Kochi": 11},
"Fort Kochi": {"MG Road": 11, "Willingdon Island": 5},
"Willingdon Island": {"Fort Kochi": 5, "Thevara": 6},
"Thevara": {"Willingdon Island": 6, "Vyttila": 7},
"Vyttila": {"Thevara": 7, "Kakkanad": 9, "Tripunithura": 6},
"Kakkanad": {"Edappally": 8, "Aluva": 14, "Vyttila": 9},
"Tripunithura": {"Vyttila": 6}
}
# Dijkstra's Algorithm to find the shortest distance
def dijkstra(graph, start, end):
queue = [(0, start)] # (distance, node)
distances = {node: float('inf') for node in graph}
distances[start] = 0
visited = set()
while queue:
current_distance, current_node = heapq.heappop(queue)
if current_node in visited:
continue
visited.add(current_node)
if current_node == end:
return current_distance # Return total shortest distance
for neighbor, weight in graph[current_node].items():
distance = current_distance + weight
if distance < distances[neighbor]:
distances[neighbor] = distance
heapq.heappush(queue, (distance, neighbor))
return float('inf') # No path found
# Home route
@app.route('/')
def home():
user_name = session.get('name', None) # Get the user's name from the session
return render_template('home.html', user_name=user_name)
# Get Ride Route (Shortest Distance Calculation)
@app.route('/get_ride', methods=['GET', 'POST'])
def get_ride():
user_name = session.get('name', None)
if request.method == 'POST':
start = request.form.get('start_location')
destination = request.form.get('destination')
# Debugging Output
print(f"Ride Request - Start: {start}, Destination: {destination}")
# Store ride details in the `rides` table
conn = get_db_connection()
cursor = conn.cursor()
cursor.execute("INSERT INTO rides (start_location, destination, email) VALUES (%s, %s, %s)",
(start, destination, session.get('email', 'anonymous'))) # Use session email
conn.commit()
conn.close()
flash("Ride request submitted successfully!", "success")
return render_template('get_ride.html', user_name=user_name)
# Pooling Route
@app.route('/pooling', methods=['GET', 'POST'])
def pooling():
user_name = session.get('name', None)
# Fetch all pooling options from the database with username instead of email
conn = get_db_connection()
cursor = conn.cursor(dictionary=True)
cursor.execute("""
SELECT p.*, u.name as username
FROM pooling p
LEFT JOIN users u ON p.email = u.email
ORDER BY p.pickup, p.destination, p.date
""")
pooling_options = cursor.fetchall()
conn.close()
if request.method == 'POST':
pickup = request.form.get('location')
destination = request.form.get('destination')
date = request.form.get('date')
# Debugging Output
print(f"Pooling Request - Pickup: {pickup}, Destination: {destination}, Date: {date}")
# Store pooling details in the `pooling` table
conn = get_db_connection()
cursor = conn.cursor()
cursor.execute("INSERT INTO pooling (pickup, destination, date, email) VALUES (%s, %s, %s, %s)",
(pickup, destination, date, session.get('email', 'anonymous'))) # Use session email
conn.commit()
conn.close()
flash("Pooling request submitted successfully!", "success")
return redirect(url_for('pooling'))
return render_template('pooling.html', user_name=user_name, pooling_options=pooling_options)
# Signup Route
@app.route('/signup', methods=['GET', 'POST'])
def signup():
if request.method == 'POST':
name = request.form.get('name')
email = request.form.get('email')
phone = request.form.get('phone')
gender = request.form.get('gender')
password = request.form.get('password')
conn = get_db_connection()
cursor = conn.cursor()
cursor.execute("SELECT * FROM users WHERE email = %s", (email,))
existing_user = cursor.fetchone()
if existing_user:
flash("Email already registered. Please log in.", "warning")
conn.close()
return redirect(url_for('login'))
cursor.execute(
"INSERT INTO users (name, email, phone, gender, password) VALUES (%s, %s, %s, %s, %s)",
(name, email, phone, gender, password)
)
conn.commit()
conn.close()
flash("Signup successful! Please log in.", "success")
return redirect(url_for('login'))
return render_template('signup.html')
# Login Route
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
email = request.form.get('email')
password = request.form.get('password')
conn = get_db_connection()
cursor = conn.cursor()
cursor.execute("SELECT * FROM users WHERE email = %s AND password = %s", (email, password))
user = cursor.fetchone()
conn.close()
if user:
session['email'] = email # Store email in session
session['name'] = user[1] # Store user's name in session
flash("Login successful!", "success")
return redirect(url_for('home'))
else:
flash("Login failed. Check your email or password.", "danger")
return render_template('login.html')
# Logout Route
@app.route('/logout')
def logout():
session.pop('email', None) # Remove email from session
session.pop('name', None) # Remove name from session
flash("Logged out successfully.", "info")
return redirect(url_for('home'))
if __name__ == '__main__':
app.run(debug=True)