-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
212 lines (162 loc) · 6.46 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
from flask import Flask, flash, g, redirect, render_template, request, session, url_for
from werkzeug.security import generate_password_hash, check_password_hash
import functools
from datetime import datetime
from pickle import load, dump
import db
app = Flask(__name__)
app.config.from_mapping(SECRET_KEY=open("secret.txt").read())
db.init_app(app)
def login_required(view):
@functools.wraps(view)
def wrapped_view(**kwargs):
if not session.get("userid", None):
return redirect(url_for("landing"))
return view(**kwargs)
return wrapped_view
def is_female():
return session["userid"][0] == "F"
@app.route("/")
def landing():
if session.get("userid", None):
return render_template("landing.html")
else:
return render_template("index.html")
@app.route("/signup", methods=("GET", "POST"))
def signup():
if request.method == "POST":
name = request.form["name"]
female = request.form["gender"].lower() == "female"
phone_no = request.form["phoneNo"]
vit_mail = request.form["vitMail"]
password = request.form["password"]
database = db.get_db()
crs = database.cursor(dictionary=True)
error = None
"""
if x is None:
error = 'blah blah blah'
flash(error)
"""
if error is None:
crs.execute(
"INSERT INTO user (email, password, name, female, phone_number) "
f"VALUES ('{vit_mail}', '{generate_password_hash(password)}', '{name}', {female}, '{phone_no}')"
)
database.commit()
crs.execute("SELECT * FROM user ORDER BY userid DESC LIMIT 1")
record = crs.fetchone()
# add else condition in case there was a new record added before this one could be read off the database
session["userid"] = ("F" if record["female"]
else "M") + str(record["userid"])
return redirect(url_for("landing"))
return render_template("signup.html")
@app.route("/login", methods=("GET", "POST"))
def login():
if request.method == "POST":
vit_mail = request.form["vitMail"]
password = request.form["password"]
database = db.get_db()
crs = database.cursor(dictionary=True)
error = None
"""
if x is None:
error = 'blah blah blah'
flash(error)
"""
if error is None:
crs.execute(
f"SELECT userid,password,female FROM user where email='{vit_mail}'"
)
record = crs.fetchone()
if check_password_hash(record["password"], password):
session["userid"] = ("F" if record["female"] else "M") + str(
record["userid"]
)
return redirect(url_for("landing"))
else:
flash("Wrong credentials")
return render_template("login.html")
@app.route("/logout")
@login_required
def logout():
session.clear()
return redirect(url_for('landing'))
@app.route("/cabshare", methods=("GET", "POST"))
@login_required
def cab_share():
if request.method == "POST":
database = db.get_db()
crs = database.cursor(dictionary=True)
pickup_point = request.form["from"]
drop_point = request.form["to"]
date = request.form["date"]
if 'create' in request.form:
# for field in ['time', 'vehicleType', 'numPeople']:
# if field not in request.form:
# break
# else:
# crs.execute("INSERT INTO trips "
# "(trip_date, trip_time, pickup_point, drop_point, "
# "vehicle, num_people, gender_filter, user_1) VALUES "
# f"({date} {} {} {} {} {} {} {})")
pass
else:
if request.form["time"]:
date_time = datetime.strptime(
f"{date} {request.form['time']}", "%Y-%m-%d %H:%M"
)
gender_filter = request.form.get("genderFilter", "off") == "on"
num_people = request.form['numPeople']
vehicle_types = {"any": 0, "cab": 1, "auto": 2}
vehicle_type = vehicle_types.get(request.form["vehicleType"], 0)
crs.execute(
f"SELECT * FROM trips WHERE "
+ "resolved!=TRUE AND "
+ (f"vehicle={vehicle_type} OR vehicle=0 AND " if vehicle_type else "")
+ f"gender_filter={gender_filter} AND "
+ f"pickup_point='{pickup_point}' AND "
+ f"drop_point='{drop_point}' AND "
+ f"trip_date='{date}'"
)
records = crs.fetchall()
for record in records:
record["datetime"] = datetime.strptime(
f"{record['trip_date']} {record['trip_time']}", "%Y-%m-%d %H:%M:%S"
)
if request.form["time"]:
records.sort(key=lambda record: abs(
record["datetime"] - date_time))
else:
records.sort(key=lambda record: record["datetime"])
for record in records:
record['trip_time'] = str(record['trip_time'])[:-3]
return render_template("cabShare.html", form=request.form, records=records, female=is_female())
database = db.get_db()
crs = database.cursor(dictionary=True)
userid = session['userid'][1:]
crs.execute(f"SELECT * FROM trips WHERE {userid} IN "
"(user_1, user_2, user_3, user_4, user_5, user_6, user_7, user_8) "
"AND NOT RESOLVED")
my_trips = crs.fetchall()
for trip in my_trips:
trip['trip_time'] = str(trip['trip_time'])[:-3]
return render_template('cabShare.html', records=my_trips,
locations=load(open('locations.dat', 'rb')), female=is_female())
@app.route("/profile", methods=("GET", "POST"))
@login_required
def profile():
if request.method == "POST":
pass
userid = session["userid"][1:]
database = db.get_db()
crs = database.cursor(dictionary=True)
user_record = crs.execute(f"SELECT * FROM user WHERE userid={userid}")
return render_template("profile.html", user_record=user_record)
@app.route("/driverdetails")
def driver_details():
database = db.get_db()
crs = database.cursor(dictionary=True)
crs.execute("SELECT * FROM drivers")
records = crs.fetchall()
return render_template("driverDetails.html", records=records)